(POJ)1961 - Period 【KMP】

本文详细解析了一道关于字符串周期性的算法题目,介绍了如何利用KMP算法或其next数组来找出字符串的所有重复前缀子串及其最大重复次数,提供两种实现代码供读者参考。
Period
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 17439 Accepted: 8404

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 A K ,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

Source

Southeastern Europe 2004


题意:告诉你字符串的长度和字符串本身,要求对于每个字符串。输出所有重复前缀子串(连续出现次数>1),最后停留的索引位置,以及最大重复的次数。以“aabaabaabaab”为例子,索引停留在2时,存在“a”重复2次;3、4、5都没有前缀重复;索引停留在6时,存在“aab”重复2次;索引停留在“9”时,存在“aab”重复3次;索引停在12时,存在“aab”重复4次。


分析:这个题目容易让人联想到KMP算法。首先回忆一下next数组存放的是什么(当前位置最大前缀后缀匹配长度),假设next位置为i,那么next[i]表示将模式串向右移动这么多位置,在第i个位置的字母依旧可以匹配他本身。这道题不存在模式串和原始串,都是自身之内的比较。那么假设我们把“aabaabaabaab”的next数组算出来了,假设在位置i并且有连续的前缀情况下(这个是前提)——那么i-next[i]就是这个最大前缀的最小重复长度。当i=9时,next[i]=6(“aabaab”),也就是将这个字符串右移动6个字母位,第9位依旧可以匹配上,i-next[i]=3(“aab”),重复前缀为i/(i-next[i])=3。

还不理解的话,请记住,我们在求next的过程中就保证了存在重复前缀是i%(i-next[i])==0的充要条件。


#include <cstdio>  
#include <cstring>  
#include <algorithm>  
#include <iostream> 

using namespace std;

const int MAXN=1000000+10;
char s[MAXN];
int nex[MAXN];

void preKMP(){
    int p=0,q=-1; nex[0]=-1;
    while(s[p]){
        while(q!=-1 && s[q]!=s[p]) q=nex[q];
        nex[++p]=++q;
    }
}

void KMP(){
    preKMP();
    for(int i=2;s[i-1];i++){
        int t=i-nex[i];
        if(i%t==0 && i/t>1) printf("%d %d\n",i,i/t);
    }
}
int main()
{
    int n,cnt=0;
    while(scanf("%d",&n)&&n){
        scanf("%s",s);
        printf("Test case #%d\n",++cnt);
        KMP();
        printf("\n");
    }
    return 0;
}

还有不使用KMP算法的代码: 传送门

把这个题进行简化,一个长度为L的串,按照这题要输出在i[1...L]位置时,前缀循环节最大重复次数。

就把一个问题变成了L个子问题,从头开始找循环节(无需循环到末尾)也很好解决。

拥有巧妙的做题思路是多么的重要啊!

#include<iostream>  
#include<cstring>  
#include<cstdio>  
#include<string>  
#include<algorithm>  
using namespace std;  
#define N 1000010  
  
char s[N];  
int nextval[N];  
int len;  
  
void getnext(const char *s)  
{  
    int i = 0, j = -1;  
    nextval[0] = -1;  
    while(i != len)  
    {  
        if(j == -1 || s[i] == s[j])  
            nextval[++i] = ++j;  
        else  
            j = nextval[j];  
    }  
}  
  
int main()  
{  
    int T = 1;  
    int length, add;  
    while(scanf("%d", &len) && len)  
    {  
        scanf("%s", s);  
        getnext(s);  
        printf("Test case #%d\n", T++);  
        for(int i = 1; i <= len; ++i)  
        {  
            length = i - nextval[i]; //循环节的长度  
            if(i != length && i % length == 0) //如果有多个循环  
                printf("%d %d\n", i, i / length);  
        }  
        printf("\n");  
    }  
    return 0;  
}  


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值