kmp

P3375 KMP(字符串)

孤者浪人 提交于 2020-01-22 14:55:13
1 #include<iostream> 2 using namespace std; 3 const int N=1000000+100; 4 int f[N]; 5 int main(void) 6 { 7 string s1,s2; 8 cin>>s1>>s2; 9 f[0]=-1;//f[0]置为-1,好判边界 10 int len2=s2.length(); 11 //因为f[0]=-1,所以f数组每个都比实际值小一 12 for(int i=1;i<len2;i++)//计算后len2-1的f值 13 { 14 int j=f[i-1];//f[i-1]表示前i-1个,前后缀相同的最长是多少,而我们要求的时前i个,只需判断第i个和f[i-1]+1是否相同 15 while(s2[j+1]!=s2[i]&&j>=0)//这个循环只是为了让长度为一的前后缀正确,试试ECAEE就知道了 16 { 17 j=f[j]; 18 } 19 if(s2[j+1]==s2[i]) 20 f[i]=j+1; 21 else 22 f[i]=-1; 23 } 24 //以上计算f数组 25 int len1=s1.length(); 26 for(int i=0,j=0;i<len1;)//使用f数组进行kmp匹配,至于为什么可以不回溯的证明我是真的看不懂 27 { 28 if(s1[i]

扩展kmp模板

只愿长相守 提交于 2020-01-22 07:26:32
参考博客: https://wenku.baidu.com/view/8e9ebefb0242a8956bece4b3.html      http://www.cnblogs.com/Rlemon/archive/2013/06/26/3157574.html 代码: 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 6 const int MAXN = 1e5 + 10; 7 char str1[MAXN], str2[MAXN]; //str1为主串,str2为模式串 8 int exnext[MAXN], extend[MAXN], n, m; 9 10 void get_exnext(void){ 11 exnext[0] = m; 12 int j = 0, k = 1; //k是使p最大的i 13 while(j + 1 < m && str2[j] == str2[j + 1]) j++; 14 exnext[1] = j; 15 for(int i = 2; i < m; i++){ 16 int p = exnext[k] + k - 1; //p为当前匹配最远的位置 17 int l = exnext[i - k]; //[k,p

KMPlayer

偶尔善良 提交于 2020-01-21 15:04:25
1、 KMP 播放时,有声无图像,黑屏。解决: 打开KMP然后右键-选项-参数设置(或者进入KMP直接按F2)-视频处理-右边选择“渲染器”-在“渲染器”中选择“VMR9 未渲染 (HQ字幕)”-选中“强制选中VMR未渲染模式中使用RGB32(HQ)”-->关闭 KMP,重启 即可。 2、 KMPlayer黑屏: “高级视频管理→视频渲染器装置”设置项中启用“增强型渲染”(旧的视频渲染器)功能即可解决问题。 还不行就在视频(高级)-重设尺寸-总是 C 来源: https://www.cnblogs.com/osskill/p/5178543.html

扩展kmp算法

