POJ 1961 Period

时间:2019-11-27
本文章向大家介绍POJ 1961 Period,主要包括POJ 1961 Period使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

POJ 1961 Period

POJ传送门

Description

For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as AK ,that is A concatenated K times, for some string A. Of course, we also want to know the period K.

Input

The input consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S.The second line contains the string S. The input file ends with a line, having the
number zero on it.

Output

For each test case, output "Test case #" and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.

Sample Input

3
aaa
12
aabaabaabaab
0

Sample Output

Test case #1
2 2
3 3

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

一道KMP算法的练手好题。

大体的题目大意是这样的:

题目大意:

如果一个字符串S是由一个字符串T重复K次形成的,则称T是S的循环元。使K最大的字符串T称为S的最小循环元,此时的K称为最大循环次数。

现给一个给定长度为N的字符串S,对S的每一个前缀S[1~i],如果它的最大循环次数大于1,则输出该前缀的最小循环元长度和最大循环次数。

题解:

一道KMP算法的题目,如果对KMP算法还是没有什么深刻的理解或者还没学KMP算法的,请移步我的这篇博客,讲解还算详细:

KMP算法详解

一开始拿到题没什么思路(我还是太菜了)

后来发现,对给出的串\(S\)自匹配求出\(nxt\)数组之后,对于每一个\(i\),一定会有这么一个结论:
\[ S[1\,\,to\,\,nxt[i]]=S[i-nxt[i]+1\,\,to\,\,i] \]
这是通过KMP算法对\(nxt\)数组的定义得来的。

那么,既然这两个东西是相等的,那么在对这个子串进行匹配的时候,这个循环节长度就应该是\(i-nxt[i]\),然后循环次数当然就是\(i/(i-nxt[i])\),当然,前提需要是\(i\%(i-nxt[i])==0\)

代码如下:

#include<cstdio>
using namespace std;
const int maxl=1e6+10;
int n,tot;
char s[maxl];
int nxt[maxl];
int main()
{
    while(~scanf("%d",&n) && n)
    {
        scanf("%s",s+1); 
        tot++;
        printf("Test case #%d\n",tot);
        nxt[1]=0;
        for(int i=2,j=0;i<=n;++i)
        {
            while(s[i]!=s[j+1] && j) 
                j=nxt[j];
            if(s[i]==s[j+1]) 
                j++;
            nxt[i]=j;
            if(nxt[i]!=0 && i%(i-nxt[i])==0) 
                printf("%d %d\n",i,i/(i-nxt[i]));
        }
        printf("\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/fusiwei/p/11945297.html