题意:问串S 最多能表示成多少个串的重复 输出重复的最大值
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 1000000 + 5;
int NEXT[maxn];
char str[maxn];
int main(){
while( scanf("%s",str) && str[0] != '.' ){
int n = strlen(str);
NEXT[0] = NEXT[1] = 0;
for(int i = 1;i < n;++i){
int j = NEXT[i];
while( j && str[i] != str[j] ) j = NEXT[j];
NEXT[i + 1] = str[i] == str[j] ? j + 1 : 0;
}
int ans;
if(NEXT[n] == 0){
ans = 1;
}
else{
if(n % (n - NEXT[n]) == 0){
ans = n / (n - NEXT[n]);
}
else ans = 1;
}
printf("%d\n",ans);
}
return 0;
}

本文介绍了一种通过构建NEXT数组来解决字符串S最多能由多少个重复子串组成的问题的方法。利用模式匹配技术,该算法能在O(n)的时间复杂度内找到最长的重复子串,并据此确定最大重复次数。
625

被折叠的 条评论
为什么被折叠?



