博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1358 Period
阅读量:4332 次
发布时间:2019-06-06

本文共 2167 字,大约阅读时间需要 7 分钟。

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 , that is A concatenated K times, for some string A. Of course, we also want to know the period K.
InputThe input file consists of several 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. The input file ends with a line, having the number zero on it.
OutputFor 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.
Sample Input
3aaa12aabaabaabaab0
Sample Output
Test case #12 23 3Test case #22 26 29 312 4 题目意思是找出字符串中由两个或以上相同的子字符串组成的前缀,输出前缀长度及其中相同的子字符串的个数。比如说aabaabaabaab,前缀aa由俩a组成,前缀aabaab由俩aab组成,都满足啦。对于长度为i的前缀串,需要找出它的相等的前后缀,如果相等的前后缀之间没有多余字符,也就是说要么是aabaab这种最大前后缀是aab,或者是aabaabaab这种最大前后缀是aabaab,相同前后缀长度不低于总串长度i的一半,才有可能满足,例如这种abcab相同前后缀是ab,肯定不满足了。对于可能满足的,要用这个前缀的长度i减去他的最大前后缀长度得到非重叠部分的长度,如果i是这个长度的倍数,且不止一倍,就是满足了。 先确立Next数组,记录最长相同的前后缀长度。然后排着去判断[1,n]长度的前缀是否满足。 代码:
#include 
#include
#include
char str[1000000];int Next[1000000],n,k;void findNext() { int j = -1,i = 0; Next[0] = -1; while(i < n) { if(j == -1 || str[i] == str[j]) { Next[++ i] = ++ j; } else j = Next[j]; }}int main() { while(~scanf("%d",&n) && n) { scanf("%s",str); findNext();///先确立Next数组 printf("Test case #%d\n",++ k); for(int i = 1;i <= n;i ++) { if(!Next[i]) continue;///没有相同前后缀 直接过 int j = i - Next[i]; if(i % j == 0) printf("%d %d\n",i,i / j); } puts(""); }}

 

转载于:https://www.cnblogs.com/8023spz/p/7786887.html

你可能感兴趣的文章
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>