hdu 4763 Theme Section(exkmp)
来源:互联网 发布:怎么登录淘宝图片空间 编辑:程序博客网 时间:2024/06/03 11:16
Theme Section
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3990 Accepted Submission(s): 1880
Problem Description
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?
Input
The integer N in the first line denotes the total number of songs in the festival. Each of the following N lines consists of one string, indicating the notes of the i-th (1 <= i <= N) song. The length of the string will not exceed 10^6.
Output
There will be N lines in the output, where the i-th line denotes the maximum possible length of the theme section of the i-th song.
Sample Input
5
xy
abc
aaa
aaaaba
aaxoaaaaa
Sample Output
0
0
1
1
2
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int N= 1e6+10;char s1[N], s2[N];int extend[N], nex[N];void getnext(char a[],int len){ int k=0, i=1; nex[0]=len; while(k+1<len&&a[k]==a[k+1])++k; nex[1]=k; k=1; while(++i<len) { int p=k+nex[k]-1; nex[i]=min(nex[i-k],max(p-i+1,0)); while(i+nex[i]<len&&a[nex[i]]==a[i+nex[i]]) ++nex[i]; if(i+nex[i]>k+nex[k]) k=i; } return ;}void exkmp(char a[],char b[],int len,int len2){ getnext(a,len); int k=0, i=0; while(k<len2&&a[k]==b[k]) ++k; extend[0]=k; k=0; while(++i<len2) { int p=k+extend[k]-1; extend[i]=min(nex[i-k],max(p-i+1,0)); while(i+extend[i]<len2&&a[extend[i]]==b[i+extend[i]]) ++extend[i]; if(i+extend[i]>k+extend[k]) k=i; } return ;}char v[1000];int a[N];int main(){ int t, ncase=1; scanf("%d", &t); while(t--) { scanf("%s",s1); int len=strlen(s1); if(len<3) { printf("0\n"); continue; } getnext(s1,len); int k=1; a[0]=0; for(int i=len-len/3;i<len;i++) { if(nex[i]==len-i) a[k++]=nex[i]; } a[k]=N+10; if(k==1) { printf("0\n"); continue; } k++; sort(a,a+k); int m=0, ans=0; for(int i=1;i<len-1;i++) { m=min(i,nex[i]); if(m==0) continue; int pos=lower_bound(a,a+k,m)-a; for(int j=pos;j>=0;j--) { if(a[j]<=m&&i+a[j]-1<len-a[j]) { ans=max(ans,a[j]); break; } } } printf("%d\n",ans); } return 0;}
- hdu 4763 Theme Section(exkmp)
- HDU 4763 Theme Section (kmp | exkmp)
- Hdu 4763 Theme Section (KMP+暴力 或 exKMP)
- HDU 4763 Theme Section
- hdu 4763 Theme Section
- hdu 4763 Theme Section
- HDU-4763 Theme Section
- HDU 4763 Theme Section
- Hdu-4763 Theme Section
- hdu 4763 Theme Section
- hdu 4763 Theme Section
- HDU 4763Theme Section
- hdu 4763 Theme Section
- hdu 4763 Theme Section
- hdu 4763Theme Section
- KMP-hdu-4763-Theme Section
- hdu 4763 Theme Section (双哈希)
- hdu 4763 Theme Section(KMP)
- FastDFS 多tracker server报错
- Python篇pip命令篇(未完待续....)
- Activiti model生成png图片(解决中文不显示问题)
- Android Makefile 错误 No rules to make target *.c , needed by ... *.o
- 20.IDA-修改二进制文件、显示修改点
- hdu 4763 Theme Section(exkmp)
- scala中操作符可以分为三类
- UVA
- LintCode:M-Sort List
- [Language]Python中的字节串
- 11.IDA-this指针
- pythonchallenge
- 阿里大鱼短信
- 训练总结(总)