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. NSArray与NSMutableArray 数组与可变数组的创建和遍历 复习

    1.NSArray 是一个父类,NSMUtableArray是其子类,他们构成了OC的数组. 2.NSArray的创建 NSArray * array = [[NSArray alloc]initWi ...

  2. UIColor和 同 CIColor 与 CGColor 之间的联系、转换

    1. 利用UIColor展现 #F6F6F6 这个传统的颜色 #F6F6F6 为一个 16 进制表示的RPG颜色,所以,需要先转换成 10进制,其中 F6 - 240,F6 -  240 ,F6 - ...

  3. JavaScript中数组的各种操作方法

    [监测数组] 使用instanceof操作符,进行检测 ar arr = [1,2,3]; // arr = '非非'; if(arr instanceof Array){ console.log(' ...

  4. sourceinsight使用技巧

    转:http://blog.csdn.net/flyyanqu/article/details/2222799 目录(?)[-] 配置成简单好用的cjava代码编辑器 缩进与tab 向项目中添加文件时 ...

  5. 关于String 后面跟省略号。。。

    今天阅读MonkeyRunner源码的时候发现下面一段: private String shell(String... args) { StringBuilder cmd = new StringBu ...

  6. MySql_SQLyog+SQL Assistant实现智能提示

    相信用过sqlserver+SQL Assistant的同学都知道其智能提示多么方便,但是转到mysql后,无论是使用navicat还是webbench都无法实现较好的智能提示效果, 最终在网上找到使 ...

  7. 介紹 IIS 8 全新的 HttpPlatformHandler 模組與 ASP.NET 5 Beta8 重大變更

    HttpPlatformHandler 是一個支援 IIS 8 與 IIS 8.5 的原生模組 (native module),主要使用於 Microsoft Azure Websites 網站服務中 ...

  8. Computer Vision Tutorials from Conferences (2) -- ECCV

    ECCV 2012 (http://eccv2012.unifi.it/program/tutorials/) Vision Applications on Mobile using OpenCVGa ...

  9. Windows Server 2012 R2搭建IIS服务器

    1-单击宫格菜单的第一个“服务器管理器”: 2 2-在“快速启动(Q)”子菜单下,单击“2 添加角色和功能”: 3 3-点击左边“安装类型”,然后单击“基于角色或基于功能的安装”,再单击“下一步(N) ...

  10. Power Desginer系列00【转载】

    绪论 Sybase PowerDesigner(简称PD)是最强大的数据库建模工具,市场占有率第一,功能也确实十分强大,现在最新版本是15.1,已经支持最新的SQL Server 2008等数据库,另 ...