Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:我用KMP过的代码In Reply To:郁闷啊,忘了把C换成C++,两个CE,有那个同学好心把KMP做的发给我么,只在算法导论上看过不会用.. Posted by:lijinwu at 2010-01-20 19:30:39 #include <stdio.h> #include <string.h> char s[1000010]; int next[1000010]; int len; int main() { int i,j; while(1) { scanf("%s",s); if(s[0]=='.')break; len=strlen(s); i=0;next[0]=-1;j=-1; while(i<len) { if(j==-1||s[i]==s[j]) { ++i; ++j; next[i]=j; }else j=next[j]; } if(len%(len-next[len])==0) { printf("%d\n",len/(len-next[len])); }else printf("1\n"); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator