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 ...
随机推荐
- 关于scala环境配置详解
首先从官网下载适合自身电脑配置的scala安装包.scala下载官网网址:http://www.scala-lang.org/download/ 同时scala还有自己集成好的IDE,例如eclips ...
- [原创]使用GCC创建 Windows NT 下的内核DLL
原文链接:使用GCC创建 Windows NT 下的内核DLL 在温习<<Windows 2000 Driving>>分层驱动程序一章的时候,看到了关于紧耦合驱动连接方式,这种 ...
- JavaScript和php常用语法——切割字符串
在面向Web的应用中,前台和后台通信非常常用的一种格式就是字符串,所以,在通信中,我们不可避免的就需要进行字符串的拼切. 在js代码中,当我们传递一个字符串到后台代码时,我们在后台需要对字符串进行切割 ...
- java中输出流OutputStream 类应用实例(转)
OutputStream类该类是字节输出流的抽象类,定义了输出流的各种操作方法.这些方法的说明如表1所示.下面通过实例介绍如何使用OutputStream类向控制台输出字符串信息.步骤如下.(1)创建 ...
- C语言入门(7)——自定义函数
C源程序是由函数组成的.虽然在C语言入门系列前面几篇的程序中大都只有一个主函数main(),但实用程序往往由多个函数组成.函数是C源程序的基本模块,通过对函数模块的调用实现特定的功能.C语言中的函数相 ...
- LA 5966 Blade and Sword (双向bfs + 想法) - from lanshui_Yang
题目大意:给你一张有n * m个网格的图,每个网格可能是如下符号: “#”:墙 “P”:出发点 “D”:终点 “.”:空地 “*”:传送机 有一个旅行家(假设名叫Mike),他要从点P到达点D,途中必 ...
- Class constructor
// example: class constructor #include <iostream> using namespace std; class Rectangle { in ...
- HDU 1879 继续畅通工程 (Prim(普里姆算法)+Kruskal(克鲁斯卡尔))
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HTTP的报文格式解析
一.概述 http报文是面向文本的,报文中每一个字段都是一些ASCII码串,各个字段的长度是不确定的.http有两类报文:请求报文 响应报文 二.请求报文 一个http请求报文由 请求行(reque ...
- CSS中的 REM PX EM
px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的 em是相对长度单位.相对于当前对象内文本的字体尺寸.如当前对行内文本的字体尺寸未被人为设置,则相对于浏览器的默认字体尺寸. ...