HDU1358 Period 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358
题目大意:给你一个长度为 \(n\) 的字符串 \(s\) ,那么它有 \(n\) 个前缀。
对于第 \(i\) 个前缀 \(t\) ,如果他们表示成一个子串重复 \(K\) 次 \((K>1)\) ,那么我们就输出这个前缀 \(t\) 的长度以及对应的最大的 \(K\) 。(因为对于 \(aaaa\) 来说,\(K\) 可以为 \(4\) 或者 \(2\) ,但是 \(4\) 比较大,所以选择 \(K\) )。
题目分析:考察 \(next\) 函数的应用。我们假设长度为 \(i\) 的前缀, \(i-nxt[i]\) 能够整除 \(i\) ,那么 \(K=i/(i-nxt[i])\) 。
实现代码如下:
#include <cstdio>
#include <string>
using namespace std;
const int maxn = 1001000;
int m, nxt[maxn], cas = 1;
string t;
char ch[maxn];
string read() {
scanf("%s", ch);
string tmp_s = ch;
return tmp_s;
}
void cal_next() {
m = t.length();
for (int i = 0, j = -1; i < m; i ++) {
while (j != -1 && t[j+1] != t[i]) j = nxt[j];
nxt[i] = (j+1 < i && t[j+1] == t[i]) ? ++j : -1;
}
}
int main() {
while ( ~scanf("%d", &m) && m ) {
t = read();
cal_next();
printf("Test case #%d\n", cas ++);
for (int i = 0; i < m; i ++) {
int len = i + 1;
int delta = i - nxt[i];
if (len % delta == 0 && len / delta > 1) {
printf("%d %d\n", i+1, len/delta);
}
}
puts("");
}
return 0;
}
作者:zifeiy
HDU1358 Period 题解 KMP算法的更多相关文章
- hdu 1358:Period(KMP算法,next[]数组的使用)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- LA 3026 && POJ 1961 Period (KMP算法)
题意:给定一个长度为n字符串s,求它每个前缀的最短循环节.也就是对于每个i(2<=i<=n),求一个最大整数k>1(如果存在),使得s的前i个字符组成的前缀是某个字符串重复得k次得到 ...
- poj1961 & hdu1358 Period【KMP】
Period Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 20436 Accepted: 9961 Descripti ...
- UVALive 3026 Period (KMP算法简介)
kmp的代码很短,但是不太容易理解,还是先说明一下这个算法过程吧. 朴素的字符串匹配大家都懂,但是效率不高,原因在哪里? 匹配过程没有充分利用已经匹配好的模版的信息,比如说, i是文本串当前字符的下标 ...
- POJ3080 Blue Jeans 题解 KMP算法
题目链接:http://poj.org/problem?id=3080 题目大意:给你N个长度为60的字符串(N<=10),求他们的最长公共子串(长度>=3). 题目分析:KMP字符串匹配 ...
- POJ2185 Milking Grid 题解 KMP算法
题目链接:http://poj.org/problem?id=2185 题目大意:求一个二维的字符串矩阵的最小覆盖子矩阵,即这个最小覆盖子矩阵在二维空间上不断翻倍后能覆盖原始矩阵. 题目分析:next ...
- HDU1711 Number Sequence 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目大意:最基础的字符串匹配,只不过这里用整数数组代替了字符串. 给你两个数组 \(a[1..N ...
- HDU1686 Oulipo 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:给你一个子串t和一个母串s,求s中有多少个子串t. 题目分析:KMP模板题. cal_ ...
- HDU3336 Count the string 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题目大意:找出字符串s中和s的前缀相同的所有子串的个数. 题目分析:KMP模板题.这道题考虑 n ...
随机推荐
- C#中Object转化为json对象
比如定义一个类: public class Lines { public string X1 { get; set; } public string X2 { get; set; } public s ...
- Postgresql 正则表达式【转】
原文:http://blog.csdn.net/wugewuge/article/details/7704996 postgresql支持POSIX 风格的正则表达式,在postgresql中使用正则 ...
- 公司mysql问题三
数据库连接不上,解决方案: # 加在绿框?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
- Python2.7用sys.stdout.write实现打印刷新
如何能在控制台实现在一行中显示进度的信息呢,就像使用pip安装时的进度那样. 如果用print则会打印成多行,下面这个小技巧可以在一行中打印: import time import sys if __ ...
- LintCode刷题笔记-- Count1 binary
标签: 位运算 描述: Count how many 1 in binary representation of a 32-bit integer. 解题思路: 统计一个int型的数的二进制表现形式中 ...
- Ubuntu 18.04中的Vim编辑器的高级配置
VIM (Unix及类Unix系统文本编辑器) Vim是一个类似于Vi的著名的功能强大.高度可定制的文本编辑器,在Vi的基础上改进和增加了很多特性.VIM是开源软件. Vim普遍被推崇为类Vi编辑器中 ...
- openstack安装dashboard后访问horizon出错 500 or 504
访问controller/horizon出错500:internal server error 访问controller/horizon出错504:internal server error gat ...
- Codeforces 336C
这题是大一暑假时候做的,当时没有出,直到今天突然觉得应该把没过的题目再做一边,不然真的是越积越多. 现在能够独立做出来真的是难以表达的兴奋,刚开始的时候就觉得 O(30 * 30 * n)的复杂度有点 ...
- 简单线性回归(最小二乘法)python实现
简单线性回归(最小二乘法)¶ 0.引入依赖¶ In [7]: import numpy as np import matplotlib.pyplot as plt 1.导入数据¶ In [ ...
- windows下多版本python安装与pip安装和pip使用 吐血总结
https://blog.csdn.net/silence2015/article/details/56483892/ 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附 ...