标签:kmp
3 aaa 12 aabaabaabaab 0
Test case #1 2 2 3 3 Test case #2 2 2 6 2 9 3 12 4
/*************************************************************************
> File Name: hdu1358.cpp
> Author: ALex
> Mail: 405045132@qq.com
> Created Time: 2015年01月05日 星期一 09时15分43秒
************************************************************************/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1000010;
int next[N];
char str[N];
void get_next()
{
int len = strlen(str);
next[0] = -1;
int j = 0;
int k = -1;
while (j < len)
{
if (k == -1 || str[j] == str[k])
{
next[++j] = ++k;
}
else
{
k = next[k];
}
}
}
int main()
{
int n;
int icase = 1;
while (~scanf("%d", &n), n)
{
scanf("%s", str);
get_next();
printf("Test case #%d\n", icase++);
for (int i = 0; i < n; ++i)
{
if ((i + 1) % (i + 1 - next[i + 1]) == 0)
{
int cnt = (i + 1) / (i + 1 - next[i + 1]);
if (cnt != 1)
{
printf("%d %d\n", i + 1, cnt);
}
}
}
printf("\n");
}
return 0;
}标签:kmp
原文地址:http://blog.csdn.net/guard_mine/article/details/42419085