作者在 2008-03-27 17:08:23 发布以下内容
用到了KMP的一些思想,主要就是求next[]函数
Problem: 2406 | User: keloy | |
Memory: 28648K | Time: 282MS | |
Language: C++ | Result: Accepted |
- Source Code
#include <iostream> #include <string> using namespace std; string t; int Getnext() { int n=t.size(); int *next=new int[n+1]; int i=0,j=-1; next[0]=-1; while(i<n) { if(j==-1) { i++; j++; next[i]=j; } else if(t[i]==t[j]) { i++; j++; next[i]=j; } else j=next[j]; } //cout<<i-j<<endl; return i-j; } int main() { while(cin>>t&&t[0]!='.') { int ans; //没用ans的时候很wa了两次 int b; //但是用不用好像都没关系 b=Getnext(); if(t.size()%b==0) ans=t.size()/b; else ans=1; cout<<ans<<endl; t.empty(); } return 0; }