poj 1961 Period(KMP训练指南例题)
Period
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 Sample Output Test case #1 Source |
题意:
给你一个字符串。叫你依次输出输出该串长度为i的前缀(如果该前缀可以由重复子串得到)的长度。产生该前缀所需的最大重复次数。
思路:
见训练指南。
详细见代码:
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=1000100;
int f[maxn];
char txt[maxn];
void getf(char *p)
{
int i,j,m=strlen(p);
f[0]=f[1]=0;
for(i=1;i<m;i++)
{
j=f[i];
while(j&&p[i]!=p[j])
j=f[j];
f[i+1]=p[i]==p[j]?j+1:0;
}
}
int main()
{
int len,i,t,cas=1; while(scanf("%d",&len),len)
{
scanf("%s",txt);
getf(txt);
printf("Test case #%d\n",cas++);
for(i=2;i<=len;i++)
{
t=i-f[i];
if(f[i]&&i%t==0)
printf("%d %d\n",i,i/t);
}
printf("\n");
}
return 0;
}
poj 1961 Period(KMP训练指南例题)的更多相关文章
- POJ 1961 Period( KMP )*
Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...
- POJ 1961 Period KMP算法next数组的应用
题目: http://poj.org/problem?id=1961 很好的题,但是不容易理解. 因为当kmp失配时,i = next[i],所以错位部分就是i - next[i],当s[0]...s ...
- POJ 1961 Period KMP算法之next数组的应用
题意:给一个长度为n的字符串,如果它长度为l(2 <= l <= n)的前缀部分是由一些相同的字符串相接而成,输出前缀的长度l和长度为l时字符串重复的最大次数. 例如字符串为: aaaba ...
- KMP POJ 1961 Period
题目传送门 /* 题意:求一个串重复出现(>1)的位置 KMP:这简直和POJ_2406没啥区别 */ /******************************************** ...
- POJ 1961 Period(KMP)
http://poj.org/problem?id=1961 题意 :给你一个字符串,让你输出到第几个字符时,循环结的个数. 思路 :这个题和2409差不多,稍微修改一下,加一个循环就行了,用的也是K ...
- (简单) POJ 1961 Period,扩展KMP。
Description For each prefix of a given string S with N characters (each character has an ASCII code ...
- 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中的出现位置或者是次数 这个出现的次数是可 ...
- LA 3026 && POJ 1961 Period (KMP算法)
题意:给定一个长度为n字符串s,求它每个前缀的最短循环节.也就是对于每个i(2<=i<=n),求一个最大整数k>1(如果存在),使得s的前i个字符组成的前缀是某个字符串重复得k次得到 ...
- poj 1961 Period
Period http://poj.org/problem?id=1961 Time Limit: 3000MS Memory Limit: 30000K Description Fo ...
随机推荐
- centos之jdk安装
1.环境说明 系统:centos6.7, 2.6.32-573.el6.x86_64; jdk:jdk1.7.0_80 2.下载jdk并上传 下载jdk-7u80-linux-x64.tar ...
- ArcGIS Engine DEM拉伸渲染
从符号库中取出渲染使用的色带对象IColorRamp(也可以自己定义色带内容) <pre name="code" class="csharp">IS ...
- ViewPager实现页卡的最新方法--简洁的TabLayout(谷歌支持包)
效果图: 添加依赖包: compile ‘com.android.support:design:‘ 布局文件: <?xml version="1.0" encoding=&q ...
- 单机Oracle+asm(11.2.0.3.0) Patch Set Update(11.2.0.3.7 )
之前写过一篇关于PSU升级的案例,参考如下: http://blog.csdn.net/jyjxs/article/details/8983880 但是,感觉有些地方理解的不是很透彻明白,照猫画虎的比 ...
- 字符编码和python使用encode,decode转换utf-8, gbk, gb2312
ASCII码 标准ASCII码使用7位二进制数表示大写或小写字母,数字0到9标点符号以及在美式英语中使用的特殊控制字符. 在标准ASCII码中,最高位(b7)用作奇偶校验位,所谓奇偶校验,是指在代码传 ...
- 【每日一摩斯】-Troubleshooting: High CPU Utilization (164768.1) - 系列6
如果问题是一个正运行的缓慢的查询SQL,那么就应该对该查询进行调优,避免它耗费过高的CPU资源.如果它做了许多的hash连接和全表扫描,那么就应该添加索引以提高效率. 下面的文章可以帮助判断查询的问题 ...
- mac 版本号控制工具SmartSVN7.5.4(破解版)
SmartSVN7.5.4和破解工具,下载地址:http://download.csdn.net/detail/pearlhuzhu/7407319 操作步骤: 1.在MAC上选中smartsvn-m ...
- SAN实现
Linux 上主要有三个 iSCSI Target(基于internet scsi协议的target) 实现: Linux SCSI Target – STGT / tgt Linux-IO Targ ...
- C#程序设计六大原则记录
本文目的在于记录,方便以后的回顾 http://www.uml.org.cn/sjms/201211023.asp 设计模式六大原则(1):单一职责原则 定义:不要存在多于一个导致类变更的原因.通俗的 ...
- oracle超过最大游标数异常分析(转贴)
问题描述 Oracle 使用 OPEN_CURSORS 参数指定一个会话一次最多可以打开的游标的数量.超过此数量时,Oracle 将报告 ORA-01000 错误.当此错误传播到 WebLogic S ...