SP263 PERIOD - 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 A K ^{K} K , that is A concatenated K times, for some string A. Of course, we also want to know the period K.
输入输出格式
输入格式:
The first line of the input file will contains only the number T (1 <= T <= 10) of the 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.
输出格式:
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.
输入输出样例
2
3
aaa
12
aabaabaabaab
Test case #1
2 2
3 3 Test case #2
2 2
6 2
9 3
12 4
Solution:
题意就是求一个字符串的所有循环前缀的最大周期数(周期个数不能为1)。
这种字符串的周期问题,下意识想到去求所有前缀的最长公共前后缀(next数组),考虑到next的性质:$S|0~next[i]|=S|i-next[i]+1~i|$,当字符串$S|0~i|$存在循环周期时,必定存在$(i-next[i])|i$(反之亦然),即错位部分$S|next[i]~i|$一定是一个周期且是最小周期(因为是最长公共前后缀),那么周期数最多就是$\frac{i}{i-next[i]}$,由于题目中周期不能为本身,所以$next[i]$必须大于$0$才去判断。
代码:
#include<bits/stdc++.h>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
using namespace std;
int t,n,next[];
char s[]; int main(){
scanf("%d",&t);
For(k,,t){
scanf("%d%s",&n,s);
next[]=next[]=;
For(i,,n-){
int p=next[i];
while(p&&s[i]!=s[p]) p=next[p];
next[i+]=(s[i]==s[p]?p+:);
}
printf("Test case #%d\n",k);
For(i,,n) if(next[i]&&i%(i-next[i])==) printf("%d %d\n",i,i/(i-next[i]));
printf("\n");
}
return ;
}
SP263 PERIOD - Period的更多相关文章
- 【题解】PERIOD - Period [POJ1961] [SP263]
[题解]PERIOD - Period [POJ1961] [SP263] 在进入这道题之前,我们需要了解 kmp 算法 不知道的童鞋可以去看一下Silent_EAG(一个可爱的女孩纸)的讲解. 关于 ...
- SP263 PERIOD - Period KMP技巧
\(\color{#0066ff}{题目描述}\) 如果一个字符串S是由一个字符串T重复K次形成的,则称T是S的循环元.使K最大的字符串T称为S的最小循环元,此时的K称为最大循环次数. 现给一个给定长 ...
- 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 ...
- Java8 Period.between方法坑及注意事项
在使用Java8 新特性中关于Period.between的方法时需注意该方法获取日期的区间问题. @Test public void test1(){ LocalDate from = LocalD ...
- Duration和Period的区别--通俗易懂
在jdk1.8以后,对表示日期时间的类型进行了重新分类,这里出现了2个新的类,Duraction 和Period Duraction表示:时间的区间,用来度量秒和纳秒之间的时间值 Period表示:一 ...
- Java学习 时间类 Period类与Duration类 / LocalDate类与Instant类 用法详解
前言 java 8 中引入的两个与日期相关的新类:Period 和 Duration.两个类看表示时间量或两个日期之间的差,两者之间的差异为:Period基于日期值,而Duration基于时间值.他们 ...
- Period 时间坑
jdk1.8 的Period Period between = Period.between( LocalDate.parse("2018-01-01 00:00:00", Dat ...
- JDK8中的新时间API:Duration Period和ChronoUnit介绍
目录 简介 Duration Period ChronoUnit 简介 在JDK8中,引入了三个非常有用的时间相关的API:Duration,Period和ChronoUnit. 他们都是用来对时间进 ...
- Java基础进阶:时间类要点摘要,时间Date类实现格式化与解析源码实现详解,LocalDateTime时间类格式化与解析源码实现详解,Period,Duration获取时间间隔与源码实现,程序异常解析与处理方式
要点摘要 课堂笔记 日期相关 JDK7 日期类-Date 概述 表示一个时间点对象,这个时间点是以1970年1月1日为参考点; 作用 可以通过该类的对象,表示一个时间,并面向对象操作时间; 构造方法 ...
随机推荐
- codechef Table Game(博弈)
题意 题目链接 很难概括.. Sol (因为比赛还没结束,所以下面讲的可能是“非官方”“正解”) maya这题我前前后后 断断续续的做了一个星期才A掉.CC一场challenge出两道打表题可有点过分 ...
- 北京Uber优步司机奖励政策(3月9日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Spark入门(Python版)
Hadoop是对大数据集进行分布式计算的标准工具,这也是为什么当你穿过机场时能看到”大数据(Big Data)”广告的原因.它已经成为大数据的操作系统,提供了包括工具和技巧在内的丰富生态系统,允许使用 ...
- [Jmeter]jmeter之BeanShell Sampler测试应用
前言: 在做接口测试的时候,有些接口做了签名校验,而签名是根据某算法进行加密,这时候,简单的接口测试工具无法完成该工作,所以想到了Jmeter,他是java编写,有强大的扩展性,足矣完成我们需要的操作 ...
- Mac OS下Android Studio:/dev/kvm not found
在配置模拟器时出现该报错,在网上找了很多教程都没能解决,当然可能是这些教程并不适用于我.总的来说,还是要“对症下药”! 解决方法如下: 点击“系统偏好设置”-“安全性与隐私”,然后会在“通用”这个界面 ...
- Python之多进程多线程
一.多进程与多线程的概念 1.多进程的概念 进程是程序在计算机上的的一次执行活动.当你运行一个程序,你就启动了一个进程.显然,程序是死的(静态的),进程是活的(动态的).进程可以分为系统进程和用户进程 ...
- InnoDB锁冲突案例演示(续)
Preface I've demontstrated several InnoDB locking cases in my previous blog.I'm gonna do the ...
- 「专题训练」Air Raid(HDU-1151)
题目 在一个城市里有\(n\)个地点和\(k\)条道路,道路是无环的(也就是说一定可以二分染色--回路长度为偶数0),现在伞兵需要去n个地点视察,只能沿着路的方向走,问最少需要多少伞兵. 分析 这是什 ...
- Python零基础入门必知
Python自学知识点总结 //2018.10.09 1. Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido ...
- XX出行项目子系统-统计系统设计(定时器项目设计例子)
一. 引言 目前开发的XX出行系统,需要开发数据统计功能,鉴于约约出行系统已经在运营,并且有新版本的迭代,方便以后下个版本复用,遂新建一个子系统. 二. 架构设计 三. 具体实现 1.MySql数据库 ...