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 ...
随机推荐
- Linux centos7iptables filter表案例、iptables nat表应用
一.iptables filter表案例 vim /usr/local/sbin/iptables.sh 加入如下内容 #! /bin/bash ipt="/usr/sbin/iptable ...
- FFmpeg——AVCodec,AVCodecContext,AVCodecParameters 辨析
先贴上雷神的一张FFmpeg关键结构体之间的关系图: 再看雷神的分析: 每个AVStream存储一个视频/音频流的相关数据: 每个AVStream对应一个AVCodecContext,存储该视频/音频 ...
- SpringBoot图文教程3—「‘初恋’情结」集成Jsp
有天上飞的概念,就要有落地的实现 概念+代码实现是本文的特点,教程将涵盖完整的图文教程,代码案例 文章结尾配套自测面试题,学完技术自我测试更扎实 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例 ...
- Github 第三方授权登录教程
Github 第三方授权登录教程 ####大致流程图 ####1.首先注册一个github帐号,Applications>Developer applications>Register a ...
- 第2节 storm实时看板案例:12、实时看板综合案例代码完善;13、今日课程总结
详见代码 将任务提交到集群上面去运行 apache-storm-1.1.1/bin/storm jar cn.itcast.storm.kafkaAndStorm.KafkTopology kafka ...
- UOJ192 最强跳蚤
题目链接 problem 给出一个n个点带边权的树,问有多少对\((u,v)\)满足\(u\)到\(v\)路径上边权的乘积为完全平方数. \(n\le 10^5,w\le 10^8\) solutio ...
- python-python基础4
本章内容: 装饰器 生成器 迭代器 json & pickle 模块 软件目录结构规范 一.装饰器 装饰器 在不改动函数代码的基础上无限制扩展函数功能的一种机制,本质上讲,装饰器是一个返回函数 ...
- 蓝牙 BLE 协议学习: 000-有关概念介绍
背景 在学校内就用过蓝牙技术参加过比赛(并拿了奖):而蓝牙作为物联网中比较常见的协议,有必要进行深入的学习.此后的文章会以 ble(v4.0) 进行学习. 介绍 蓝牙技术最初由电信巨头爱立信公司于 1 ...
- P1070 结绳
P1070 结绳 转跳点:
- springboot2.1以javabean整合rabbitmq及自动注入rabbitmqTemplate为空问题
springboot集成rabbitmq之前也写过,这次再来个总结,总体来讲比较简单 主要就是配置属性文件,将属性以javabean的形式注入,配置工厂,对象等原来以xml<bean>形式 ...