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]++;
            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;
}

?

时间: 2024-10-12 16:58:41

Hihocoder1061-Beautiful String的相关文章

hiho一下 第五十八周 Beautiful String dp

题目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"

微软校招编程题&quot;Beautiful String&quot;的状态机解法

昨天碰巧看到一道微软校招的编程题,题目大意如下: 如果一个字符串包括三组或者更多组的连续升序字母,每组长度相等,那么我们就称这个字符串是Beautiful String 如下是一些Beautiful String的例子: abc.cde.aabbcc.aaabbbccc 这些不是Beautiful String: abd.cba.aabbc.zab 输入一个只含有小写字母的字符串,如果它含有一个Beautiful的子串,就输出YES,否则输出NO 输入: 第一行是案例个数,之后的每一行是一个数字

Codeforces Round #604 (Div. 2) A. Beautiful String

链接: https://codeforces.com/contest/1265/problem/A 题意: A string is called beautiful if no two consecutive characters are equal. For example, "ababcb", "a" and "abab" are beautiful strings, while "aaaaaa", "abaa&

hiho一下:Beautiful String

hiho一下:Beautiful String 记不清这是 hiho一下第几周的题目了,题目不难,不过对于练习编程,训练思维很有帮助.况且当时笔者处于学习算法的早期, 所以也希望刚接触算法的同学能多去练习一下. 题目介绍 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 We say a string is beautiful if it has the equal amount of 3 or more continuous letters (in increasi

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 exa

hihocoder 1061.Beautiful String

题目链接:http://hihocoder.com/problemset/problem/1061 题目意思:给出一个不超过10MB长度的字符串,判断是否里面含有一个beautiful strings的子串:连续递增且数量相等的字母. 照着题目分析翻译的代码... 分析得很到位呢,大赞 ^_^ http://hihocoder.com/discuss/question/2083 hiho的题目其实挺好的,有专题,有分析,有代码 & 思路参考... 想想出来工作那么久,浮躁的心啊,一个多快两个月没

Pasha and String(思维,技巧)

Pasha and String Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 525B Description Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. Th

UESTC_Ferris Wheel String 2015 UESTC Training for Search Algorithm &amp; String&lt;Problem L&gt;

L - Ferris Wheel String Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 43000/43000KB (Java/Others) Submit Status Have you ever been to London? Our Master Qiu will tell you how amazing in London and how funny a Ferris Wheel String is. One day

Codeforces Round #297 (Div. 2)B Pasha and String

B. Pasha and String time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the

hiho_1061_beautiful_string

题目大意 题目连接:beautiful string     写代码之前,考虑清楚流程,以及需要维护的变量.... 实现 #include<iostream> #include<stdio.h> #include<string.h> #include<stack> #include<vector> #include<unordered_set> #include<unordered_map> using namespace