Count good substringstime limit: 2000 msmemory limit: 262144 kbthis problem will be judged on codeforces. Original ID: 451d
64-bit integer Io format: % i64d Java class name: (any) 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 LengthN(1 digit ≤ DigitNLimit ≤ limit 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 inputinput
bb
Output
1 2
Input
baab
Output
2 4
Input
babb
Output
2 5
Input
babaa
Output
2 7
Hint
In example 1, there are three good substrings ("B", "B", and "BB"). One of them has even length and two of them have odd length.
In Example 2, there are six good substrings (I. e. "B", "A", "A", "B", "AA", "Baab "). two of them have even length and four of them have odd length.
In Example 3, there are seven good substrings (I. e. "B", "A", "B", "B", "BB", "Bab", "babb "). two of them have even length and five of them have odd length.
Definitions
A substringS[L, Bytes,R] (1 limit ≤ limitLLimit ≤ limitRLimit ≤ limitN) Of stringSSignature = SignatureS1S2...SNIs stringSLSLLifecycle + lifecycle 1...SR.
A stringSSignature = SignatureS1S2...SNIs a palindrome if it is equal to stringSNSNAudio-extract 1...S1.
Sourcecodeforces round #258 (Div. 2) Problem-solving: compress continuous identical characters into one! The rest is the string between A and B. What we need is the compressed input string. Of course, only the first sub-string with the same name can be used. How can we find the number of identical first strings in the original string in linear time? Because there are only two types of characters: 'A' and 'B', pay attention to the details! In a natural number, a closed interval between an odd and an odd number contains an odd number. The closed interval between an odd and an even number contains an even number. A closed interval between an even number and an even number must contain an even number. A string with an odd length is either an even to an even number or an odd to an odd number. Similarly, we can conclude that strings with an even length can only be from an odd to an even number or even to an odd number! For example, if it is a at an odd position, how many strings are used as the backend compressed and retrieved? Look at the header. the header is an odd number. It must be an odd number. The first is an even number. It must be an even number! How many odd strings are there? You can find the number of A-containing and odd-number positions in front of the string, and then you can spell out the number of compressed and retrieved strings ending with 'A!
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <algorithm> 6 # include <climits> 7 # include <vector> 8 # include <queue> 9 # include <cstdlib> 10 # include <string> 11 # include <set> 12 # include <stack> 13 # define ll long long14 # define INF 0x3f3f3f15 using namespace STD; 16 LL odd [2], even [2], X, Y; 17 char STR [100100]; 18 int main () {19 while (~ Scanf ("% s", STR) {20 memset (odd, 0, sizeof (ODD); 21 memset (even, 0, sizeof (even )); 22 x = y = 0; 23 for (INT I = 0; STR [I]; I ++) {24 int sb = STR [I]-'A '; 25 if (I & 1) {26 x + = odd [SB]; // the odd to odd values are 27 y + = even [SB]; // 28 odd [SB] ++; 29} else {30 x + = even [SB]; 31 y + = odd [SB]; 32 even [SB] ++; 33} 34} 35 printf ("% i64d % i64d \ n", Y, x + strlen (STR); 36} 37 return 0; 38}
View code