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日为参考点; 作用 可以通过该类的对象,表示一个时间,并面向对象操作时间; 构造方法 ...
随机推荐
- P1209 [USACO1.3]修理牛棚 Barn Repair
P1209 [USACO1.3]修理牛棚 Barn Repair 题目描述 在一个夜黑风高,下着暴风雨的夜晚,farmer John的牛棚的屋顶.门被吹飞了. 好在许多牛正在度假,所以牛棚没有住满. ...
- 机器学习常用算法(LDA,CNN,LR)原理简述
1.LDA LDA是一种三层贝叶斯模型,三层分别为:文档层.主题层和词层.该模型基于如下假设:1)整个文档集合中存在k个互相独立的主题:2)每一个主题是词上的多项分布:3)每一个文档由k个主题随机混合 ...
- leetcode笔记--7 Find the Difference
question: Given two strings s and t which consist of only lowercase letters. String t is generated b ...
- 『Python Kivy』Kivy模板语言KV说明
语言概念 KV语言允许你以声明的方式创建控件树,以及绑定控件属性到其他的控件或使用一种自然的方式进行回调. 它允许非常快速并灵活的改变你的UI. 它还可以让你的应用程序与应用程序的界面进行分隔. 如何 ...
- hdu1181变形课(floyd)
变形课 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submis ...
- 刚安装的Linux Centos7使用yum安装firefox时提示:cannot find a valid baseurl for repo
出现这个问题是因为yum在安装包的过程中,虽然已经联网,但是没法解析远程包管理库对应的域名,所以我们只需要在网络配置中添加上DNS对应的ip地址即可. 解决参考链接:https://blog.csdn ...
- 初学Direct X(9) ——文字的显示
初学Direct X(9) --文字的显示 本次学习如何使用ID3DXFont创建字体,使得我们可以在任何安装了Windows系统中TrueType字体来打印文字,不过最好使用标准字体,这样文字在每一 ...
- leetcode-累加数(C++)
累加数是一个字符串,组成它的数字可以形成累加序列. 一个有效的累加序列必须至少包含 3 个数.除了最开始的两个数以外,字符串中的其他数都等于它之前两个数相加的和. 给定一个只包含数字 '0'-'9' ...
- [Clr via C#读书笔记]Cp6类型和成员基础
Cp6类型和成员基础 成员 常量:字段(静态字段和实例字段):实例构造器:类型构造器(用于静态字段的构造):方法(静态方法和实例方法):操作符重载(本质是一个方法):转换操作符:属性(本质还是方法): ...
- Centos7添加静态路由
本文摘取自 Centos7系统配置上的变化(二)网络管理基础 一.ip route显示和设定路由 1.显示路由表 [root@centos7 ~]# ip route show default via ...