时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
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
#include "iostream"
#include "set"
#include "algorithm"
#include "string"
#include "string.h"
#include "stdlib.h"
using namespace std;
int N;
char c[10485761]; //用(c,n)来表示一串连续相同的字母,比如"aaa"表示(a,3),"bb"表示为(b,2)
int n[10485761];
int ok(int len)
{
int i;
for(i=0; i<len-2; i++)
{
if(c[i]+1 == c[i+1] && c[i+1]+1 == c[i+2] && n[i] >= n[i+1] && n[i+1] <= n[i+2])
return 1;
}
return 0;
}
int main()
{
cin >> N;
while(N--)
{
int len;
cin >> len;
string s;
cin >> s;
int i;
int index = 0;
memset(n, 0, sizeof(n));
memset(c, 0, sizeof(c));
for(i=0; i<len; i++)
{
int num = 0;
while(s[i] == s[i+1])
{
i++;
num++;
}
c[index] = s[i];
n[index++] = num;
}
if(ok(len) == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}