题意:给定一个串,求此串可由最多几个子串连接而成. 思路:套用模板next函数,若该串可由某子串连接而成,len(s)-next[len] 为子串长度(未能证明,自己写了几个例子,感觉是对的,相当不严谨) 代码: #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAX = 1000009; int next[MAX]; char s[MAX]; int len; void next1() { int i=0, j=-1; next[0] = -1; while(i<len) { if(j==-1 || s[i]==s[j]) { ++i; ++j; next[i] = j; } else j = next[j]; } } int main(void) { while(scanf("%s",s)!=EOF) { if(strcmp(s,".")==0) break; len = strlen(s); next1(); int ans = 1; if(len%(len-next[len])==0) ans = len/(len-next[len]); printf("%d/n",ans); } return 0; }