Codeforces Round #258 (Div. 2) D

D. Count Good Substrings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output


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:

  1. the number of good substrings of even length;
  2. the number of good substrings of odd length.
Input

The first line of the input contains a single string of length n (1 ≤ n ≤ 105). Each character of the string will be either 'a' or 'b'.

Output

Print two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length.

Sample test(s)
input
bb
output
1 2
input
baab
output
2 4
input
babb
output
2 5
input
babaa
output
2 7

 

 sl :分析发现最后回文串的第一个字符和第二个字符相同,这样统计相应数位上的字符就行了。

 

 1 //by caonima
 2 //hehe
 3 #include <bits/stdc++.h>
 4 typedef long long LL;
 5 const int MAX = 1e5+10;
 6 char str[MAX];
 7 LL even_cnt[2],odd_cnt[2];
 8 // odd ji even o
 9 int main() {
10     LL odd,even;
11     while(scanf("%s",str+1)==1) {
12         memset(even_cnt,0,sizeof(even_cnt));
13         memset(odd_cnt,0,sizeof(odd_cnt));
14         int n=strlen(str+1);
15         odd=even=0;
16         for(int i=1;i<=n;i++) {
17             int x=str[i]-'a';
18             if(i&1) {
19                 odd+=odd_cnt[x];
20                 even+=even_cnt[x];
21                 odd_cnt[x]++;
22             }
23             else {
24                 odd+=even_cnt[x];
25                 even+=odd_cnt[x];
26                 even_cnt[x]++;
27             }
28         }
29         odd+=n;
30         printf("%I64d %I64d\n",even,odd);
31     }
32     return 0;

33 } 

 

posted @ 2014-08-15 19:25  acvc  阅读(165)  评论(0编辑  收藏  举报