poj 1961(KMP)

poj 1961
kmp求循环节是$n-f[n]$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define ms(i,j) memset(i, j, sizeof i);
const int MAXN = 1000000 + 5;
int n, kase = 0;
char s[MAXN];
int f[MAXN];
void init()
{
scanf("%s", s);
}
void solve()
{
f[0] = f[1] = 0;
for (int i=1;i<n;i++)
{
int j = f[i];
while (j && s[i]!=s[j]) j = f[j];
f[i+1] = (s[i]==s[j]) ? (j+1) : (0);
}
printf("Test case #%d\n", kase);
for (int i=1;i<=n;i++)
{
int xh = i - f[i];
if (i != xh && i % xh == 0)
printf("%d %d\n", i, i/xh);
}
printf("\n");
}
int main()
{
while (scanf("%d", &n)==1&&n)
{
kase++;
init();
solve();
}
return 0;
}

------ 本文结束 ------