codeforces D. Count Good Substrings

 http://codeforces.com/contest/451/problem/D

题意:给你一个字符串,然后找出奇数和偶数长度的子串的个数,这些字串符合,通过一些连续相同的字符合并后是回文串。

思路:因为这些字符串中的字符只有'a','b',所以首位相同的字串都可以满足,这样就分别统计奇数和偶数位置的字符的个数,然后相互组合就可以。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #define maxn 100010
 6 #define ll long long
 7 using namespace std;
 8 
 9 char str[maxn];
10 ll o[maxn];
11 ll e[maxn];
12 
13 int main()
14 {
15     cin>>str;
16     int k=strlen(str);
17     ll odd=0;
18     ll even=0;
19     memset(o,0,sizeof(o));
20     memset(e,0,sizeof(e));
21     for(int i=0; i<k; i++)
22     {
23        odd++;
24        int x=str[i]-'a';
25        if(i%2==0)
26        {
27            odd+=o[x];
28            even+=e[x];
29            o[x]++;
30        }
31        else
32        {
33            odd+=e[x];
34            even+=o[x];
35            e[x]++;
36        }
37     }
38     printf("%I64d %I64d
",even,odd);
39     return 0;
40 }
View Code
原文地址:https://www.cnblogs.com/fanminghui/p/4342339.html