题目链接: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算法的更多相关文章

  1. POJ3080 Blue Jeans 题解 KMP算法

    题目链接:http://poj.org/problem?id=3080 题目大意:给你N个长度为60的字符串(N<=10),求他们的最长公共子串(长度>=3). 题目分析:KMP字符串匹配 ...

  2. POJ2185 Milking Grid 题解 KMP算法

    题目链接:http://poj.org/problem?id=2185 题目大意:求一个二维的字符串矩阵的最小覆盖子矩阵,即这个最小覆盖子矩阵在二维空间上不断翻倍后能覆盖原始矩阵. 题目分析:next ...

  3. HDU1711 Number Sequence 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目大意:最基础的字符串匹配,只不过这里用整数数组代替了字符串. 给你两个数组 \(a[1..N ...

  4. HDU1686 Oulipo 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:给你一个子串t和一个母串s,求s中有多少个子串t. 题目分析:KMP模板题. cal_ ...

  5. HDU3336 Count the string 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题目大意:找出字符串s中和s的前缀相同的所有子串的个数. 题目分析:KMP模板题.这道题考虑 n ...

  6. POJ2752 Seek the Name, Seek the Fame 题解 KMP算法

    题目链接:http://poj.org/problem?id=2752 题目大意:给你一个字符串 \(S\) ,如果它的一个前缀同时也是它的后缀,则输出这个前缀(后缀)的长度. 题目分析:next函数 ...

  7. POJ2406 Power Strings 题解 KMP算法

    题目链接:http://poj.org/problem?id=2406 题目大意:给你一个字符串 \(t\) ,\(t\) 可以表示为另一个小字符串循环了 \(K\) 了,求最大的循环次数 \(K\) ...

  8. HDU2087 剪花布条 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 题目大意:给定字符串 \(s\) 和 \(t\) ,找出 \(s\) 中出现不重叠的 \(t\) ...

  9. HDU1358 Period 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目大意:给你一个长度为 \(n\) 的字符串 \(s\) ,那么它有 \(n\) 个前缀. 对 ...

随机推荐

  1. java-多态-object

    概要图 一 多态 1.1 多态的产生 下面的 红色部分降低了代码的可扩展性 Dog d = new Dog(); method(d); Cat c = new Cat(); method(c); } ...

  2. 小米网关api

    http://bbs.xiaomi.cn/t-13198850 https://github.com/snOOrz/homebridge-aqara/blob/master/README.md htt ...

  3. 解决安装编译工具gcc后无法连接mysql

    在安装编译工具gcc后: yum -y install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-devel libxml2 ...

  4. LintCode刷题笔记-- Edit distance

    标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...

  5. 写论文,没数据?R语言抓取网页大数据

    写论文,没数据?R语言抓取网页大数据 纵观国内外,大数据的市场发展迅猛,政府的扶持也达到了空前的力度,甚至将大数据纳入发展战略.如此形势为社会各界提供了很多机遇和挑战,而我们作为卫生(医学)统计领域的 ...

  6. git解决冲突的最佳方法2

    1.冲突后和远程仓库的文件进行比对的时候,善于用上图所示的按钮,会提高效率 copy all-nonconflicting changes from right to left next differ ...

  7. 使用nuxt.js官方脚手架构建koa2的es6编译问题

    最近在学用nuxt集成koa2做vue后台,发现官方自带脚手架搭建的koa2使用的仍是es5语法,如果想用es6怎么办呢? 这是由于自带脚手架在构建koa2时默认的nodemon是没有使用babel编 ...

  8. linux系统命令配置文件

    系统命令要独占地控制系统,并让一切正常工作.所有如 login(完成控制台用户身份验证阶段)或 bash(提供用户和计算机之间交互)之类的程序都是系统命令.因此,和它们有关的文件也特别重要.这一类别中 ...

  9. 一键制作启动elasticsearch和kibana启动的脚本可执行程序

    1.测试环境 测试环境: . windows10专业版 . elasticsearch6.5.4 . kibana6.5.4 2.启动的脚本run.py import os import time i ...

  10. Django中blank和NULL

    当我们在django中添加一个数据库字段时,我们通常会写models.CharField(max_length = 100,null = True,blank = True).用ForeignKey, ...