[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. 迅雷首席架构师刘智聪:微信小程序的架构与系统设计的几点观感

    笔者注:本文来自于迅雷首席工程师刘智聪的个人分享,他毕业于南昌大学化学系,加入迅雷后设计开发了多款迅雷核心产品,凭借“大规模网络流媒体服务关键支撑技术”项目获得2015年国家科学技术进步奖二等奖,同时 ...

  2. JavaScript基础系列目录(2014.06.01~2014.06.08)

    下列文章,转载请亲注明链接出处,谢谢! 链接地址: http://www.cnblogs.com/ttcc/tag/JavaScript%20%E5%9F%BA%E7%A1%80%E7%9F%A5%E ...

  3. JavaScript事件---事件绑定和深入

    发文不易,转载传播,请亲注明链接出处,谢谢! 内容提纲: 1.传统事件绑定的问题 2.W3C事件处理函数 3.IE事件处理函数 4.事件对象的其他内容 事件绑定分为两种:一种是传统事件绑定(内联模型, ...

  4. Source Tree for MAC1.6

    Atlassian ID has become the new Atlassian Account. Read more about it here. After some great communi ...

  5. 如何正确地使用Entity Framework Database First

    毕设依旧在不紧不慢地以每天解决一个问题的进度进行中.今天遇到的问题就是在建立数据模型时遇到的.因为项目是基于数据库构建的,所以理所应当地采用DB First来构造实体类和DbContext类.于是想也 ...

  6. BZOJ-2929 洞穴攀岩 最大流Dinic(傻逼题)

    竟然没有1A真羞耻...1分钟不到读完题,10分钟不到打完....MD没仔细看...WA了一遍,贱! 2929: [Poi1999]洞穴攀行 Time Limit: 1 Sec Memory Limi ...

  7. BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...

  8. 装了虚拟机,但是没有虚拟网卡vmnet0 vmnet1 vmnet8

    在服务里面启动图中的两个服务,在再你的虚拟网络编辑器里面点恢复默认设置就会出现了,我的是win8.1,默认这两个服务是手动启动的,可能是优化软件优化的结果 我是win10  里面显示以太网3 和 4, ...

  9. 抓包利器Fiddler

    1).Fiddler安装 a.下载地址: http://fiddler2.com/get-fiddler b.安装:省略(下一步...下一步即可) 2).Fiddler配置 a.允许远程计算机连接Fi ...

  10. UVA116Unidirectional TSP(DP+逆推)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18206 题意:M*N的数阵,从左边一列到右边一列走过的数的和的最小.并输出路 ...