E - Period
InputThe input file consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) �C 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.
OutputFor 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 结论:j%(j-Nex[t])==0 说明该前缀是一个周期为j/(j-Next[j])的周期子序列
证明: j - Next[j] 是子串在失配时候的右移长度,说明在j位置失配的时候子串转移到next[j]处继续匹配,说明j之前的部分和next[j]之前的部分是相同的,也就是说j-Next[j]
是一个周期
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
#define MAXN 1000001
typedef long long LL;
/*
*/
char s[MAXN];
int Next[MAXN];
void kmp_pre(int m)
{
int j,k;
j = ;
k = -;
Next[] = -;
while(j<m)
{
if(k==-||s[j]==s[k])
Next[++j] = ++k;
else
k = Next[k];
}
}
int main()
{
int m;
int cas=;
while(scanf("%d",&m),m)
{
scanf("%s",s);
kmp_pre(m);
printf("Test case #%d\n",cas++);
for(int i=;i<=m;i++)
if((i)%(i-Next[i])==&&i/(i-Next[i])>)
printf("%d %d\n",i,(i)/(i-Next[i]));
cout<<endl;
}
}
E - Period的更多相关文章
- TCP Provider The semaphore timeout period has expired
我们一数据库服务器上有个作业最近几天偶尔会遇到下面错误(敏感信息已做处理),主要是报"TCP Provider: The semaphore timeout period has expir ...
- SSRS 2008 R2 错误:Timeout expired. The timeout period
今天遇到了Reporting Services(SQL SERVER 2008 R2)的报表执行异常情况,报表加载数据很长时间都没有响应,最后报"An error occurred with ...
- Clock Skew , Clock Uncertainty和 Period
本文将介绍FPGA中和时钟有关的相关概念,阅读本文前需要对时序收敛的基本概念和建立.保持关系有一定了解,这些内容可以在时序收敛:基本概念,建立时间和保持时间(setup time 和 hold tim ...
- HDU 5908 Abelian Period(暴力+想法题)
传送门 Description Let S be a number string, and occ(S,x) means the times that number x occurs in S. i. ...
- Match:Period(POJ 1961)
Period 题目大意:给定一个字符串,要你找到前缀重复了多少次 思路,就是kmp的next数组的简单应用,不要修正next的距离就好了,直接就可以跳转了 PS:喝了点酒用递归实现除法和取余了...结 ...
- cargo failed to finish deploying within the timeout period [120000]
cargo插件,报错:failed to finish deploying within the timeout period [120000] 解决方法:配置timeout为0 <plugin ...
- Date get period
/** * get period for last year * @param time * @return */ public static DatePeriodDTO getLastYear(lo ...
- Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
今天碰到了一个查询异常问题,上网查了一下,感谢原创和译者 如果你使用的数据库连接类是 the Data Access Application Blocks "SqlHelper" ...
- POJ 1961 Period( KMP )*
Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...
- [LA3026]Period
[LA3026]Period 试题描述 For each prefix of a given string S with N characters (each character has an ASC ...
随机推荐
- P1462 通往奥格瑞玛的道路(二分答案+最短路)
P1462 通往奥格瑞玛的道路 题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡 ...
- canvas 文字转化为粒子
var canvas = document.createElement('canvas'); var cxt = canvas.getContext('2d'); var W = canvas.wid ...
- akka设计模式系列-消息模型
通过前面的文章我们总结了几个常见的actor设计模式,但此处不得不提前介绍一下在Akka中消息的设计模式.随着对Akka的使用,我们会发现,使用Akka设计系统其实就是面向消息编程.actor之间消息 ...
- 最大正方形 同luogu1387
这道题下面这么写就够了(n<=100)暴力,枚举 #include<bits/stdc++.h> #define ULL unsigned long long #define MAX ...
- jsp动态网页开发基础
JSP基础语法 jsp页面元素构成 jsp页面组成部分有:指令,注释,静态内容,表达式,小脚本,声明. 1.表达式<%= %> 2.小脚本<% %> 3.声 ...
- 线性回归的Cost function实现
此处使用Octave来实现 线性方程的代价函数: 代价函数: X 是测试值,假设用矩阵表示为 为了方便用矩阵计算我们把X加一列 1 : 同时 那么h( ...
- HTTP05--HTML常用知识
一.URL地址含义 需要搞清URL和URI的差别,以及QueryString的含义. 二.GET和POST的区别 详细介绍可参考文章:http://zengrong.net/post/1802.htm ...
- jQuery——尺寸位置
获取宽:$(".box").width() 设置宽:$(".box").width(200) 获取高:$(".box").height() ...
- 用Grunt进行CSS文件压缩
假设你的项目的CSS文件全部放在项目目录下名为css的文件夹中,现在将它压缩合并成一个名为main-min.css的文件,放在css-min文件夹下. (1)首先保证机器安装了node.js. (2) ...
- DateTimePicker 控件置空
dtOrderDateFrom.Format = DateTimePickerFormat.Custom; dtOrderDateFrom.CustomFormat = " "; ...