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 3Test 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[1000005]; char s[1000005];int main(){scanf("%d",&t);For(k,1,t){scanf("%d%s",&n,s);next[0]=next[1]=0;For(i,1,n-1){int p=next[i];while(p&&s[i]!=s[p]) p=next[p];next[i+1]=(s[i]==s[p]?p+1:0);}printf("Test case #%d\n",k);For(i,2,n) if(next[i]&&i%(i-next[i])==0) printf("%d %d\n",i,i/(i-next[i]));printf("\n");}return 0; }
转载于:https://www.cnblogs.com/five20/p/9448899.html
SP263 PERIOD - Period相关推荐
- java between_Java8 Period.between方法坑及注意事项
在使用Java8 新特性中关于Period.between的方法时需注意该方法获取日期的区间问题. @Test public void test1(){ LocalDate from = LocalD ...
- 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 ...
- JDK8中的新时间API:Duration Period和ChronoUnit介绍
文章目录 简介 Duration Period ChronoUnit 简介 在JDK8中,引入了三个非常有用的时间相关的API:Duration,Period和ChronoUnit. 他们都是用来对时 ...
- Java8中使用以下类计算日期时间差:Period、Duration、ChronoUnit
一.简述 在Java8中,我们可以使用以下类来计算日期时间差异: 1.Period 2.Duration 3.ChronoUnit 二.Period类 主要是Period类方法getYears(),g ...
- java amount_java.time.Period.plus(TemporalAmount amountToAdd)方法
java.time.Period.plus(TemporalAmount amountToAdd)方法返回此Period的副本,并添加了指定的Period. 声明 以下是java.time.Perio ...
- JAVA时间存储类Period和Duration
JAVA时间存储类Period和Duration 前言 在JDK1.8中区分了时间和日期的概念,所以有了两个对应的类,LocalDate和LocalTime,区别如下 LocalDate表示日期,不会 ...
- Pandas-时期(二)-数据类型:PeriodIndex(其中每个元素的类型为:Period)【创建:①pd.PeriodIndex([], freq=‘‘);②pd.period_range()】
Period('2011', freq = 'A-DEC')可以看成多个时间期的时间段中的游标 一.创建PeriodIndex数据类型 1. pd.PeriodIndex(data=[], freq= ...
- Java 8中计算两个日期间隔多少年、多少月、多少天 (Period)
有一个常见日期操作计算两个日期之间的年数.月数.天数.在Java 8中可以用java.time.Period类来做计算. import java.time.LocalDate; import java ...
- Java: LocalDate、LocalDateTime、Duration、Period、TimeUnit类的一些用法
一.LocalDate 初始化操作 // 获取当前年月日 LocalDate now = LocalDate.now(); System.out.println(now); // 2021-03-13 ...
- java时间单位框架接口TemporalAmount及其实现类Duration和Period
上一篇我们聊了时间单位抽象出的接口TemporalUnit,今天我们来聊时间段抽象出的接口TemporalAmount及其实现类Duration和Period. 实例化emporalAmount的实现 ...
最新文章
- 50多种适合机器学习和预测应用的API,你的选择是?(2018年版本)
- linux设置外接显示器的分辨率
- “有些重要的事要告诉你” !小米春季新品发布会定档3月29日
- 漫画:应用程序被拖慢?罪魁祸首是 Log4j!
- QT中中文乱码的解决
- [黑苹果双系统]macOS 12.4正式版OpenCore/Clover/winPE原版镜像
- Android蜗牛睡眠技术文档,蜗牛睡眠app 问题提问集中贴 常见问题汇总
- css 平行四边形 梯形 组合_CSS实现平行四边形布局
- Navicat:Access violation at address xxxxxxxxx in module 'navicat.exe'.Read of address xxxxxx
- python后台开发性能问题_《Python高性能编程》——2.14 确保性能分析成功的策略-阿里云开发者社区...
- 屏幕绘画 V1.1 - 一念(VB 绘制 图形)
- 云服务器虚拟手机版,云服务器虚拟手机
- 网络编程--JAVA之多线程下载后续:断点续存
- 解决pycharm中中文列表输出'\xe5\xa4\xa7\xe8\x92\x9c'之类的字符串
- @Validated和@Valid使用
- ssm高校党员信息管理系统
- XPRIZE确定十项未来保护森林所需的技术突破
- 均匀分布 卡方分布_【Math】概率论常用分布大全
- 基于Antd Input组件自定义Input的成功状态
- 大饼博士X Blog文章索引:机器学习方法系列,深度学习方法系列,三十分钟理解系列等