hiho一下~week58 Beautiful String

来源:互联网 发布:网络星期一 2015 编辑:程序博客网 时间:2024/06/09 17:36

题目意思:
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 longusing 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;}
0 0
原创粉丝点击