HUD1358-Period Praying number of characters in the number of cycles

The sample format

Enter
. 3
AAA
12 is
aabaabaabaab
0
---
---
output
the Test Case #. 1
2 2
. 3. 3

Test case #2
2 2
6 2
9 3
12 4
---

0 for launch, launched the previous output looks no line breaks, it can actually add line breaks, so do not cover this, as long as each set of data will be able to put a line break

HDU1358-Period

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

char b[1000005];
int Next[1000005];
int len;
int cnt=1;
void getnext(){
    int p=0;//p是下标
    Next[1]=0;
    for(int i=2;i<=len;i++){
        while(p&&b[i]!=b[p+1]){
            p=Next[p];
        }
        if(b[p+1]==b[i])p++;
        Next[i]=p;
    }
}

int main(){


    int n;
    while(scanf("%d",&n)!=EOF){
        if(n==0)break;
        scanf("%s",b+1);

        len=strlen(b+1);
        getnext();

        //printf("Test case #%d\n",cnt++);
        printf("Test case #%d\n",cnt++);

        for(int i=1;i<=n;i++){
            int t=i-Next[i];//循环节字符串长度
            if(i%t==0&&i/t>1){//整数倍,有循环
                printf("%d %d\n",i,i/t);
            }
        }
        putchar('\n');

    }

    return 0;
}

Guess you like

Origin www.cnblogs.com/Emcikem/p/11345138.html