pid=3746">点击打开链接

题意:给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的更多相关文章

  1. HDU 3746 Cyclic Nacklace (KMP找循环节)

    题目链接:HDU 3746 Sample Input 3 aaa abca abcde Sample Output 0 2 5 Author possessor WC Source HDU 3rd & ...

  2. HDU 3746 Cyclic Nacklace KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3746 KMP算法—— AC代码: #include <iostream> #include ...

  3. HDU 3746 Cyclic Nacklace(kmp next数组运用)

    Cyclic Nacklace Problem Description CC always becomes very depressed at the end of this month, he ha ...

  4. hdu 3746 Cyclic Nacklace(kmp最小循环节)

    Problem Description CC always becomes very depressed at the end of this month, he has checked his cr ...

  5. HDU 3746 Cyclic Nacklace (KMP求循环节问题)

    <题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题]  #include &l ...

  6. hdu 3746 Cyclic Nacklace KMP循环节

    Cyclic Nacklace 题意:给一个长度为Len( 3 <= Len <= 100000 )的英文串,问你在字符串后面最少添加几个字符可以使得添加后的串为周期串? Sample I ...

  7. hdu 3746 Cyclic Nacklace (KMP求最小循环节)

    //len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string. ...

  8. Match:Cyclic Nacklace(KMP的next数组的高级应用)(HDU 3746)

    串珠子 题目大意:给定一个字串,要你找到如果要使之成为循环串,在末尾需要的最小的字数(只能添加字符,不能删减字符) 首先联动一下之前做过的动态规划问题POJ 3280,当然了3280这一题是用的LD, ...

  9. Cyclic Nacklace HDU 3746 KMP 循环节

    Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len ...

随机推荐

  1. HDU 4705 Y (2013多校10,1010题,简单树形DP)

    Y Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submiss ...

  2. How to make a combo box with fulltext search autocomplete support?

    I would like a user to be able to type in the second or third word from a TComboBoxitem and for that ...

  3. 一、 Log4E插件下载

    下载地址:http://log4e.jayefem.de/content/view/3/2/ 二.安装Log4E插件 将下载下来的压缩包解压缩,如下图所示: 解压缩生成的[de.jayefem.log ...

  4. Go语言设计模式实践:组合(Composite)

    关于本系列 这个系列首先是关于Go语言实践的.在项目中实际使用Go语言也有段时间了,一个体会就是不论是官方文档.图书还是网络资料,关于Go语言惯用法(idiom)的介绍都比较少,基本只能靠看标准库源代 ...

  5. Mr.Xu的找实习之路

    长长的路慢慢走 深深的话浅浅说 --广工Mr.Xu的找前端实习之路 这 不(display:none) 是 广 告 本人广工大三学生一枚,也是学校TopView团队的成员之中的一个.之前我们团队有位屌 ...

  6. 可用Active Desktop Calendar V7.86 注册码序列号

    可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code:  DC8EC-16985-F26A6

  7. SharePoint 2013 本地创建解决方案

    在之前的博客<SharePoint 2013本地开发解决方案以及远程调试>中,我们介绍了如何通过修改注册表,使SharePoint 2013 解决方案可以本地编辑,也提及了即使修改注册表, ...

  8. 谈谈Copy-on-Write容器

    1.简介 Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容,当某个人想要修改这个内容的时候,才会真正把内容Copy出去形成一个新的内容 ...

  9. Android内存调试命令

    adb shell dumpsys meminfo 包名 比如: adb shell dumpsys meminfo cn.com.test

  10. poj 1035 Spell checker ( 字符串处理 )

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16675   Accepted: 6087 De ...