D. Count Good Substrings
题目连接:
http://codeforces.com/contest/451/problem/D
Description
We call a string good, if after merging all the consecutive equal characters, the resulting string is palindrome. For example, “aabba” is good, because after the merging step it will become “aba”.
Given a string, you have to find two values:
the number of good substrings of even length;
the number of good substrings of odd length.
Input
The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105).
Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case.
Output
Print two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length.
Sample Input
bb
Sample Output
1 2
Hint
题意
他定义了一个叫做good串的东西,就是重叠的字符就算作一个,然后如果重叠算一个,且最后是回文串的话,那么他就是好的。
现在给你一个串,问你他的奇数长度good,和偶数长度good各有多少个。
题解:
由于只含有ab,所以这种回文串一定是ababababa这种的,那么只要两端相同就好了。
然后我们统计一下就行。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
char s[maxn];
long long cnt[3][3];
int main()
{
scanf("%s",s);
int len=strlen(s);
long long odd=0,even=0;
for(int i=0;i<len;i++)
{
cnt[s[i]-'a'][i%2]++;
even+=cnt[s[i]-'a'][1-i%2];
odd+=cnt[s[i]-'a'][i%2];
}
printf("%lld %lld\n",even,odd);
}