Hdu 1867 KMP】的更多相关文章

贴两道题,其中HDU2087是中文题,故不解释题目, 思路是,一发KMP,但是特别处理最后一位的失配边为0,这样就可以保证“判断完成但是不多判断”. 第二题,很毒瘤的题,要求求出,给定字符串A,B能够缠到一起组成的子字符串长度“长度较小且字典序较小”的一个....要求,假设str1+str2组成答案,则str1的后缀和str2的前缀中相同的部分,只出现一次..于是做法就是,两法KMP,特判答案咯...然而....此题..最有难度的地方是读懂提....看了别人的提解读懂得.... 2087AC代码…
题目链接 题目意思: 给出两个字符串a, b, 求最长的公共字串c, c是a的后缀,也是b的前缀. 本题没有具体说明哪个字符串是文本串和匹配串, 所以都要考虑 思路: 查找的时候, 当文本串结束的时候, 返回匹配串的位 /************************************************************************* > File Name: 1867.cpp > Author: Stomach_ache > Mail: sudaweit…
#include<stdio.h> #include<string.h> #define N 100100 void getnext(int next[],char s[]) {     int k=-1,j=0; next[0]=-1; while(s[j]!=0) { if(k==-1||s[j]==s[k]) { j++;k++; if(s[j]!=s[k]) next[j]=k; else next[j]=next[k]; } else k=next[k]; } } int…
// hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; ; ; char T[MAX_N]; char p[MAX_M]; int f[MAX_M]; int n,m; void getfail(){ f[] = f[] = ; ;i&l…
Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len]-len的差即是循环部分的长度. 这个是重点.这个题目自己开始没有想明白,看的博客,推荐这个. 代码实现 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const i…
题意: 给你两个字符串,输出他们合并之后的字符串,合并的时候把A的后缀和B的前缀重叠合(或者把A的前缀和B的后缀重合).要求合并后的串既包含A右包含B, 且使得合并后的字符串尽量短,其次是使得合并后的字符串字典序尽量小. 分析: 首先A和B合并他们一定是首尾重叠相连,要求合并后字典序最小,所以当合并后串长度一样时,我们要把A或B中字典序小 的放在前面. 然后计算A的后缀和B的前缀匹配长度为len1,计算A的前缀和B的后缀匹配长度为len2. 如果len1大,那么就把A放前面.如果len2大,那么…
这是一个典型问题KMP申请书. 结果求增加两个字符串.该法的总和是相同的前缀和后缀也是字符串的字符串,您将可以合并本节. 但是,这个问题是不是问题非常明确的含义,因为不是太清楚,外观这两个字符串的顺序无关紧要,后只需要输出的最短的组合长度的结果,并后长度一样,那么就依照字典顺序,输出字典顺序在前的字符串. 思路: 1 使用kmp在s2查找s1,那么终于结束的时候next table的值就是s1前缀和s2的后缀同样的最长的长度了. 2 输入两个字符串s1和s2.那么就能够在s2中查找s1.得到长度…
Problem Description Generally speaking, there are a lot of problems about strings processing. Now you encounter another such problem. If you get two strings, such as “asdf” and “sdfg”, the result of the addition between them is “asdfg”, for “sdf” is…
题意:       给你两个字符串,让你求str1+str2,就是把1的后面和2的前面重叠的地方只显示一遍就行了 abc + bcd = abcd,要求和的长度最小,和最小的前提下求字典序最小,还有就是两个串可以交换位置的,cdab + abcd = abcdab 而不是 cdabcd,交换位置后合并是最短并且字典序最小的. 思路:       首先得到两个next数组,然后用str1 去匹配 str2,再用str2 去匹配str1,(因为可以交换),分别得到子串走到最后时匹配串的位置,x,y,…
A + B for you again Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4496    Accepted Submission(s): 1157 Problem Description Generally speaking, there are a lot of problems about strings process…
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1867 A + B for you again Description Generally speaking, there are a lot of problems about strings processing. Now you encounter another such problem. If you get two strings, such as “asdf” and “sdfg”, t…
题意: 用两个字符串分别表示布条和图案,问能从该布条上剪出多少这样的图案. 分析: 毫无疑问这也是用KMP匹配,关键是一次匹配完成后,模式串应该向后滑动多少. 和上一题 HDU 1686 不同,两个图案肯定不能在母串中有交叉的部分,所以当匹配成功一次后,应当滑动整个模式串的长度. 和上一题比,代码几乎不变,只是 j = next[j]; 变为 j = 0; #include <cstdio> #include <cstring> + ; char p[maxn], q[maxn];…
http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 30591    Accepted Submission(s): 12870 Problem Description Given two sequences of…
http://acm.hdu.edu.cn/showproblem.php?pid=2203 亲和串 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15601    Accepted Submission(s): 6895 Problem Description 人随着岁数的增长是越大越聪明还是越大越笨,这是一个值得全世界科学家思考的问…
http://acm.hdu.edu.cn/showproblem.php?pid=2087 算是模板题吧,找到一个子串之后将模板串指针归零否则会重复计算. #include<bits/stdc++.h> using namespace std; ]; ],t[]; void kmp() { int szs=strlen(s),szt=strlen(t),i,j,k; nex[]=nex[]=; ;i<szt;++i) { j=nex[i]; while(j&&t[i]!…
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15406    Accepted Submission(s): 6419 Problem Description CC always becomes very depressed at the end of this month, he has checke…
题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串长度". 当发生失配的情况下,j的新值next[j]取决于模式串中T[0 ~ j-1]中前缀和后缀相等部分的长度, 而且next[j]恰好等于这个最大长度. 防止超时.注意一些细节.. 另外:尽量少用strlen.变量记录下来使用比較好,用字符数组而不用string //KMP算法模板题 //hdu…
hdu 3336 题意:输入一个字符串求每个前缀在串中出现的次数和 sol:只要稍微理解下next 数组的含义就知道只要把每个有意义的next值得个数加起来即可 PS:网上有dp解法orz,dp[i]表示以i为前缀串结尾的前缀串的总和,方程很容易写出 ; ; ]=next[]=;         ;i<n;i++)         {             ]=j+;             ]=;         }         ,cnt=;         ;i<n;i++)    …
找AEAEA形式的字符串最长的A长度,E可以为空 只可意会,不可言传,懂kmp即可 #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string&…
题意: 求模板在匹配串所有子串中出现次数. SOL: 本题与普通kmp有一点不同,因为待匹配串中的模板串可能相互包含. 我们考虑正常的kmp是在怎么做的 i = 1 2 3 4 5 6 7 8 9 ……    A = a b a b a b a a b a b …    B = a b a b a c b     j = 1 2 3 4 5 6 7 i=j=6时两个字符串不同了,那么j要减小到上一个匹配的地方. 当匹配完了呢? 比如 i = 1 2 3 4 5 6   A= a z a z a…
s为主串 t为模板串 求t的nextt 加const #include<stdio.h> #include<string.h> #include<algorithm> #include<map> #include<math.h> #include<queue> using namespace std; char s[1005]; char t[1005]; int nextt[1005]; void makenext(const ch…
Sequence I Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1013    Accepted Submission(s): 393 Problem Description Mr. Frog has two sequences a1,a2,⋯,an and b1,b2,⋯,bm and a number p. He wants t…
题意: 求模式串W在母串T中出现的次数,各个匹配串中允许有重叠的部分. 分析: 一开始想不清楚当一次匹配完成时该怎么办,我还SB地让i回溯到某个位置上去. 后来仔细想想,完全不用,直接让模式串向前滑动,即 j = next[j] #include <iostream> #include <cstdio> #include <cstring> using namespace std; + ; + ; char W[maxn1], T[maxn2]; int next[ma…
题意:说实话这个题的题意还真的挺难懂的,我开始看了好久都没看懂,后来百度了下题意才弄懂了,这题的意思就是首先有一个字母的转换表,就是输入的第一行的字符串,就是'a'转成第一个字母,'b'转成转换表的第二个字母·······然后下面一个字符串是密文+明文的形式的字符串.就是说前后两段是重复的,只不过通过转换表转换了下.而且后面一段可能不完整,让我们补完整. 思路:这道题我很久之前就已经a掉了,当时是用普通的kmp做的,稍微变通下就行了.只不过现在正在学习扩展kmp,所以就用扩展kmp做了下,个人觉…
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<cmath> using namespace std; #define Maxn 1000010 char s[Maxn]; int a[Maxn],nt[Maxn]; int l; void…
Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book: Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair…
题目大意:求模式串在主串中的出现次数. 题目思路:KMP模板题 #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<stdio.h> #include<stdlib.h> #include<queue> #include<math.h> #include<map> #define INF…
题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<stdio.h> #include<stdlib.h> #include<queue> #include<m…
解题报告:给你两个字符串,让你连接起来,没有前后顺序,要求是长度最短优先,其次是字典序最小.这题我用的是KMP,做两次匹配,分别把第一次跟第二次输入的字符串放前面,然后比较两次得到的字符窜的长度和字典序. #include<cstdio> #include<cstring> +; //因为如果两个加一起就有可能超出了,干脆开两倍的数组 int next[MAX]; void get_next(const char *t) { next[] = -; int len = strlen…
题目链接 题意:给定两个字符串s1,s2,求最长的s1前缀s使得s为s2的最长后缀,输出该字符串和其长度. 题解:调换s1和s2的顺序,用KMP求解即可. #include <bits/stdc++.h> using namespace std; ; char s1[N],s2[N]; int len1,len2; int next_val[N]; void getnext_val() { ,j=-; next_val[i]=-; while(i<len2){ ||s2[i]==s2[j…