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:
- 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 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.
Examples
input
bb
output
1 2
input
baab
output
2 4
input
babb
output
2 5
input
babaa
output
2 7
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#define maxn 1000005
#define MOD 1000000007
#define INF 1e9
using namespace std;
typedef long long ll;
char str[maxn];
ll get(int m){
return (ll)m * (m-1) / 2;
}
int main(){
// freopen("in.txt", "r", stdin);
scanf("%s", str);
int p1 = 0, p2 = 0;
int k1 = 0 , k2 = 0;
for(int i = 0; str[i]; i++){
if(str[i] == 'a'){
if(i&1)
p1++;
else
p2++;
}
else{
if(i&1)
k1++;
else
k2++;
}
}
ll odd, even;
even = (ll)p1 * p2 + (ll)k1 * k2;
odd = get(p1) + get(p2) + get(k1) + get(k2) + strlen(str);
cout << even << " " << odd << endl;
return 0;
}