相比一般KMP,构建next数组需要多循环一次,因为next[j]代表前j-1个字符的最长相同前缀后缀,比如字符串为aab aab aab共9个字符,则next[10]等于前9个字符中最长相同前缀后缀长度,如果字符串是由某个子串比如aab循环而形成的,则next[10]就会等于(字符串长度len-循环子串长度).但是需要注意aab aab aa这种情况,算出来结果是不能被len整除的,需要特判 #include <iostream> #include <cstdio> #inclu…
Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33178   Accepted: 13792 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "…
转载自:https://www.cnblogs.com/chenxiwenruo/p/3546457.html KMP模板,最小循环节   下面是有关学习KMP的参考网站 http://blog.csdn.net/yaochunnian/article/details/7059486 http://blog.csdn.net/v_JULY_v/article/details/6111565 http://blog.csdn.net/v_JULY_v/article/details/6545192…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description CC always becomes very depressed at the end of this month, he has checked his credit card yesterd…
题目链接:http://poj.org/problem?id=2406 题意:确定字符串最多是多少个相同的字串重复连接而成的 思路:关键是找到字符串的最小循环节 code: #include <cstdio> #include <cstring> ; char s[MAXN]; int next[MAXN]; void GetNext() { int len = strlen(s); ; ; next[] = -; while (i < len) { == j || s[i]…
//len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int len; char a[100010]; int next[100010]; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<=len) { if(j==-1||a[i]==a[j]) i…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目描述: 给出一个字符串S,输出S的前缀能表达成Ak的所有情况,每种情况输出前缀的结束位置和k. 解题思路: 打表算出next数组,然后对位置i求循环节,如果满足 i % (i - Next[i]) == 0 && Next[i] != 0,所对应的循环节(i - Next[i]), 循环次数是i / (i - Next[i]) #include<cstdio> #inc…
题目链接:http://poj.org/problem?id=2406 Time Limit: 3000MS Memory Limit: 65536K Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we thi…
Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 13511   Accepted: 6368 Description For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the…
题目链接:https://cn.vjudge.net/problem/HDU-3746 题意 给一串珠子,我们可以在珠子的最右端或最左端加一些珠子 问做一条包含循环珠子的项链,最少还需要多少珠子 思路 KMP的另一个用法,求最小循环节minloop=len-fail[len] 用我的观点来看KMP的fail数组,就是值域和定义域都是串的长度,返回值是这个串能够匹配后缀的最大前缀串长度 但是纯循环节构成的串中,这个返回值不包括第一个循环节 比如aabaabaab fail[9]==6 fail[6…