Hihocoder1061-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

题意

如果给出的字符串有连续递增的三个字母或者有等量连续递增的三个字母,则输出YES,结合样例理解。

思路

我们只需要判断到3个就行了,用一个cnt数组记录对应字母的个数,把中间那个拿出来比较,只要又一个不同就将上一个扫描过的字母放到第一个不同的字母那里方便比较,就是中间的小于等于两边且是属于且中间的字母也小于等于前后的的就符合条件

代码

#include<bits/stdc++.h>
using namespace std;
#define maxn 10000000
int cnt[maxn];
int main() {
    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        string s;
        cin >> s;
        int flag = 0;
        int cur = 0;
        cnt[cur] = 1;
        for(int i = 1; i < n; i++) {
            if(s[i] == s[i - 1])
                cnt[cur]++;//字母相同时cur没变,相当于去重
            else {
                s[++cur] = s[i];//
                cnt[cur] = 1;
            }
        }
        for(int i = 1; i < cur; i++) {
            if(s[i] == s[i - 1] + 1 && s[i] + 1 == s[i + 1] && cnt[i] <= cnt[i - 1] && cnt[i] <= cnt[i + 1]) {
                flag = 1;
                break;
            }
        }
        puts(flag ? "YES" : "NO");
    }
    return 0;
}

 

posted @ 2017-01-12 19:30  April_AA  阅读(314)  评论(0编辑  收藏  举报