两盒软妹~` 提交于 2020-01-21 00:26:44
扩展kmp算法 参考链接: 扩展kmp算法 **问题定义:**给定俩个字符串,文本串S,模式串T,长度分别为n和m,下标从0开始定义。定义Extend[]数组,Extend[i] 等于 S[i]…S[n-i] 与 T 的最长相同的前缀长度,现在问题是求出所有Extend[i]。举个例子,看下表: i 0 1 2 3 4 5 6 7 S a a a a a b b b T a a a a a c Extend[i] 5 4 3 2 1 0 0 0 来源: CSDN 作者: 放过@ 链接: https://blog.csdn.net/wxy2635618879/article/details/104048878

从 LeetCode_28_strStr() 到 KMP 算法

怎甘沉沦 提交于 2020-01-19 01:01:07
这些天一直在回顾 LeetCode 上刷过的题,当看到第 28 题实现 strStr() 时,我发现虽然它是简单题,可以使用 Java 的 indesOf() 和暴力遍历轻松的解答,但是更好也更为符合实际的应该是用 KMP 算法 。关于什么是 KMP 算法,大家可以自己去 google,或者参考 严蔚敏 老师的《数据结构》。 这里我要分享的只是简单的实现以及其它大佬的解题思路(因为我现在对 KMP 算法理解的也不够深入)。 KMP 算法的大体思路可以参考 KMP 字符串匹配算法 个人认为视频讲解比画图讲解会好一点,毕竟视频是动态的。 KMP 算法中的重难点是 next 数组的生成以及具体实现中模式串匹配。具体可以参考一下文章。珠玉在前,我就不分享自己还不够清晰完整的思路了。等我有了更为深入的理解后会来补充这一部分。 从头到尾理解 KMP 深入了解 KMP 算法中的 next 数组 以下是我关于 LeetCode 上的第 28 道题来具体实现 KMP 算法代码 public int strStr(String haystack, String needle) { if(needle == null || needle.length() == 0) { return 0; } int[] next = new int[needle.length()]; // 得到 next 数组

KMP模式匹配

我的梦境 提交于 2020-01-18 19:34:26
A[1~N]是否为B[1~M]的子串 next[1]=0; for(int i=2,j=0;i<=n;i++) { while(j>0&&a[i]!=a[j+1]) j=next[j]; if(a[i]==a[j+1]) j++; next[i]=j; } for(int i=1,j=0;i<=m;i++) { while(j>0&&(j==n||b[i]!=a[j+1])) j=next[j]; if(b[i]==a[j+1]) j++; f[i]=j; //if(f[i]==n) A在B中出现一次 } 来源: https://www.cnblogs.com/ChildeZhe/p/12209931.html

kmp(循环节)

∥☆過路亽.° 提交于 2020-01-18 07:49:37
Cyclic Nacklace Problem Description CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspired by the entrepreneurial spirit of "HDU CakeMan", he wants to sell some little things to make money. Of course, this is not an easy task. As Christmas is around the corner, Boys are busy in choosing christmas presents to send to their girlfriends. It is believed that chain bracelet is a good choice. However, Things are not

[Codeforces1137B]Camp Schedule(KMP)

北城余情 提交于 2020-01-17 21:57:41
[Codeforces1137B]Camp Schedule(KMP) 题面 给出两个01串S和T,要求把S重新排列,使得T在S中出现的次数最多。求重新排列后的S 分析 用贪心的思想,尽量用T首尾相连的去凑出S。比如S="00111",T="101",那么我们可以把两个101拼在一起,相同的前后缀合并,变成10101 发现要求公共前后缀,我们想到KMP。先对T跑一遍KMP求出nex数组,然后用cnt数组统计S中0,1出现次数。 我们记录当前T匹配到的位置j,如果S里还有足够的字符T[j],那就把这一位设为T[j]并--cnt[T[j]],否则结束。当j匹配到T末尾的时候要把j变成nex[j],表示重新匹配下一个,nex[j]即为前后缀重复部分。最后如果有剩下的0和1要全部输出。 代码 #include<iostream> #include<cstdio> #include<cstring> #define maxn 500000 using namespace std; int n,m; char s[maxn+5],t[maxn+5]; void get_nex(char* s,int n,int* nex){ nex[1]=0; for(int i=2,j=0;i<=n;i++){ while(j&&s[j+1]!=s[i]) j=nex[j]; if(s[j+1]==s[i]

KMP循环节

这一生的挚爱 提交于 2020-01-16 08:40:20
链接: https://www.nowcoder.com/acm/contest/141/E 题目描述 Eddy likes to play with string which is a sequence of characters. One day, Eddy has played with a string S for a long time and wonders how could make it more enjoyable. Eddy comes up with following procedure: 1. For each i in [0,|S|-1], let S i be the substring of S starting from i-th character to the end followed by the substring of first i characters of S. Index of string starts from 0. 2. Group up all the S i . S i and S j will be the same group if and only if S i =S j . 3. For each group, let L j be the list of index i in non-decreasing

poj1961Period【kmp next数组】

痞子三分冷 提交于 2020-01-16 02:01:16
大意:跟poj2406一样的题 思路见 http://www.cnblogs.com/zhanzhao/p/4761477.html 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn = 1000005; 7 8 int next[maxn]; 9 10 void get(char *s) { 11 int l = strlen(s); 12 int j = 0, k = -1; 13 next[0] = -1; 14 while(j < l) { 15 if(k == -1 || s[j] == s[k]) { 16 17 18 19 // if(s[++j] == s[++k]) { 20 // next[j] = next[k]; 21 // } else { 22 // next[j] = k; 23 // } 24 next[++j] = ++k; 25 } else { 26 k = next[k]; 27 } 28 } 29 } 30 char s[maxn]; 31 32 int main() { 33 int n; 34 int kase = 1; 35 while(scanf("%d",&n) &