kmp

 1 #include <algorithm>
2 #include <iostream>
3 #include <cstring>
4 #include <cstdio>
5
6 using namespace std;
7
8 struct KMP{
9 char y[1010];//主串
10 char x[1010];//模式串
11 int n,m;
12 int next[1010];
13
14 int init(){
15 scanf("%s%s",y,x);
16 n=strlen(y);
17 m=strlen(x);
18 kmp_pre();
19 //prekmp();
20 return 1;
21 }
22
23 void kmp_pre(){//生成next数组
24 int i,j;
25 j=next[0]=-1;
26 i=0;
27 while(i<m){
28 while(-1!=j&&x[i]!=x[j])j=next[j];
29 next[++i]=++j;
30 }
31 }
32
33 /*void prekmp(){
34 int i,j;
35 j=next[0]=-1;
36 i=0;
37 while(i<m){
38 while(-1!=j&&x[i]!=x[j])j=next[j];
39 if(x[++i]==x[++j])next[i]=next[j];
40 else next[i]=j;
41 }
42 }*/
43
44 int kmp_count(){
45 int i,j;
46 int ans;
47 i=j=ans=0;
48 while(i<n){
49 while(-1!=j&&y[i]!=x[j])j=next[j];
50 i++;j++;
51 if(j>=m){
52 ans++;
53 j=next[j];
54 }
55 }
56 return ans;
57 }
58 };
59
60 int main()
61 {
62 KMP a;
63 while(a.init())
64 printf("%d\n",a.kmp_count());
65 return 0;
66 }
67 //如果n%(n-next[n])==0,则存在重复连续子串,长度为n-next[n]。

扩展kmp

 1 #include <algorithm>
2 #include <iostream>
3 #include <cstring>
4 #include <cstdio>
5
6 using namespace std;
7
8 struct KZ_KMP{//求最长公共前缀
9 char y[1010];//主串
10 char x[1010];//模式串
11 int n,m;
12 int next[1010];//next[i]:x[i...m-1]与x[0...m-1]的最长公共前缀
13 int extend[1010];//extend[i]:y[i...n-1]与x[0...m-1]的最长公共前缀
14
15 int init(){
16 scanf("%s%s",y,x);
17 n=strlen(y);
18 m=strlen(x);
19 pre_EKMP();
20 EKMP();
21 return 1;
22 }
23
24 void pre_EKMP(){
25 next[0]=m;
26 int j=0;
27 while(j+1<m&&x[j]==x[j+1])j++;
28 next[1]=j;
29 int k=1;
30 for(int i=2;i<m;i++){
31 int p=next[k]+k-1;
32 int L=next[i-k];
33 if(i+L<p+1)next[i]=L;
34 else{
35 j=max(0,p-i+1);
36 while(i+j<m&&x[i+j]==x[j])j++;
37 next[i]=j;
38 k=i;
39 }
40 }
41 }
42
43 void EKMP(){
44 int j=0;
45 while(j<n&&j<m&&x[j]==y[j])j++;
46 extend[0]=j;
47 int k=0;
48 for(int i=1;i<n;i++){
49 int p=extend[k]+k-1;
50 int L=next[i-k];
51 if(i+L<p+1)extend[i]=L;
52 else{
53 j=max(0,p-i+1);
54 while(i+j<n&&j<m&&y[i+j]==x[j])j++;
55 extend[i]=j;
56 k=i;
57 }
58 }
59 //printf("%d ",extend[0]);
60 }
61 };
62
63 int main()
64 {
65 KZ_KMP a;
66 while(a.init()){
67 }
68 return 0;
69 }

