ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络练习赛 题目4 : Beautiful String
We say a string is beautiful if it has the equal amount of 3 or more continuous letters (in increasing order.)
Here are some example of valid beautiful strings: "abc", "cde", "aabbcc", "aaabbbccc".
Here are some example of invalid beautiful strings: "abd", "cba", "aabbc", "zab".
Given a string of alphabets containing only lowercase alphabets (a-z), output "YES" if the string contains a beautiful sub-string, otherwise output "NO".
输入
The first line contains an integer number between 1 and 10, indicating how many test cases are followed.
For each test case: First line is the number of letters in the string; Second line is the string. String length is less than 10MB.
输出
For each test case, output a single line "YES"/"NO" to tell if the string contains a beautiful sub-string.
提示
Huge input. Slow IO method such as Scanner in Java may get TLE.
样例输入
4 3 abc 4 aaab 6 abccde 3 abb
样例输出
YES NO YES NO
1 #include<iostream> 2 #include<vector> 3 #include<string> 4 using namespace std; 5 6 int main() { 7 int n; 8 cin>>n; 9 while( n-- ) { 10 vector<char> v; 11 int num; 12 cin>>num; 13 string str; 14 cin>>str; 15 vector<char> v1; 16 vector<int> v2; 17 v1.push_back(str[0]); 18 v2.push_back(1); 19 int flag = 1; 20 21 for( int i = 1; i < str.size(); i++ ) { 22 if( str[i] == str[i-1] ) { 23 int temp = *(v2.end()-1); 24 temp++; 25 v2.pop_back(); 26 v2.push_back(temp); 27 if( v2[1] == v2[2] && v2[0] >= v2[1] && v1[2] - v1[1] == 1 && v1[1] - v1[0] == 1) { 28 cout<<"YES"<<endl; 29 flag = 0; 30 break; 31 } 32 } 33 else { 34 if( v1.size() < 3 ) { 35 v1.push_back(str[i]); 36 v2.push_back(1); 37 if( v2[1] == v2[2] && v2[0] >= v2[1] && v1[2] - v1[1] == 1 && v1[1] - v1[0] == 1) { 38 cout<<"YES"<<endl; 39 flag = 0; 40 break; 41 } 42 } 43 else { 44 v1.erase(v1.begin()); 45 v2.erase(v2.begin()); 46 v1.push_back(str[i]); 47 v2.push_back(1); 48 if( v2[1] == v2[2] && v2[0] >= v2[1] && v1[2] - v1[1] == 1 && v1[1] - v1[0] == 1) { 49 cout<<"YES"<<endl; 50 flag = 0; 51 break; 52 } 53 54 } 55 } 56 } 57 if( flag ) 58 cout<<"NO"<<endl; 59 } 60 return 0; 61 }