码迷,mamicode.com
首页 > 其他好文 > 详细

hiho一下 第五十八周 Beautiful String dp

时间:2015-08-09 17:19:59      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

题目1 : Beautiful String

时间限制: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
题意,给一个字符串,是否存在一个连续子串,满足子串有大于3种字符,是递增的顺序且每种字符个数相同,如a..ab...bc...c其中a b c个数相等,且是递增的。由于,当大于3个的时候,总会3个字符是满足的,所以只用考虑是3个的情况,num[i] 表示,第i个位置,向后最多有num[i]个字符是相同的。如果,有这样的x1...x2...x3...,x1x2x3递增,且num[x2] == num[x3] ,num[x3] >= num[x1](由于可以存在这样的串aabbccc,所以只要,num[x3] >= x[1]),str[x1] == str[i] + 1 &&  str[x2] == str[i] + 2&& num[x1] == num[i] && num[x2] >= num[i] 则说明存在。
#define N 10000050
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
int n,num[N],len;
char str[N];
int main()
{
     while(S(n)!=EOF)
    {
        while(n--){
            S(len);
            gets(str);
            FI(len){
                str[i] = getchar();
            }
            num[len - 1] = 1;
            for(int i = len - 2 ;i>=0;i--){
                if(str[i] == str[i+1])
                    num[i] = num[i+1]  + 1;
                else
                    num[i] = 1;
            }
            bool flag = false;
            FI(len){
                int x1 = i + num[i],x2 = i + 2 * num[i],x3 = i + 3 * num[i];
                if(x3 <= len && str[x1] == str[i] + 1 &&  str[x2] == str[i] + 2
                   && num[x1] == num[i] && num[x2] >= num[i] ){
                    flag = true;
                    break;
                }
            }
            if(flag){
                printf("YES\n");
            }
            else {
                printf("NO\n");
            }
        }
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

hiho一下 第五十八周 Beautiful String dp

标签:

原文地址:http://blog.csdn.net/mengzhengnan/article/details/47375415

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!