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. 解决redis远程连接不上的问题

    解决redis远程连接不上的问题 redis现在的版本开启redis-server后,redis-cli只能访问到127.0.0.1,因为在配置文件中固定了ip,因此需要修改redis.conf(有的 ...

  2. AOP切点表达式

    Aspectj切入点语法定义 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义切入点表达式  execu ...

  3. SQL Server查看库、表占用空间大小

    转自:https://blog.csdn.net/yenange/article/details/50493580 查询数据文件与日志文件占用情况,查看数据大小,查看库大小 1. 查看数据文件占用(权 ...

  4. (转)nginx负载均衡(5种方式)、rewrite重写规则及多server反代配置梳理

    Nginx除了可以用作web服务器外,他还可以用来做高性能的反向代理服务器,它能提供稳定高效的负载均衡解决方案.nginx可以用轮询.IP哈希.URL哈希等方式调度后端服务器,同时也能提供健康检查功能 ...

  5. 跟我学Makefile(四)

    使用函数:函数调用,很像变量的使用,也是以“$”来标识的,其语法如下: $(<function> <arguments>) 或是 ${<function> < ...

  6. leetcode 数据库题解

    184. Department Highest Salary 题意: The Employee table holds all employees. Every employee has an Id, ...

  7. java之标记型接口Cloneable

    1.克隆用途. Cloneable和Serializable一样都是标记型接口,它们内部都没有方法和属性,implements Cloneable表示该对象能被克隆,能使用Object.clone() ...

  8. 1 :2 Strust2—Demo

    =============================================================== Demo基础包:

  9. CDOJ 1048 Bob's vector(快速幂+三分法)

    题目大意:原题链接 给定数组A[i]的计算方法,求出其任意一个极值点 解题思路:求极值点用三分法,一般计算100次足矣,所以三分时上限为100,不过运行时间可能会长一点    用for循环    用w ...

  10. Flex远程调用机制RemoteObject应用技巧

    转自:http://zerozone.javaeye.com/blog/60846Flex远程调用RemoteObject出现的问题及解答: 本文主要讨论Flex在客户端与J2EE中间层数据交互的过程 ...