题意: 求所有可以 表示成 (S)^K的前缀 输出前缀位置 和 k的最大值
代码:
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#define sf scanf
#define pf printf
using namespace std;
const int maxn = 1000000 + 5;
char str[maxn];
int next[maxn];
int getNext(char* p,int* next){
int m = strlen(p);
next[0] = 0;
next[1] = 0;
for(int i = 1;i < m;++i){
int j = next[i];
while(j && p[i] != p[j]){
j = next[j];
}
next[i + 1] = p[i] == p[j] ? j + 1 : 0;
}
return 0;
}
int main(){
int n,ca = 0;
while( sf("%d",&n) && n ){
sf("%s",str);getNext(str,next);
int ans = 0;
pf("Test case #%d\n",++ca);
for(int i = 1;i <= n;++i){
if(next[i] && i % (i - next[i]) == 0){
pf("%d %d\n",i,i / (i - next[i]) );
}
}
pf("\n");
}
return 0;
}

本文介绍了一种用于字符串匹配的高效算法,并通过一个具体的题目来演示如何实现和使用该算法。通过对给定字符串进行预处理得到 next 数组,进而找出所有可以表示为特定形式 (S)^K 的子串及其对应的 K 值。
648

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



