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”.
Input
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.
Output
For each test case, output a single line “YES”/“NO” to tell if the string contains a beautiful sub-string.
Sample Input
4
3
abc
4
aaab
6
abccde
3
abb
Sample Output
YES
NO
YES
NO
Hint
Huge input. Slow IO method such as Scanner in Java may get TLE.
题意:
判断给出的字符串是否为漂亮字符串??
问:何为漂亮字符串?
答:连续字母至少有三个且连续紧挨字母的个数相同
例:漂亮字符:abc,cde,aabcc
不漂亮字符:abd,aabbc
思路:
把字符串转换成有字母和数字的形式.
即:例aabbc–>a2b2c1 aabcc–>a2b1c2
转换之后判断:
如果一个字母的前一个字母比它小1且这个字母的后一个字母比它大1(ASCII码判断),满足字母连续条件。
如果一个字母的个数比前一个字母的个数小或者等于且这个字母也小于等于后一个字母的个数,即满足连续字母的个数条件 以上两个条件即可判断字符串是否为漂亮字符串
代码:
#include<stdio.h>
#include<string.h>
char a[1000000];
int s[1000000];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(s,0,sizeof(s));
memset(a,0,sizeof(a));
int n,i;
scanf("%d",&n);
scanf("%s",a);
int j=0;
s[j]=1;
for(i=1; i<n; i++) //字符串转换
{
if(a[i]==a[i-1])
s[j]++;
else
{
a[++j]=a[i]; //j++不对
s[j]=1;
}
}
int f=0;
for(i=1; i<n; i++)
{
if(a[i-1]+1==a[i]&&a[i]+1==a[i+1]&&s[i-1]>=s[i]&&s[i+1]>=s[i])
{
f=1;
break;
}
}
if(f)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
本文介绍了一种用于识别特定模式的漂亮字符串的算法。漂亮字符串定义为包含至少三个连续字母且这些字母数量相等的字符串。文章详细解释了如何将原始字符串转换为便于处理的形式,并通过示例展示了算法的具体实现。
504

被折叠的 条评论
为什么被折叠?



