POJ 1961:Period
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的更多相关文章
- 【poj 1961】Period(字符串--KMP 模版题)
题意:给你一个字符串,求这个字符串到第 i 个字符为止的重复子串的个数. 解法:判断重复子串的语句很重要!!if (p && i%(i-p)==0) printf("%d % ...
- 【POJ 1961】 Period
[题目链接] 点击打开链接 [算法] KMP 和POJ2406很像 [代码] #include <algorithm> #include <bitset> #include & ...
- KMP POJ 1961 Period
题目传送门 /* 题意:求一个串重复出现(>1)的位置 KMP:这简直和POJ_2406没啥区别 */ /******************************************** ...
- Period POJ - 1961
Period POJ - 1961 时限: 3000MS 内存: 30000KB 64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 For each ...
- POJ 1961 2406 (KMP,最小循环节,循环周期)
关于KMP的最短循环节.循环周期,请戳: http://www.cnblogs.com/chenxiwenruo/p/3546457.html (KMP模板,最小循环节) POJ 2406 Powe ...
- 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 ...
- POJ 3252:Round Numbers
POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...
- poj 1961 Period
Period http://poj.org/problem?id=1961 Time Limit: 3000MS Memory Limit: 30000K Description Fo ...
- POJ 1961 Period( KMP )*
Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...
随机推荐
- [转载]android 显示多选列表对话框setMultiChoiceItems
public class MultiChoiceItemsTest extends Activity implements OnClickListener { private String[] pro ...
- Debug运行项目时报错,connected to the target VM, address: '127.0.0.1:50495', transport: 'socket'
Debug运行项目时报错,无法进入Debug,猜想出错原因可能是未正确关闭IDEA. 解决方法,先直接运行项目,然后停掉项目,再用Debug模式启动,问题解决.
- Day11 - J - Brave Game HDU - 1846
十年前读大学的时候,中国每年都要从国外引进一些电影大片,其中有一部电影就叫<勇敢者的游戏>(英文名称:Zathura),一直到现在,我依然对于电影中的部分电脑特技印象深刻.今天,大家选择上 ...
- Python 基础之生成器
一.生成器表达式 生成器本质是迭代器,允许自定义逻辑的迭代器迭代器和生成器区别:迭代器本身是系统内置的,重写不了.而生成器是用户自定义的,可以重写迭代逻辑生成器可以用来钟方式创建: (1)生成器 ...
- 无线冲方案 WPC Qi v1.2.4 update
参考: 1. Qi标准v1.2.4最新版 2. Qi Baseline Power Profile (BPP) and Extended Power Profile (EPP) Wireless Ch ...
- INI文件,WritePrivateProfileString()和GetPrivateProfileString()函数----转载
INI文件就是扩展名为“ini”的文件.在Windows系统中,INI文件是很多,最重要的就是“System.ini”.“System32.ini”和“Win.ini”.该文件主要存放用户所做的选择以 ...
- docker的概念
Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源.Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 Lin ...
- Python学习第六课——基本数据类型一之tuple and dict
元组 (tuple) tu=(11,22,(123,456),[22,55],) # 一般定义元组的时候最后面加一个, # 元组不能被修改或者删除 v = tu[0] # 也可以根据索引取值 prin ...
- 基于Phoenix对HBase建索引
参考: Phoenix与HBase集成进行数据分析 HBase查询速度慢原因排查 操作1,执行查询,如下: : jdbc:phoenix:node3::/hbase> SELECT * FROM ...
- oracle练习-day01
.基础查询 yearly .条件查询小于的员工 ; ;的员工; ,,); .模糊查询.排序.字符函数,) test .数值函数.,),round(.,),round(.) .,.),trunc(., ...