Beautiful String HihoCoder - 1061(字符串处理)

本文介绍了一种用于识别特定模式的漂亮字符串的算法。漂亮字符串定义为包含至少三个连续字母且这些字母数量相等的字符串。文章详细解释了如何将原始字符串转换为便于处理的形式,并通过示例展示了算法的具体实现。
Seed-Coder-8B-Base

Seed-Coder-8B-Base

文本生成
Seed-Coder

Seed-Coder是一个功能强大、透明、参数高效的 8B 级开源代码模型系列,包括基础变体、指导变体和推理变体,由字节团队开源

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

您可能感兴趣的与本文相关的镜像

Seed-Coder-8B-Base

Seed-Coder-8B-Base

文本生成
Seed-Coder

Seed-Coder是一个功能强大、透明、参数高效的 8B 级开源代码模型系列,包括基础变体、指导变体和推理变体,由字节团队开源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值