这个算法简单,但是这两天做了两个kmp的算法,都没做出来,细细的想想,我的kmp一直是我的一块心病,一直没有完全的理解,对于我来说,这个算法真的很难!好吧,你也许会说这个算法很简单啊,next函数是个模板,套上之后匹配就是了,嗯,我同意,简单,但是next函数是怎么求的呢?得出的结果中有什么重要信息么?
我先在这儿存几个网址,以后我再细细的看:http://www.ics.uci.edu/~eppstein/161/960227.html http://www.ics.uci.edu/~eppstein/161/960222.html
这里面的东西全是英文的,听说很不错
开始kmp算法,开始学的时候总是一头的雾水,弄不明白,
Time Limit: 1000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
[Submit] [Go Back] [Status]
Description
Detection of Extraterrestrial |
E.T. Inc. employs Maryanna as alien signal researcher. To identify possible alien signals and background noise, she develops a method to evaluate the signals she has already received. The signal sent by E.T is more likely regularly alternative.
Received signals can be presented by a string of small latin letters 'a' to 'z' whose length isN. For each X between 1 andN inclusive, she wants you to find out the maximum length of the substring which can be written as a concatenation ofX same strings. For clarification, a substring is a consecutive part of the original string.
The first line contains T, the number of test cases (T200). Most of the test cases are relatively small. T lines follow, each contains a string of only small latin letters 'a' - 'z', whose lengthN is less than 1000, without any leading or trailing whitespaces.
For each test case, output a single line, which should begin with the case number counting from 1, followed byN integers. The X-th (1-based) of them should be the maximum length of the substring which can be written as a concatenation ofX same strings. If that substring doesn't exist, output 0 instead. See the sample for more format details.
Hint: For the second sample, the longest substring which can be written as a concatenation of 2 same strings is "noonnoon", "oonnoonn", "onnoonno", "nnoonnoo", any of those has length 8; the longest substring which can be written as a concatenation of 3 same strings is the string itself. As a result, the second integer in the answer is 8 and the third integer in the answer is 12.
2 arisetocrat noonnoonnoon
Case #1: 11 0 0 0 0 0 0 0 0 0 0 Case #2: 12 8 12 0 0 0 0 0 0 0 0 0
Input
Output
Sample Input
Sample Output
Hint
[Submit] [Go Back] [Status]
以下代码出处http://blog.csdn.net/allenjy123/article/details/6629885
/*KMP*/ /*注意:对ans[1]特殊考虑*/ /*AC代码:288ms*/ #include <iostream> #include <cstdio> #include <memory.h> #include <algorithm> #include <cstring> #define MAXN 1005 #define max(a,b) (a>b?a:b) using namespace std; int cas,len; int ans[MAXN],next[MAXN]; char s[MAXN]; void get_next(char s[]) { int i=1,t,lens=strlen(s+1); next[0]=-1; while(i<=lens) { t=next[i-1]; while((t+1)&&s[t+1]!=s[i]) t=next[t]; next[i]=t+1; i++; } } void Solve() { int i,j,k; memset(ans,0,sizeof(ans)); ans[1]=len; char temp[MAXN]; for(i=0;i<len;i++) { strcpy(temp+1,s+i); get_next(temp); int len=strlen(temp+1); for(j=len;j>=1;j--) { int x=j-next[j]; if(j%x==0) { int w=j/x; for(k=w;k>=1;k--)//注意这里要更新多组答案 ans[k]=max(ans[k],j-(w%k)*x); } } } } int main() { int i,T; cas=1; scanf("%d",&T); while(T--) { scanf("%s",s); len=strlen(s); Solve(); printf("Case #%d:",cas++); for(i=1;i<=len;i++) printf(" %d",ans[i]); printf("\n"); } return 0; } /* asasasa Case #27: 7 4 6 0 0 0 0 */