Period

Time Limit: 3000MS Memory Limit: 30000K

Total Submissions: 14280 Accepted: 6773

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

POJ2406与这道题一个意思,就是这道题细化了一点。

代码:

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std; char a[1000005];
int next1[1000005]; void cal()
{
int len = strlen(a);
int i,j=-1;
next1[0]=-1;
for(i=0;i<len;)
{
if(j==-1||a[i]==a[j])
{
i++;
j++; next1[i]=j;
}
else
{
j=next1[j];
}
}
} int main()
{
int len,count=1;
while(cin>>len)
{
if(len==0)
break; cin>>a;
cal(); int i;
cout<<"Test case #"<<count++<<endl; for(i=2;i<=len;i++)
{
if(i%(i-next1[i])==0 && i/(i-next1[i])>=2)
cout<<i<<" "<<i/(i-next1[i])<<endl;
} cout<<endl;
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1961:Period的更多相关文章

  1. 【poj 1961】Period(字符串--KMP 模版题)

    题意:给你一个字符串,求这个字符串到第 i 个字符为止的重复子串的个数. 解法:判断重复子串的语句很重要!!if (p && i%(i-p)==0) printf("%d % ...

  2. 【POJ 1961】 Period

    [题目链接] 点击打开链接 [算法] KMP 和POJ2406很像 [代码] #include <algorithm> #include <bitset> #include & ...

  3. KMP POJ 1961 Period

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

  4. Period POJ - 1961

    Period POJ - 1961 时限: 3000MS   内存: 30000KB   64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 For each ...

  5. POJ 1961 2406 (KMP,最小循环节,循环周期)

    关于KMP的最短循环节.循环周期,请戳: http://www.cnblogs.com/chenxiwenruo/p/3546457.html (KMP模板,最小循环节) POJ 2406  Powe ...

  6. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  7. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  8. poj 1961 Period

    Period http://poj.org/problem?id=1961 Time Limit: 3000MS   Memory Limit: 30000K       Description Fo ...

  9. POJ 1961 Period( KMP )*

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

随机推荐

  1. jsp 防止表单多次提交

    1:首先java 后台代码生成一个token,然后保存到jsp 页面的一个隐藏控件并且保存到set session中 */ @RequestMapping("/yuDengJi") ...

  2. 树莓派4B踩坑指南 - (5)设置阿里云的源及解决apt提示依赖

    解决树莓派apt升级/安装提示依赖问题: 注意!!buster是根据系统版本(cat /etc/os-release)来写的,如果是jessie或者stretch要改为buster.参考解决树莓派ap ...

  3. Navicat连接mysql时候出现1251错误代码

    出现1251错误代码 是因为mysql8.0的密码加密方式与之前5.0的不同 如果是字母式的密码 比如root 可能会出现这种情况 1.先通过命令行进入mysql的root账户 Enter passw ...

  4. Color Space 和 Color Range

    颜色有两个属性Color Range和Color Space 有关Color Space的解释可以看下面两个链接: https://www.jianshu.com/p/facdbab5ac20 htt ...

  5. MySQL实现主从复制功能

    环境说明         centos7.3.MySQL5.7 前言         MySQL安装参考之前的文章https://www.jianshu.com/p/452aa99c7476有讲解. ...

  6. A Simple Problem with Integers-POJ3468 区间修改+区间查询

    题意: 给你n个数和2个操作,C操作是将一个区间内的每个数都加上k,Q操作是询问一个区间的和 链接:http://poj.org/problem?id=3468 思路: 线段树区间修改+区间查询 代码 ...

  7. 如何:使用 as 和 is 运算符安全地进行强制转换(C# 编程指南)

    如何:使用 as 和 is 运算符安全地进行强制转换(C# 编程指南) 由于对象是多态的,因此基类类型的变量可以保存派生类型.若要访问派生类型的方法,需要将值强制转换回该派生类型.不过,在这些情况下, ...

  8. github 创建分支

    1.github网站创建 参考:https://www.cnblogs.com/autoXingJY/p/9004724.html 2.命令更新 参考:https://www.cnblogs.com/ ...

  9. Intellij Idea webstorm 激活

    参考: 最新破解 https://www.cnblogs.com/litterCoder/p/12175461.html 推荐 https://mp.weixin.qq.com/s/zxfDAlN8G ...

  10. redis api-list