HDU
来源:互联网 发布:中国古代数学 知乎 编辑:程序博客网 时间:2024/05/17 07:52
Theme Section
It's time for music! A lot of popular musicians are invited to join us in the music festival. Each of them will play one of their representative songs. To make the programs more interesting and challenging, the hosts are going to add some constraints to the rhythm of the songs, i.e., each song is required to have a 'theme section'. The theme section shall be played at the beginning, the middle, and the end of each song. More specifically, given a theme section E, the song will be in the format of 'EAEBE', where section A and section B could have arbitrary number of notes. Note that there are 26 types of notes, denoted by lower case letters 'a' - 'z'.To get well prepared for the festival, the hosts want to know the maximum possible length of the theme section of each song. Can you help us?
5xyabcaaaaaaabaaaxoaaaaa
00112
题意:
问是否存在这样形式的字符串:ABACA
其中B,C可以是空串,A是长度至少为一的串。
若存在则输出A的最长长度,否则输出0
主要利用 KMP 中的 next 数组来解决。
nex[len]表示第len位之前的字符串中 前nex[len]位 和 后nex[len] 相等
即 adbdad nex[6]=2;
(具体解析见代码)
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;char b[1000005];int nex[1000005];void get_next(char *b){int m=strlen(b);int j=-1,i=0;nex[0]=-1;while(i<m){if(j==-1 || b[i]==b[j])nex[++i]=++j;else j=nex[j];}}int main(){int t;scanf("%d",&t);while(t--){scanf("%s",b);get_next(b);int m=strlen(b);int f=1,ans=0;int n=m;//保证 i 的最大值不变 while(m>0 && f){int len=nex[m];//求出最大的前后缀个数 for(int i=len*2; i<=n-len && f; i++){//求中间是否存在于前后缀相同的个数 if(nex[i]==len){//存在时 ans=len;f=0;break;}}m=nex[m];//返回此时对应的上一个//例如 aabaaa 当前缀是aa时不满足情况;//此时m=nex[m]表示返回到第一个a ,进行接下来的前缀是a 的判断 }printf("%d\n",ans);}return 0;}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- [BZOJ3399]连通块计数
- C++函数重载
- Redis服务器简单操作类及GEO坐标附近位置查询
- Struts2学习第三天:访问web元素
- 项目中遇到的问题
- HDU
- 传智_Springmvc+Mybatis由浅入深全套视频教程-SpringMVC笔记(两天)(2017年8月17日21:30:09)
- 二分图匹配 (模板篇)
- 从招聘网站爬取职位信息小窥IT发展
- bzoj 3944 Sum
- 函数实参的计算顺序
- 进制均值(京东2017秋招真题)
- 链表ALDS1-3-C:Doubly Linked List
- 装逼的网站