[LA3026]Period

试题描述

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.

输入

The input file consists of several test cases. Each test case consists of two lines. The first one contains N (2 ≤ N ≤ 1000000) 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.

输出

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.

输入示例

aaa

aabaabaabaab

输出示例

Test case #

Test case #

数据规模及约定

见“输入

题解

KMP 裸题,对于位置 i,它指向的失配的位置为 f[i+1],那么当 f[i+1] > 1 且 (i - f[i+1] + 1) | i 时答案为 i / (i - f[i+1] + 1). 我 KMP 从 1 开始做的所以前面的式子可能会奇怪一些。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 1000010
int n, f[maxn];
char S[maxn]; int main() {
n = read();
int kase = 0; // bool fl = 1;
while(n) {
scanf("%s", S + 1);
// if(!fl) putchar('\n'); fl = 0;
printf("Test case #%d\n", ++kase);
f[1] = f[2] = 1;
for(int i = 2; i <= n; i++) {
int u = f[i];
while(u > 1 && S[u] != S[i]) u = f[u];
f[i+1] = S[u] == S[i] ? u + 1 : u;
if(f[i+1] > 1 && i % (i + 1 - f[i+1]) == 0) printf("%d %d\n", i, i / (i + 1 - f[i+1]));
}
putchar('\n');
n = read();
} return 0;
}

[LA3026]Period的更多相关文章

  1. LA3026 - Period(KMP)

    For each prefix of a given string S with N characters (each character has an ASCII code between 97 a ...

  2. TCP Provider The semaphore timeout period has expired

    我们一数据库服务器上有个作业最近几天偶尔会遇到下面错误(敏感信息已做处理),主要是报"TCP Provider: The semaphore timeout period has expir ...

  3. SSRS 2008 R2 错误:Timeout expired. The timeout period

    今天遇到了Reporting Services(SQL SERVER 2008 R2)的报表执行异常情况,报表加载数据很长时间都没有响应,最后报"An error occurred with ...

  4. Clock Skew , Clock Uncertainty和 Period

    本文将介绍FPGA中和时钟有关的相关概念,阅读本文前需要对时序收敛的基本概念和建立.保持关系有一定了解,这些内容可以在时序收敛:基本概念,建立时间和保持时间(setup time 和 hold tim ...

  5. 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. ...

  6. Match:Period(POJ 1961)

    Period 题目大意:给定一个字符串,要你找到前缀重复了多少次 思路,就是kmp的next数组的简单应用,不要修正next的距离就好了,直接就可以跳转了 PS:喝了点酒用递归实现除法和取余了...结 ...

  7. cargo failed to finish deploying within the timeout period [120000]

    cargo插件,报错:failed to finish deploying within the timeout period [120000] 解决方法:配置timeout为0 <plugin ...

  8. Date get period

    /** * get period for last year * @param time * @return */ public static DatePeriodDTO getLastYear(lo ...

  9. Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

    今天碰到了一个查询异常问题,上网查了一下,感谢原创和译者 如果你使用的数据库连接类是 the Data Access Application Blocks "SqlHelper" ...

随机推荐

  1. jqurey 遍历 div内的所有input单选复选按钮并判断是否选中及Attr(checked)无效的解决

    关于页面前面标签 <ul> @{ foreach (var item in vote) { if (!string.IsNullOrEmpty(item.Img)) { <li cl ...

  2. [Aaronyang] 写给自己的WPF4.5 笔记[2依赖属性]

    人生的意义不在于拿一手好牌,而在于打好一手坏牌 --Aaronyang的博客(www.ayjs.net)-www.8mi.me =============时隔两年后再看WPF========== 因为 ...

  3. Linux 下SVN服务器搭建

    系统环境        RHEL5.4最小化安装(关iptables,关selinux) + ssh + yum 一,安装必须的软件包.  yum install subversion (SVN服务器 ...

  4. 每天一个linux命令(34):kill命令

    Linux 中的kill命令用来终止指定的进程(terminate a process)的运行,是Linux下进程管理的常用命令.通常,终止一个前台进程可以 使用Ctrl+C键,但是,对于一个后台进程 ...

  5. GIthub的小技巧

    目录: 一.快捷键一览表 二.快速搜索项目文件功能 三.使用Github Pages搭建项目网站 一.快捷键一览表 具体操作:         在各个页面下按下shift+/也就是?都可以打开键盘快捷 ...

  6. nginx 日志怎么实现显示真实客户端IP

    这篇文章页不错: http://www.tuicool.com/articles/E32mYf 假如说我们现在的架构是,nginx做反向代理,apache做web服务器.那么我们怎么让我的web服务器 ...

  7. SQLHelper---赵晓虎(简洁,全面)

    public static class SQLHelper { //获取连接字符串,,首先添加对configuration的引用 private static string connStr = Con ...

  8. hdu1828 线段树+离散化+扫描线

    添加lb[],rb[]数组,来标记竖边.添加num,来计算竖边的个数,因为计算周长的时候,未覆盖的竖边都要加. #include<stdio.h> #include<stdlib.h ...

  9. Emmet

    一.简介 Emmet (前身为 Zen Coding) ,不是软件也不是代码,是编辑器(如sublime text)的插件,相应的后缀文件(.html/.css)输入指定的缩写语法,按下tab键就能生 ...

  10. python 学习笔记1(序列;if/for/while;函数;类)

    本系列为一个博客的学习笔记,一部分为我原创. 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 1. print 可以打印 有时需要 ...