hdu1358 KMP循环节

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <iomanip>

using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define maxn 1000005
#define MOD 1000000007
#define mem(a , b) memset(a , b , sizeof(a))
#define LL long long
#define ULL long long
const long long INF=0x3fffffff;
char a[maxn];
int Next[maxn];
//ofstream  ofile;
int main()
{
    int cases=1 , m;
    while(scanf("%d",&m) != EOF && m)
    {
        scanf("%s" , a+1);
        mem(Next , 0);
        int num = 0;
        for(int i = 2 ; i <= m ; i ++)
        {
            while(num > 0 && a[num + 1] != a[i]) num = Next[num];
            if(a[num+1] == a[i]) num++;
            Next[i] = num;
        }
        printf("Test case #%d\n",cases++);
        for(int i = 2 ; i <= m ; i ++)
            if(i % (i-Next[i]) == 0 && Next[i] != 0)
                printf("%d %d\n",i,i/(i-Next[i]));
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(hdu1358 KMP循环节)