(POJ)1961 - Period 【KMP】

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;  
}  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值