hdu 3746 kmp的next数组理解
题目大意:
求最少在结尾补上几个字符才能形成循环
基本思路:
next数组有一个性质,长度为len的字符串的最小长度的循环节(可能没有,但有的话一定是)len-next【len】,因为最长不能是原串,所以最长为next【len】,所以最短的,也就是最基本的循环节就是len-next[【len】;
对于这个题,当next【len】不是0(即len=循环节长度tmp),而且长度len%tmp=0时,结果是0;否则结果为tmp-len%tmp,其中len%tmp表示出去多少个循环后还剩几个字符;
代码如下
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; const int inf = 0x3f3f3f3f;
const int maxn = 100000+10; char T[maxn];
int _next[maxn];
int len;
void getNext(){
len=strlen(T);
int j=0,k=-1;
_next[0]=-1;
while(j<len){
if(k==-1||T[j]==T[k]){
_next[++j]=++k;
}else{
k=_next[k];
}
}
}
int main(){
int cas;
scanf("%d",&cas);
while(cas--){
scanf("%s",T);
getNext();
int tmp=len-_next[len];
if(tmp!=len&&len%tmp==0) printf("0\n");
else{
printf("%d\n",tmp-len%tmp);
}
}
return 0;
}
hdu 3746 kmp的next数组理解的更多相关文章
- Cyclic Nacklace HDU 3746 KMP 循环节
Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len ...
- Cyclic Nacklace HDU - 3746 (kmp)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 3746 kmp求循环节
题意就是将所给的字符串变成多个完整的循环(至少两个),然后给出最少需要添加的字符数.
- HDU 3746 (KMP求最小循环节) Cyclic Nacklace
题意: 给出一个字符串,要求在后面添加最少的字符是的新串是循环的,且至少有两个循环节.输出最少需要添加字符的个数. 分析: 假设所给字符串为p[0...l-1],其长度为l 有这样一个结论: 这个串的 ...
- HDU 3746
http://acm.hdu.edu.cn/showproblem.php?pid=3746 kmp的Nxet数组求字符串循环节例题 lenB%(lenB-Next[lenB])==0则其有周期len ...
- Match:Cyclic Nacklace(KMP的next数组的高级应用)(HDU 3746)
串珠子 题目大意:给定一个字串,要你找到如果要使之成为循环串,在末尾需要的最小的字数(只能添加字符,不能删减字符) 首先联动一下之前做过的动态规划问题POJ 3280,当然了3280这一题是用的LD, ...
- HDU 3746 Cyclic Nacklace KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3746 KMP算法—— AC代码: #include <iostream> #include ...
- HDU 3746 数据结构之KMP
pid=3746">点击打开链接 题意:给T组数据,每组一个字符串,问最少加入多少个字符能够使这个串变成一个子串连续出现的串 思路:利用KMP的next数组进行变换,next数组保存的 ...
- HDU 3746 Cyclic Nacklace (KMP找循环节)
题目链接:HDU 3746 Sample Input 3 aaa abca abcde Sample Output 0 2 5 Author possessor WC Source HDU 3rd & ...
随机推荐
- 第12篇Kubernetes 监控
一.Weave Scope: Weave Scope 容器地图 创建 Kubernetes 集群并部署容器化应用只是第一步.一旦集群运行起来,我们需要确保一起正常,所有必要组件就位并各司其 ...
- Vue2 实现时空穿梭框功能模块
前言 这篇文章主要是分享一个时空穿梭框功能,也就是我们平时用的选择功能.勾选了的项就会进入到另一个框中. 时空穿梭框之旅 示例演示: 这个时空穿梭框实现了: 1.可以全选.反选 2.没有选中时,不可以 ...
- 用私有构造器或者枚举类型强化SingleTon(单例)属性
单例(singleton)就是一个只实例化一次的类.使类成为单例可能会使它的测试变得困难,因为除非它实现了作为其类型的接口,否则不可能用模拟实现来代替这个单例.下面是几种实现单例的方法: 1.共有静态 ...
- SP2-0618: Cannot find the Session Identifier.
[oracle@trade1 ~]$ sqlplus user1/user1 SQL*Plus: Release 11.2.0.3.0 Production on Tue Aug 6 14:31:1 ...
- 15.stop引发的数据不一致
/** * 数据不一致问题 * stop */ public class StopDemo { public static Student student = new Student(); publi ...
- Delphi 字符串函数SysUtils单元 AnsiSameStr、AnsiSameText、AnsiCompareStr、AnsiCompareText、AnsiCompareFileName、AnsiUpperCase、AnsiLowerCase、AnsiUpperCaseFileName、AnsiLowerCaseFileName、AnsiPos、AnsiQuotedStr
USES 单元 SysUtils 非 StrUtils AnsiSameStr.AnsiSameText.AnsiCompareStr.AnsiCompareText.AnsiCompareFileN ...
- UVA 10529 - Dumb Bones (概率dp)
题目描述 You are trying to set up a straight line of dominos, standing on end, to be pushed over later f ...
- Migrating Your Android App from Eclipse to Android Studio
By: Jason Snell | Posted in: Mobile, Performance Tech Tips, Top Post, Jun. 7th, 2013 12 inShare One ...
- MySQL date_sub 和 date_add 函数
DATE_SUB: 定义和用法 DATE_SUB() 函数从日期减去指定的时间间隔. 语法 DATE_SUB(date,INTERVAL expr type) date 参数是合法的日期表达式.exp ...
- Eclipse Missing artifact jdk.tools:jdk.tools:jar:1.6
Missing artifact jdk.tools:jdk.tools:jar:1.6 问题出在Eclipse Maven的支持上,在Eclipse下,java.home变量设置为用于启动Eclip ...