kmp与扩展kmp模板的更多相关文章

  1. Manacher模板,kmp,扩展kmp,最小表示法模板

    *N]; //储存临时串 *N];//中间记录 int Manacher(char tmp[]) { int len=strlen(tmp); ; ;i<len;i++) { s[cnt++]= ...

  2. KMP和扩展KMP【转】

    这种东西基本上在纸上自己推导一下就能做出来XD 转发注明出处 KMP 给出两个字符串A(称为模板串)和B(称为子串),长度分别为lenA和lenB,要求在线性时间内,对于每个A[i] (0<=i ...

  3. KMP与扩展KMP

    原文转自:http://www.cppblog.com/MatoNo1/archive/2011/04/17/144390.aspx KMP:给出两个字符串A(称为模板串)和B(称为子串),长度分别为 ...

  4. KMP && Manacher && 扩展KMP整理

    KMP算法: kmp示例代码: void cal_next(char *str, int *next, int len) { next[0] = -1;//next[0]初始化为-1,-1表示不存在相 ...

  5. KMP 、扩展KMP、Manacher算法 总结

    一. KMP 1 找字符串x是否存在于y串中,或者存在了几次 HDU1711 Number Sequence HDU1686 Oulipo HDU2087 剪花布条 2.求多个字符串的最长公共子串 P ...

  6. 666 专题三 KMP &#38; 扩展KMP &#38; Manacher

    KMP: Problem A.Number Sequence d.求子串首次出现在主串中的位置 s. c. #include<iostream> #include<stdio.h&g ...

  7. KMP和扩展KMP

    文章网上太多这里提一下代码细节: KMP: scanf("%s\n",s); scanf("%s\n",t); int ls=strlen(s),lt=strl ...

  8. 【kmp或扩展kmp】HDU 6153 A Secret

    acm.hdu.edu.cn/showproblem.php?pid=6153 [题意] 给定字符串A和B,求B的所有后缀在A中出现次数与其长度的乘积之和 A和B的长度最大为1e6 方法一:扩展kmp ...

  9. KMP 和 扩展KMP

    KMP:在主串S中找子串T的位置KMP算法的时间复杂度O(|S|+|T|). #define maxn 1000 char s[maxn],t[maxn];//s为主串,t为子串 int net[ma ...

随机推荐

  1. 组件化框架设计之阿里巴巴开源路由框架——ARouter原理分析(一)

    阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680 背景 当项目的业务越来越复杂,业务线越来越多的时候,就需要按照业 ...

  2. Fix invisible cursor issue in Ubuntu 13.10

    Fix invisible cursor issue in Ubuntu 13.10 Fixing this problem is rather too easy. Open a terminal ( ...

  3. 空类的sizeof,有一个虚函数的类的sizeof

    今天面试,忽然被问到这个题目,查了一下果然有欸. #include <iostream> using namespace std; class A { }; class B { publi ...

  4. 40.Unique Binary Search Trees(不同的二叉搜索树)

    Level:   Medium 题目描述: Given n, how many structurally unique BST's (binary search trees) that store v ...

  5. firefox浏览器强制取消自动更新

    问题:Firefox浏览器,在浏览器的设置中已经设置了取消自动升级,实际退出Firefox浏览器重新启动浏览器后还是会升级到最新版本.影响:Firefox浏览器不同的版本的插件的支持兼容不一样,如果需 ...

  6. PL/SQL to update all columns

    undefine schema_name; declare l_Err ); begin for r in (select atc.table_name, atc.column_name, atc.d ...

  7. vue框架中什么是MVVM

    前端页面中使用MVVM的思想,即MVVM是整个视图层view的概念,属于视图层的概念. MVVM是前端视图层的分层开发思想,将页面分成了Model, View,和VM:其中VM是核心,因为VM是V和M ...

  8. CNN基础三:预训练模型的微调

    上一节中,我们利用了预训练的VGG网络卷积基,来简单的提取了图像的特征,并用这些特征作为输入,训练了一个小分类器. 这种方法好处在于简单粗暴,特征提取部分的卷积基不需要训练.但缺点在于,一是别人的模型 ...

  9. css雪碧图实现数字切换

    vue中 css 雪碧图应用及数字切换demo 1. CSS Sprites一般只能使用到固定大小的盒子(box)里,这样才能够遮挡住不应该看到的部分. 2.使用css雪碧图的优点: 利用CSS Sp ...

  10. 阿里云公共DNS正式发布支持IPv6的版本

    在10月23日召开的GNTC 2019全球网络技术大会IPv6分论坛上,阿里云高级技术专家张先国宣布支持阿里公共DNS的IPv6版本正式发布,即阿里公共DNS在保持IPv4 稳定解析服务的基础上(An ...