KMP中的计算next函数中的
两个下标i , j是什么含义
i %( i - j) ==0 && i / (i - j) > 1 应该如何理解找到了循环周期的字符串 ?
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <deque>
#include <queue>
#include <map>
#include <queue>
#include <list>
#include <iomanip>
using namespace std;
///
///
const int INF = 20000000;
#define maxn 1000
#define max(a,b)(a>b?a:b)
///
char S[maxn];
char next[maxn];
int n;
void getnext()
{
int i, j;
i = 0;
j = -1;
next[0] = -1;
while (i < n)
{
if (j == - 1 || S[i] == S[j])
{
i++;
j++;
if(i %( i - j) ==0 && i / (i - j) > 1)
printf("%d %d\n", i, i / (i - j));
next[i] = j;
}
else
j = next[j];
}
}
int main()
{
///
int i, j, t;
t = 1;
while (1)
{
scanf("%d", &n);
if (n == 0)
break;
scanf("%s", S);
printf("Test case #%d\n",t++);
getnext();
printf("\n");
}
///
return 0;
}