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 |
这道题让我背下了kmp算法,在这里再默写一遍#include <iostream> #include <string> #include <memory.h> using namespace std; int mnext[1000001]; void getNext(string str) { int i,j; i=0; j=-1; mnext[0]=-1; while(i<str.size()) { if(j==-1||str[i]==str[j]) { i++; j++; mnext[i]=j; } else j=mnext[j]; } } int main() { int len; cin>>len; int num=0; while(len!=0) { memset(mnext,0,sizeof(mnext)); string str; cin>>str; getNext(str); cout<<"Test case #"<<++num<<endl; for(int i=2;i<=str.size();i++) { int len=i-mnext[i]; if(i%len==0&&i/len>1) cout<<i<<" "<<i/len<<endl; } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator