hiho一下~week58 Beautiful String

题目意思:
http://hihocoder.com/contest/hiho58/problem/1
给定一个有序的小写字母字符串,判断这个字符串中有没有 符合要求的子串 要求出现至少出现三个连续的字母 且每个字母的个数相等 。
例如:
Here are some example of valid beautiful strings: “abc”, “cde”, “aabbcc”, “aaabbbccc”.

Here are some example of invalid beautiful strings: “abd”, “cba”, “aabbc”, “zab”.

题目注意:
直接统计每个字母出现的个数,进行判读即可。注意最前最后的字母是可以丢弃的 所以要求两边的个数比中间大于等于。可以用滚动数组减少空间的开销。

AC代码:

/**
 *Author: xiaoran
 *Solution:压塑判断
 *
 */
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<time.h>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<fstream>
#define LL long long
using namespace std;
const int MAXN=1000005;
char s[MAXN],res[MAXN];
int a[MAXN];

int main()
{

    //freopen("E:/input.txt","r",stdin);
    //freopen("E:/output.txt","w",stdout);
    int n,t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%s",&n,s);
        int i,ok=0,k=0,cnt=1;
        res[k]=s[0];
        for(i=1;i<n;i++){
            if(s[i]==s[i-1]){
                cnt++;
            }
            else{
                a[k++]=cnt;
                res[k]=s[i];
                cnt=1;
            }
        }
        a[k++]=cnt;
        res[k]='\0';
        /**
        for(i=0;i<k;i++){
            cout<<res[i]<<" "<<a[i]<<endl;
        }
        **/
        for(i=0;i<k-2;i++){//两边是可以丢弃的,保证两边个数大于等于中间即可。
            if(res[i]+1==res[i+1]&&res[i+1]+1==res[i+2]&&a[i]>=a[i+1]&&a[i+1]<=a[i+2]){
                ok=1; break;
            }

        }
        if(ok) printf("YES\n");
        else printf("NO\n");

    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值