标签:aaa turn algorithm input esc incr ref possible order
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 24399 | Accepted: 12731 |
Description
Input
Output
Sample Input
ababcababababcabab aaaaa
Sample Output
2 4 9 18 1 2 3 4 5
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int maxn=400010; 6 7 int nex[maxn]; 8 int extend[maxn]; 9 char buf[maxn]; 10 11 void Get_Next(const char T[]) 12 { 13 int len=strlen(T); 14 int j=0; 15 nex[0]=len; 16 while(j+1 < len && T[j+1] == T[j]) 17 j++; 18 nex[1]=j; 19 int k=1; 20 for(int i=2;i < len;++i) 21 { 22 int P=nex[k]+k-1; 23 int L=nex[i-k]; 24 if(i+L <= P) 25 nex[i]=L; 26 else 27 { 28 j=max(0,P-i+1); 29 while(i+j < len && T[i+j] == T[j]) 30 j++; 31 nex[i]=j; 32 k=i; 33 } 34 } 35 } 36 void Get_Extend(const char S[],const char T[]) 37 { 38 Get_Next(T); 39 int n=strlen(S); 40 int m=strlen(T); 41 int j=0; 42 while(j < n && j < m && S[j] == T[j]) 43 j++; 44 extend[0]=j; 45 int k=0; 46 for(int i=1;i < n;++i) 47 { 48 int P=extend[k]+k-1; 49 int L=nex[i-k]; 50 if(i+L <= P) 51 extend[i]=L; 52 else 53 { 54 j=max(0,P-i+1); 55 while(i+j < n && j < m && S[i+j] == T[j]) 56 j++; 57 extend[i]=j; 58 k=i; 59 } 60 } 61 } 62 63 int main() 64 { 65 while(~scanf("%s",buf)) 66 { 67 Get_Extend(buf,buf); 68 int len=strlen(buf); 69 for(int i=len-1;i > 0;--i) 70 if(i+extend[i] == len) 71 printf("%d ",extend[i]); 72 printf("%d\n",len); 73 } 74 return 0; 75 }
标签:aaa turn algorithm input esc incr ref possible order
原文地址:https://www.cnblogs.com/violet-acmer/p/9652216.html