Period

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10419    Accepted Submission(s): 4940

Problem 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 file 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

 

简单题,既然next[I]表示LBorderi,那么该串是重复串,首先要符合他是个周期串,即next[i]*2>i,其次如果其为重复串,那么除去最大匹后缀后的部分和除去最大匹配前缀的部分应相同,并且他们的长度整除i。事实上前一个条件包含在后一个条件里,因为能整除说明他们皆为该重复串的一个基串,那么他们必相同(有点绕,你自己动手证明下,从周期串的角度)。因此做法就很显而易见了。

#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
#define LL long long
#define mod 1000000007
#define INF 0x3f3f3f3f
#define next nexted
using namespace std;
const int N=1e6+;
char s[N];
int next[N];
int n,T;
void getnext(int n,char *s)
{
next[]=next[]=;
int j=;
int len=n;
for(int i=;i<len;i++)
{
while(j && s[i]!=s[j]) j=next[j];
if(s[j]==s[i]) j++;
next[i+]=j;
}
return ;
}
int main()
{
T=;
while(scanf("%d",&n)== && n)
{
scanf("%s",s);
getnext(n,s);
printf("Test case #%d\n",++T);
for(int i=;i<=n;i++)
{
if(next[i]*>=i)
{
if(i%(i-next[i])==)
printf("%d %d\n",i,i/(i-next[i]));
}
}
printf("\n");
}
return ;
}

poj 1348 Period(KMP)的更多相关文章

  1. POJ 1961 Period( KMP )*

    Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...

  2. POJ 1961 Period KMP算法next数组的应用

    题目: http://poj.org/problem?id=1961 很好的题,但是不容易理解. 因为当kmp失配时,i = next[i],所以错位部分就是i - next[i],当s[0]...s ...

  3. POJ 1961 Period KMP算法之next数组的应用

    题意:给一个长度为n的字符串,如果它长度为l(2 <= l <= n)的前缀部分是由一些相同的字符串相接而成,输出前缀的长度l和长度为l时字符串重复的最大次数. 例如字符串为: aaaba ...

  4. KMP POJ 1961 Period

    题目传送门 /* 题意:求一个串重复出现(>1)的位置 KMP:这简直和POJ_2406没啥区别 */ /******************************************** ...

  5. KMP——POJ-3461 Oulipo && POJ-2752 Seek the Name, Seek the Fame && POJ-2406 Power Strings && POJ—1961 Period

    首先先讲一下KMP算法作用: KMP就是来求在给出的一串字符(我们把它放在str字符数组里面)中求另外一个比str数组短的字符数组(我们叫它为ptr)在str中的出现位置或者是次数 这个出现的次数是可 ...

  6. POJ 1961 Period(KMP)

    http://poj.org/problem?id=1961 题意 :给你一个字符串,让你输出到第几个字符时,循环结的个数. 思路 :这个题和2409差不多,稍微修改一下,加一个循环就行了,用的也是K ...

  7. (简单) POJ 1961 Period,扩展KMP。

    Description For each prefix of a given string S with N characters (each character has an ASCII code ...

  8. poj 1961 Period(KMP训练指南例题)

    Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 11356   Accepted: 5279 Descripti ...

  9. LA 3026 && POJ 1961 Period (KMP算法)

    题意:给定一个长度为n字符串s,求它每个前缀的最短循环节.也就是对于每个i(2<=i<=n),求一个最大整数k>1(如果存在),使得s的前i个字符组成的前缀是某个字符串重复得k次得到 ...

随机推荐

  1. 【BZOJ】1202: [HNOI2005]狡猾的商人

    [题意]w组数据,给定n和m,给出m段区间[s,t](1<=s<=t<=n)的数字和,求是否矛盾.n<100,m<1000,w<100. [算法]带权并查集 [题解 ...

  2. WP8.1 Windows Phone 8.1开发:何如定义Pivot头部样式、定义Pivot头部颜色

    Windows Phone 8.1 ,如何自定义Pivot头部样式?用Pivot控件完成这样的效果. 网上找了好久,只找到了windows phone 8的解决方案. 终于一个大神给支了招,我觉得我有 ...

  3. 大聊Python----Select解析

    1.首先列一下,sellect.poll.epoll三者的区别 select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当sele ...

  4. web-project 故障修复功能 传递所有的event_id数据到后台

    <script language=javascript> function IdentifyRepair(event_id) { var url; url = "/View/fa ...

  5. 第一章:读取文件一行IO::File

    #!c:\\perl\\bin\\perl.exe use IO::File; #读取一行 my $fd = IO::File->new('perl.txt'); my $one_line = ...

  6. Python脚本 - 查询磁盘的读写次数信息

    测试系统为:Centos 6.7 Python版本为: 3.6.4 脚本功能:查看指定磁盘的读写及时间等相关信息 #!/usr/bin/env python3 from collections imp ...

  7. go环境变量及build文件

    package main /* windows go环境设置: # 参考:https://blog.csdn.net/quicmous/article/details/80360126 GOROOT ...

  8. 【Python学习笔记】Coursera课程《Using Python to Access Web Data 》 密歇根大学 Charles Severance——Week2 Regular Expressions课堂笔记

    Coursera课程<Using Python to Access Web Data > 密歇根大学 Charles Severance Week2 Regular Expressions ...

  9. Linux内核基础--事件通知链(notifier chain)【转】

    转自:http://blog.csdn.net/wuhzossibility/article/details/8079025 内核通知链 1.1. 概述 Linux内核中各个子系统相互依赖,当其中某个 ...

  10. sunos kernel src

    https://github.com/eocallaghan/AuroraUX-SunOS https://github.com/zoyanhui/coroutine-libtask https:// ...