Question: beautifulstring
Time Limit: 10000 ms
Single-point time limit: 1000 ms
Memory limit: 256 MB
Description
We say a stringis beautiful if it has the equal amount of 3 or more continuous letters (inincreasing order .)
Here are someexample of valid beautiful strings: "ABC", "CDE", "aabbcc", "aaabbbccc ".
Here are someexample of invalid beautiful strings: "Abd", "CBA", "aabbc", "Zab ".
Given a stringof alphabets containing only lowercase Alphabets (a-z), output "yes" if the string contains a beautiful sub-string, otherwise output "no ".
Input
The first linecontains an integer number between 1 and 10, indicating how many test cases arefollowed.
For each testcase: first line is the number of letters in the string; second line is thestring. String Length is less than 10 MB.
Output
For each testcase, output a single line "yes"/"no" to tell if the stringcontains a beautiful sub-string.
Prompt
Huge input. slowio method such as needed in Java may get TLE.
Sample Input
4
3
ABC
4
Aaab
6
Abccde
3
ABB
Sample output
Yes
No
Yes
No
The answers I submitted and AC are:
# Include <iostream>
# Include <stdio. h>
Using namespace STD;
Int main ()
{
Int I;
Int T;
Int N;
Scanf ("% d", & T );
While (t --)
{
Scanf ("% d", & N );
Getchar ();
Int CNT [3], Loc = 0;
Char CH, pre = '\ 0 ';
Bool flag = false;
// Memset (CNT, 0, sizeof (CNT ));
For (I = 0; I <n; I ++)
{
Scanf ("% C", & Ch );
// If (CH <'A' | ch> 'Z') {I --; continue ;}
If (FLAG) {continue ;}
If (CH = pre) {CNT [loc] ++ ;}
Else {
If (loc = 2 & CNT [0]> = CNT [1] & CNT [2]> = CNT [1]) {
Flag = true;
Continue;
}
Else if (loc = 2 & (pre + 1 = CH )){
CNT [0] = CNT [1], CNT [1] = CNT [2];
CNT [2] = 1;
}
Else if (pre + 1 = CH ){
CNT [++ loc] = 1;
}
Else {
Loc = 0;
CNT [loc] = 1;
}
Pre = CH;
}
}
If (loc = 2 & CNT [0]> = CNT [1] & CNT [2]> = CNT [1]) {flag = true ;}
If (FLAG) {printf ("Yes \ n ");}
Else {printf ("NO \ n ");}
} // End while
Return 0;
}
The basic idea is: to form a beautiful string, it must be composed of three consecutive letters, and the number of intermediate letters must be smaller than the number of front and back letters, so you just need to find the combination.
A question for taking part in Microsoft Online Testing