多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > LA 3026(Period-MP算法)[Template:KMP]

LA 3026(Period-MP算法)[Template:KMP]

來源:程序員人生   發布時間:2015-04-10 07:53:10 閱讀次數:3321次

3026 - Period

Time limit: 3.000 seconds 

For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 ≤ i ≤ N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as AK , that is A concatenated K times, for some string A. Of course, we also want to know the period K.

Input 

The input file consists of several test cases. Each test case consists of two lines. The first one contains N (2 ≤ N ≤ 1 000 000) the size of the string S. The second line contains the string S. The input file ends with a line, having the number zero on it.

Output 

For each test case, output 'Test case #' and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.

Sample Input 

3
aaa
12
aabaabaabaab
0

Sample Output 

Test case #1
2 2
3 3

Test case #2
2 2
6 2
9 3
12 4

本題用了MP算法

明顯i能和f[i]匹配,那末字符串恰好可以連著等下去






#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cctype> #include<ctime> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;i>=0;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define Forpiter(x) for(int &p=iter[x];p;p=next[p]) #define Lson (x<<1) #define Rson ((x<<1)+1) #define MEM(a) memset(a,0,sizeof(a)); #define MEMI(a) memset(a,127,sizeof(a)); #define MEMi(a) memset(a,128,sizeof(a)); #define INF (2139062143) #define F (100000007) #define MAXN (1000000+10) typedef long long ll; ll mul(ll a,ll b){return (a*b)%F;} ll add(ll a,ll b){return (a+b)%F;} ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;} void upd(ll &a,ll b){a=(a%F+b%F)%F;} // kmp class KMP { public: int f2[MAXN]; //字符串從0開始,但是f[i]表示匹配第i個字符,前面留1個 f[0]--a-->f[1]--...這樣的 char T2[MAXN],P2[MAXN]; //T is long,P is model str void mem(){MEM(f2) MEM(T2) MEM(P2) } int getFail(char *P=0,int* f=0) { if (P==0) P=P2;if (f==0) f=f2; int m=strlen(P); f[0]=f[1]=0; For(i,m⑴) { int j=f[i]; while(j&&P[i]!=P[j]) j=f[j]; f[i+1]= P[i] == P[j] ? j+1 : 0; } } int find(char* T=0,char* P=0,int* f=0) { if (T==0) T=T2;if (P==0) P=P2;if (f==0) f=f2; int n=strlen(T),m=strlen(P); getFail(P,f); int j=0; Rep(i,n) { while(j&&T[i]!=P[j]) j=f[j]; if (T[i]==P[j]) j++; if (j==m) return i-m+1; } } }S; int n; int main() { // freopen("la3026.in","r",stdin); // freopen(".out","w",stdout); int tt=0; while(scanf("%d%s",&n,S.P2)==2) { printf("Test case #%d ",++tt); S.getFail(S.P2,S.f2); Fork(i,2,n) { if (i%(i-S.f2[i])==0&&S.f2[i]) printf("%d %d ",i,i/(i-S.f2[i])); } putchar(' '); S.mem(); } return 0; }





生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 欧美日韩亚洲综合久久久 | 久久国内精品 | 自拍视频一区二区 | 日本在线观看www免费 | 精彩视频在线观看 | 久久免费视频1 | 日本一区免费看 | 国产亚洲精品国产福利在线观看 | 亚洲一区亚洲二区亚洲三区 | 国产一级一片免费播放视频 | 最近最新中文字幕大全2019免费视频 | 久久艹免费视频 | 精品久久久久久国产91 | 国产成人福利美女观看视频 | 国产永久免费爽视频在线 | 亚洲黄色高清 | 国产精品任我爽爆在线播放66 | 欧美亚洲另类久久综合 | 国产一区二区免费视频 | uuzyz噜噜资源站 | 黄色网址在线免费 | 琪琪五月天| 亚洲成人影院在线观看 | 最新亚洲人成网站在线影院 | 最近中文字幕mv免费高清视频免费 | 另类 校园 春色 都市 亚洲 | 国产亚洲视频在线观看 | 亚洲天堂2016 | www.黄色网址.com | 国产免费v片在线看 | 波多野结衣中文一区 | 国产日韩欧美亚洲综合在线 | 请看一下欧美一级毛片 | 精品一区二区三区视频在线观看免 | 成人eeuss影院在线观看 | 午夜亚洲国产理论秋霞 | 国内精品视频九九九九 | 欧美综合视频在线 | 国产亚洲欧美日韩在线一区 | 国产高清在线视频一区二区三区 | 日本午夜三级 |