kmp:

KMP的主要目的是求B是不是A的子串,以及若是,B在A中所有出现的位置

写的很详细的大佬的博客:http://www.matrix67.com/blog/archives/115

模板:

/*
pku3461(Oulipo), hdu1711(Number Sequence)
这个模板 字符串是从0开始的
Next数组是从1开始的 */
#include <iostream>
#include <cstring>
using namespace std; const int N = ;
int next[N];
char S[N], T[N];
int slen, tlen; void getNext()
{
int j, k;
j = ; k = -; next[] = -;
while(j < tlen)
if(k == - || T[j] == T[k])
next[++j] = ++k;
else
k = next[k]; }
/*
返回模式串T在主串S中首次出现的位置
返回的位置是从0开始的。
*/
int KMP_Index()
{
int i = , j = ;
getNext(); while(i < slen && j < tlen)
{
if(j == - || S[i] == T[j])
{
i++; j++;
}
else
j = next[j];
}
if(j == tlen)
return i - tlen;
else
return -;
}
/*
返回模式串在主串S中出现的次数
*/
int KMP_Count()
{
int ans = ;
int i, j = ; if(slen == && tlen == )
{
if(S[] == T[])
return ;
else
return ;
}
getNext();
for(i = ; i < slen; i++)
{
while(j > && S[i] != T[j])
j = next[j];
if(S[i] == T[j])
j++;
if(j == tlen)
{
ans++;
j = next[j];
}
}
return ans;
}
int main()
{ int TT;
int i, cc;
cin>>TT;
while(TT--)
{
cin>>S>>T;
slen = strlen(S);
tlen = strlen(T);
cout<<"模式串T在主串S中首次出现的位置是: "<<KMP_Index()<<endl;
cout<<"模式串T在主串S中出现的次数为: "<<KMP_Count()<<endl;
}
return ;
}
/*
test case
aaaaaa a
abcd d
aabaa b
*/

扩展kmp:

给出模板串S和串T,长度分别为Slen和Tlen,要求在线性时间内,对于每个S[i](0<=i<Slen),求出S[i..Slen-1]与T的

最长公共前缀长度,记为extend[i](或者说,extend[i]为满足S[i..i+z-1]==T[0..z-1]的最大的z值)。

扩展KMP可以用来解决很多字符串问题,如求一个字符串的最长回文子串和最长重复子串。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int cnt[];
char t[],p[];
int Next[],ex[];
void pre(char p[]) // next[i]: 以第i位置开始的子串 与 T的公共前缀
{
int m=strlen(p);
Next[]=m;
int j=,k=;
while(j+<m&&p[j]==p[j+]) j++;
Next[]=j;
for(int i=; i<m; i++)
{
int P=Next[k]+k-;
int L=Next[i-k];
if(i+L<P+) Next[i]=L;
else
{
j=max(,P-i+);
while(i+j<m&&p[i+j]==p[j]) j++;// 枚举(p+1,length) 与(p-k+1,length) 区间比较
Next[i]=j;
k=i;
}
}
}
void exkmp(char p[],char t[])
{
int m=strlen(p),n=strlen(t);
pre(p); //next[]数组初始化
int j=,k=;
while(j<n&&j<m&&p[j]==t[j]) j++;
ex[]=j;
for(int i=;i<n;i++)
{
int P=ex[k]+k-;
int L=Next[i-k];
if(i+L<P+) ex[i]=L;
else
{
j=max(,P-i+);
while(i+j<n&&j<m&&t[i+j]==p[j]) j++;
ex[i]=j;
k=i;
}
}
}
int main()
{
int ss;
scanf("%d",&ss);
while(ss--)
{
scanf("%s %s",&t,&p);
pre(p); //处理next数组
exkmp(p,t); //处理extand数组
//接下来看自己要怎么处理
}
return ;
}

kmp&扩展kmp的更多相关文章

  1. 【string】KMP, 扩展KMP,trie,SA,ACAM,SAM,最小表示法

    [KMP] 学习KMP,我们先要知道KMP是干什么的. KMP?KMPLAYER?看**? 正如AC自动机,KMP为什么要叫KMP是因为它是由三个人共同研究得到的- .- 啊跑题了. KMP就是给出一 ...

  2. 字符串匹配—KMP 扩展KMP Manacher

    kuangbin字符串专题传送门--http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70325#overview 算法模板: KMP: ; ...

  3. Kuangbin 带你飞 KMP扩展KMP Manacher

    首先是几份模版 KMP void kmp_pre(char x[],int m,int fail[]) { int i,j; j = fail[] = -; i = ; while (i < m ...

  4. kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)

    Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...

  5. hdu 4300 Clairewd’s message(kmp/扩展kmp)

    题意:真难懂.. 给出26个英文字母的加密表,明文中的'a'会转为加密表中的第一个字母,'b'转为第二个,...依次类推. 然后第二行是一个字符串(str1),形式是密文+明文,其中密文一定完整,而明 ...

  6. ACM之路(12)—— KMP & 扩展KMP & Manacher

    最近做完了kuangbin的一套关于kmp的题目(除了一道字典树的不会,因为还没学字典树所以先放放),做个总结.(kuangbin题目的链接:http://acm.hust.edu.cn/vjudge ...

  7. kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  8. kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  9. kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

随机推荐

  1. iOS邮箱、手机号等常用验证功能 判断字符串是否int float

    手机号验证 /* 手机号码 13[0-9],14[5|7|9],15[0-3],15[5-9],17[0|1|3|5|6|8],18[0-9] 移动:134[0-8],13[5-9],147,15[0 ...

  2. OC如何跳到系统设置里的各种设置界面

    当 iOS系统版本 <= iOS7时 , 只能跳转到 系统设置页面 ,楼主试了下,非真机是没有任何效果的 当iOS系统版本 < iOS 10.0 时 NSURL *url= [NSURL ...

  3. uva The Tower of Babylon[LIS][dp]

    转自:https://mp.weixin.qq.com/s/oZVj8lxJH6ZqL4sGCXuxMw The Tower of Babylon(巴比伦塔) Perhaps you have hea ...

  4. cocos代码研究(25)Widget子类PageView学习笔记

    基础理论 ListView控件是一个显示滚动项目列表的视图组. 列表项是通过使用addChild或insertDefaultItem插入到列表中的,继承自ScrollView. 代码实践 static ...

  5. 11月16host文件

    #################################################################################################### ...

  6. 20165207 学习基础与C语言基础调查反馈

    文章阅读体会与学习调查反馈 文章阅读体会 我在娄老师的文章里了解到了"做中学"的概念.并且通过娄老师慷慨地分享的相关经验,我对于它有了进一步的理解以及体会.以下是我收获以及我的感想 ...

  7. 通过J2EE Web工程添加Flex项目,进行BlazeDS开发

    http://www.cnblogs.com/noam/archive/2010/07/22/1782955.html 环境:Eclipse 7.5 + Flex Builder 4 plugin f ...

  8. DB开发之oracle

    常用命令: select table_name from user_tables;  //当前用户的表 select table_name from all_tables;  //所有用户的表 sel ...

  9. .net core 2.2 & Mongodb

    .net core 2.2 API项目中使用Mongodb 简单的CRUD封装 创建FoodPlan.Core 项目 创建IEntityBase.cs 接口约束 创建Single.cs 实体 IEnt ...

  10. STM32|4-20mA输出电路(转)

    源:STM32|4-20mA输出电路 STM32+运算放大器实现VI转换,4mA-20mA发送器