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

输入示例

3
aaa
12
aabaabaabaab
0

输出示例

Test case #1
2 2
3 3Test case #2
2 2
6 2
9 3
12 4

数据规模及约定

见“输入

题解

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;
}

转载于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5753884.html

[LA3026]Period相关推荐

  1. LA3026 Period

    题意大概是,给你一个字符串,如果该字符串的某一个前缀Si是由若干个相同的字符串循环构成,就输出i和最短循环节长度 利用KMP算法里面失配函数可以解决这个问题,失配函数f(i)可以定义为字符串的前缀Si ...

  2. 时间周期 java_周期和持续时间 / Period and Duration

    # 周期和持续时间 / Period and Duration 当您编写代码来指定一段时间时,请使用最符合您需要的类或方法: Duration 类, Period 类或 ChronoUnit.betw ...

  3. java between_Java8 Period.between方法坑及注意事项

    在使用Java8 新特性中关于Period.between的方法时需注意该方法获取日期的区间问题. @Test public void test1(){ LocalDate from = LocalD ...

  4. HDU 5908 Abelian Period 暴力

    Abelian Period 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5908 Description Let S be a number st ...

  5. Java 8 – Period and Duration examples

    Few examples to show you how to use Java 8 Duration, Period and ChronoUnit objects to find out the d ...

  6. Back Stub Period

    http://www.investopedia.com/terms/p/portfolio.asp#axzz1lnDvrq00 The last interim period in the life ...

  7. JDK8中的新时间API:Duration Period和ChronoUnit介绍

    文章目录 简介 Duration Period ChronoUnit 简介 在JDK8中,引入了三个非常有用的时间相关的API:Duration,Period和ChronoUnit. 他们都是用来对时 ...

  8. Java8中使用以下类计算日期时间差:Period、Duration、ChronoUnit

    一.简述 在Java8中,我们可以使用以下类来计算日期时间差异: 1.Period 2.Duration 3.ChronoUnit 二.Period类 主要是Period类方法getYears(),g ...

  9. 【POJ - 1961】Period(KMP,循环节问题)

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

最新文章

  1. 设置Open in Terminal快捷键Alt+Shift+Z,方便angular项目的各种命令输入(多任务状态下)
  2. AI算法起家的今日头条为何败给了色情?一文告诉你机器学习何以搞不定鉴黄
  3. MILA研究院唐建:基于图神经网络的关系推理(附视频)
  4. 在ubuntu上安装,使用MQTT Mosquitto
  5. 容器处于restarting状态_Docker容器操作-基础命令
  6. mysql pgsql 2017,DB-Engines 2017 年度数据库:PostgreSQL 实至名归
  7. Dalvik线程模型
  8. 我在 1969 年写代码
  9. 部分Excel函数的使用
  10. 11.sql条件查询
  11. 【Mybatis学习路线】day02mybatis的增删改查操作
  12. redis查看集合中元素的数量,scard
  13. [Git 1]基本操作与协同开发
  14. SpringCloud-Seata分布式事务服务案例之订单Order服务搭建(Day16)
  15. matlab做的脑网络分析,eeg脑网络分析
  16. 疲劳驾驶检测系统的研究
  17. 2023年全国最新会计专业技术资格精选真题及答案16
  18. 2020年车联网行业深度报告 附下载地址
  19. GOA WinForms 系列
  20. (K8s入门到精通) 10 k8s service(SVC),管理多个pod信息

热门文章

  1. Excel公式与函数案例速查手册/电脑技巧从入门到精通丛书
  2. 今天下午接到阔总编的电话
  3. ImportError cannot import name BytesIO when import caffe
  4. 【数据平台】sqoop支持hive和mysql互导的基本语句格式
  5. vue中引入jquery
  6. Vue常用特性——表单操作、表单域修饰符(number:转化为数值 ;trim:去掉开始和结尾的空格 ; lazy : 将input事件切换为change事件)||自定义指令|| 局部指令
  7. 多重循环 ——— 打印九九乘法表 || 经典的打印金字塔(打印整个金字塔 打印空心金字塔 打印空心菱形金字塔)
  8. 标签页使用及bug解决
  9. Windows 技术篇-LDSGameMaster文件夹有什么用,删除方法
  10. C# ListView控件显示表格(自适应宽度),添加 Checkbox,删除选择项,选中颜色和鼠标滑过背景变色