[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher E - Period HDU - 1358(循环节kmp)
时间:2019-08-15
本文章向大家介绍[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher E - Period HDU - 1358(循环节kmp),主要包括[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher E - Period HDU - 1358(循环节kmp)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
E - Period HDU - 1358
题目链接:https://vjudge.net/contest/70325#problem/E
题目:
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
3 aaa 12 aabaabaabaab 0Sample Output
Test case #1 2 2 3 3 Test case #2 2 2 6 2 9 3 12 4
题意:求出i个字母之前的字符串如果具有循环特性给出字符串的长度和其循环节的数量
思路:i-next[i]是如果具有循环节的话,那么他就是循环节的长度,当i%(i-next[i])==0的话,那么i这么长的字符串就是具有循环特性,那么i/(i-next[i])就是循环节的数量了
注意下列代码注释的部分不能那样写,因为他是优化过的kmp匹配,不能匹配到每个next[i],而一般的kmp就会每个都匹配到,从而不会造成漏解
// // Created by HJYL on 2019/8/15. // #include <iostream> #include <vector> #include <map> #include <string> #include <queue> #include <stack> #include <set> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> using namespace std; const int maxn=1e6+10; char str[maxn]; int nextt[maxn]; void getnext() { int i=0,j=-1; nextt[0]=-1; int n=strlen(str); while(i<n) { if(j==-1||str[i]==str[j]) { i++,j++; //if(str[i]!=str[j]) nextt[i]=j; // else // nextt[i]=nextt[j]; } else j=nextt[j]; } } int main() { //freopen("C:\\Users\\asus567767\\CLionProjects\\untitled\\text","r",stdin); int T; int kase=1; while(~scanf("%d",&T)){ if(T==0) break; scanf("%s",str); getnext(); printf("Test case #%d\n",kase++); int len=strlen(str); for(int i=1;i<=len;i++) { if(nextt[i]!=0&&i%(i-nextt[i])==0) printf("%d %d\n",i,i/(i-nextt[i])); } printf("\n"); } return 0; }
原文地址:https://www.cnblogs.com/Vampire6/p/11360537.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 图文详解互联网根基之HTTP
- Jenkins部署异常:报错 TomcatManagerException: FAIL - Unable to delete
- Spring Boot打包的jar运行的时候提示:没有主清单属性
- 主流消息队列选型技术比较
- 深入浅出-网络七层模型
- 【警惕】K8S下Telnet失效陷阱
- nginx优化之keepalive
- 血泪教训,线程池引发的内存泄露
- RestTemplate设置转换类型
- 详解PROTOCOL BUFFERS
- Spring boot常用注解收集
- Go依赖模块版本之Module避坑使用详解
- 【tcl学习】vivado write_edif
- K8s中优雅停机和零宕机部署
- Python读取.edf格式脑电数据文件