HDU3746 Teacher YYF 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746
题目大意:给你一个串 \(s\) ,要求 \(s\) 的开头或结尾添加最少的字符,使得添加后的串可以表示为 \(K\) 个相同的子串的拼接 \((K>=2)\) 。
题目分析:首先如果这个串s已经是一个循环串了,这种情况下 \(nxt[m-1] != -1\) 且 \(m-1-nxt[m-1]\) 能够整除 \(m\) ,那么输出 \(0\) 即可。
错误分析: 不然的话,我们添加一些字符后的串应该是 \(m-1-nxt[m-1]\) 长度的两倍,所以要添加的字符的数量是 \((m-1-nxt[m-1)*2-m\) 。(这样是WA的)。
然后,我们令 \(len = m-1-nxt[m-1]\) ,答案就是 \(len - m % len\) 。
实现代码如下:
#include <string>
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 100100;
int T, m, nxt[maxn];
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() {
scanf("%d", &T);
while (T --) {
t = read();
cal_next();
int p = nxt[m-1] + 1;
if (nxt[m-1] != -1 && m % (m-1-nxt[m-1]) == 0) {
puts("0");
} else {
// printf("%d\n", (m-1-nxt[m-1])*2-m );
int len = m-1-nxt[m-1];
printf("%d\n", len - m % len );
}
}
return 0;
}
作者:zifeiy
HDU3746 Teacher YYF 题解 KMP算法的更多相关文章
- 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 ...
- POJ2752 Seek the Name, Seek the Fame 题解 KMP算法
题目链接:http://poj.org/problem?id=2752 题目大意:给你一个字符串 \(S\) ,如果它的一个前缀同时也是它的后缀,则输出这个前缀(后缀)的长度. 题目分析:next函数 ...
- POJ2406 Power Strings 题解 KMP算法
题目链接:http://poj.org/problem?id=2406 题目大意:给你一个字符串 \(t\) ,\(t\) 可以表示为另一个小字符串循环了 \(K\) 了,求最大的循环次数 \(K\) ...
- HDU2087 剪花布条 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 题目大意:给定字符串 \(s\) 和 \(t\) ,找出 \(s\) 中出现不重叠的 \(t\) ...
- HDU1358 Period 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目大意:给你一个长度为 \(n\) 的字符串 \(s\) ,那么它有 \(n\) 个前缀. 对 ...
随机推荐
- day38 04-Spring加载配置文件
Spring的工厂类ApplicationContext有两个子类:ClassPathXmlApplicationConext和FileSystemXmlApplication都可以用来加载配置文件. ...
- P1460 健康的荷斯坦奶牛 Healthy Holsteins
P1460 健康的荷斯坦奶牛 Healthy Holsteins 题目描述 农民JOHN以拥有世界上最健康的奶牛为傲.他知道每种饲料中所包含的牛所需的最低的维他命量是多少.请你帮助农夫喂养他的牛,以保 ...
- Leetcode63.Unique Paths II不同路径2
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为" ...
- Eclipse Git插件切换分支的时候不要Reset
今天做了一件蠢事,我在当前分支上改了很多代码,后来切换分支的时候,有一个文件有冲突,eclipse提示这个文件冲突,我可以选择commit/stash/reset,我一看这个文件没什么关系,不需要提交 ...
- java如何访问memcache
1 Memcache是什么 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的 ...
- spring boot 异常处理(转)
spring boot在异常的处理中,默认实现了一个EmbeddedServletContainerCustomizer并定义了一个错误页面到"/error"中,在ErrorMvc ...
- FastAdmin 使用 phpmail 出现 spl_autoload_register 错误
FastAdmin 使用 phpmail 出现 spl_autoload_register 错误 现象 意思是 __autoload() 已经废弃 问题来源于:https://ask.fastadmi ...
- Leetcode844.Backspace String Compare比较含退格的字符串
给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果. # 代表退格字符. 示例 1: 输入:S = "ab#c", T = " ...
- could not insert: [com.trs.om.bean.UserLog] The user specified as a definer ('root'@'127.0.0.1') does not exist
2019-07-01 11:24:09,315 [http-8080-24] org.hibernate.util.JDBCExceptionReporter logExceptionsWARN: S ...
- nodeJs学习-16 数据字典示例
1.banner ID title 标题 varchar(32) sub_title 副标题 varchar(16) src 图片地址 varchar(64) 2.文章 ID author 作者 va ...