HDU 3746 数据结构之KMP
题意:给T组数据,每组一个字符串,问最少加入多少个字符能够使这个串变成一个子串连续出现的串
思路:利用KMP的next数组进行变换,next数组保存的是眼下为止与字符串从头開始的匹配的程度,也能够看成从头開始的位置。所以假设Next数组最后一个为0,则须要在加入一个这种串才干匹配成功。不然ans=len-Next[len]代表的是不能匹配的后面的串的长度。假设这个长度能够被len取余,则说明这个串已经匹配好了,不然就是这个长度减去取余后的数
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=200010;
const int mod=10007;
char str1[maxn];
int Next[maxn];
void makenext(int m){
int i=0,j=-1;
Next[i]=-1;
while(i<m){
if(j==-1||str1[i]==str1[j])
Next[++i]=++j;
else j=Next[j];
}
}
//int KMP(int n,int m){
// int i=0,j=0,ans=0;
// while(i<n){
// if(j==-1||str2[i]==str1[j]) i++,j++;
// else j=Next[j];
// if(j==m){
// ans++;j=Next[j-1];i--;
// }
// }
// return ans;
//}
int main(){
int T,n;
scanf("%d",&T);
while(T--){
scanf("%s",str1);
int len=strlen(str1);
makenext(len);
if(Next[len]==0){
printf("%d\n",len);
continue;
}
int ans=len-Next[len];
if(len%ans==0) printf("0\n");
else printf("%d\n",ans-len%ans);
}
return 0;
}
HDU 3746 数据结构之KMP的更多相关文章
- HDU 3746 Cyclic Nacklace (KMP找循环节)
题目链接:HDU 3746 Sample Input 3 aaa abca abcde Sample Output 0 2 5 Author possessor WC Source HDU 3rd & ...
- HDU 3746 Cyclic Nacklace KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3746 KMP算法—— AC代码: #include <iostream> #include ...
- HDU 3746 Cyclic Nacklace(kmp next数组运用)
Cyclic Nacklace Problem Description CC always becomes very depressed at the end of this month, he ha ...
- hdu 3746 Cyclic Nacklace(kmp最小循环节)
Problem Description CC always becomes very depressed at the end of this month, he has checked his cr ...
- HDU 3746 Cyclic Nacklace (KMP求循环节问题)
<题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题] #include &l ...
- hdu 3746 Cyclic Nacklace KMP循环节
Cyclic Nacklace 题意:给一个长度为Len( 3 <= Len <= 100000 )的英文串,问你在字符串后面最少添加几个字符可以使得添加后的串为周期串? Sample I ...
- hdu 3746 Cyclic Nacklace (KMP求最小循环节)
//len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string. ...
- Match:Cyclic Nacklace(KMP的next数组的高级应用)(HDU 3746)
串珠子 题目大意:给定一个字串,要你找到如果要使之成为循环串,在末尾需要的最小的字数(只能添加字符,不能删减字符) 首先联动一下之前做过的动态规划问题POJ 3280,当然了3280这一题是用的LD, ...
- Cyclic Nacklace HDU 3746 KMP 循环节
Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len ...
随机推荐
- Using an open debug interconnect model to simplify embedded systems design
Using an open debug interconnect model to simplify embedded systems design Tom Cunningham, Freescale ...
- hihoCoder - 1082 - 然而沼跃鱼早就看穿了一切 (字符串处理!!)
#1082 : 然而沼跃鱼早就看穿了一切 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描写叙述 fjxmlhx每天都在被沼跃鱼刷屏,因此他急切的找到了你希望你写一个程序屏蔽全 ...
- VB.net X86设置
在Visual Studio中,在任何项目中点击右键,选择项目属性->编译->高级编译选项
- .NET:C#的匿名委托 和 Java的匿名局部内部类
背景 这几天重温Java,发现Java在嵌套类型这里提供的特性比较多,结合自身对C#中匿名委托的理解,我大胆的做了一个假设:Java的字节码只支持静态嵌套类,内部类.局部内部类和匿名局部内部类都是编译 ...
- [Lua]mac 上安装lua
mac 安装lua google了好个看起来都不怎么好操作.这个是在命令行下操作的非常easy. curl -R -O http://www.lua.org/ftp/lua-5.2.3.tar.gz ...
- 第一章 Actionscript学习基本知识笔记及flashdevelop软件的安装问题
OOP:封装.继承.多态. Pubilc :完全公开. Internal:包内类成员可以互相访问. Private:仅当前类可以访问. Protected:当前类和当前类的子类可以访问. 被关键词fi ...
- [Android Pro] 有关Broadcast作为内部类时注册的一些问题
很经常Broadcast都会写成一个Activity或者Service的内部类.这时候的注册和普通有点小区别. 有两种情况 1.假如是再Manifest文件里面静态注册的话,需要注意. ex: < ...
- AlphaGo:用机器学习技术古老的围棋游戏掌握AlphaGo: Mastering the ancient game of Go with Machine Learning
AlphaGo: Mastering the ancient game of Go with Machine Learning Posted by David Silver and Demis Has ...
- Informatica 常用组件Source Qualifier之四 SQL Query
源限定符转换提供 SQL 查询选项以覆盖默认的查询.您可以输入您的源数据库支持的 SQL 语句.输入查询之前,请连接您要在映射中使用的所有输入和输出端口. 编辑 SQL 查询时,您可以生成并编辑默认查 ...
- MYSQL错误1130:ERROR 1130: Host 10.10.36.115 is not allowed to connect to this MySQL server
解决远程连接mysql错误1130代码的方法 在用远程连接Mysql服务器的数据库,不管怎么弄都是连接不到,错误代码是1130,ERROR 1130: Host 10.10.36.115 is no ...