C++ 代码
#include<cstdio>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int fun(const string &str){
for (int i = 1; i <=str.length(); ++i) {
string tmp=str.substr(0,i);
int pre=0;
int offindex = str.find(tmp, 0);
pre=offindex;
while (offindex != string::npos)
{
if(offindex+tmp.length()==str.length()){
return str.length()/tmp.length();
}
offindex = offindex + 1;
offindex = str.find(tmp, offindex);
if(offindex-pre!=tmp.length())
break;
pre=offindex;
}
}
return str.length();
}
int main(){
string a;
while((cin>>a)&&a!="."){
cout<<fun(a)<<endl;
}
return 0;
}
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度分析:blablabla
C++ 代码
blablabla