• 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;
    }
  • 相关阅读:
    layer子层给父层页面元素赋值,以达到向父层页面传值的效果
    根据HttpServletRequest获取用户真实IP地址
    KVM--安装及初步使用
    eslint 踩坑 -- error '***' is assigned a value but never used no-unused-vars
    eslint 效验规则
    leetcode407接雨水II
    leetcodej剑指offer41.数据流中的中位数
    leetcode44通配符匹配
    leetcode955K连续位的最小反转次数
    leetcode638大礼包
  • 原文地址:https://www.cnblogs.com/zhien-aa/p/6279520.html
Copyright © 2020-2023  润新知