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
- hiho一下~week58 Beautiful String
- hiho一下 第五十八周 Beautiful String dp
- hiho一下 微软笔试题《String Matching Content Length》
- hiho一下第60周《String Matching Content Length》
- hiho一下 第六十周 String Matching Content Length
- Beautiful String
- Beautiful String
- hiho一下 第八周
- hiho一下 第十五周
- hiho一下 Trie树
- hiho一下 第一周
- hiho一下 第二周
- hiho一下 第三周
- hiho一下1015
- hiho一下 第五十周
- hiho一下 第五十一周
- hiho一下 欧拉路三
- hiho一下 KMP 算法
- android中延迟执行某个任务
- 使用自定义控件和ListView实现快速索引功能
- 欢迎使用CSDN-markdown编辑器
- 依赖ijkplayer播放视频的项目
- Openstack组件部署 — keystone(domain, projects, users, and roles)
- hiho一下~week58 Beautiful String
- 乐视笔试第二编程题(一)
- 【JavaScript笔记二】请和我互动(常用互动方法)
- Linux系统下的/etc/nsswitch.conf文件
- Sublime3 快捷键
- LeetCode 352. Data Stream as Disjoint Intervals(数据流区间)
- Count Numbers with Unique Digits
- As3嵌入字体
- ios学习路线—Objective-C(通知机制)