kmp&扩展kmp
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的更多相关文章
- 【string】KMP, 扩展KMP,trie,SA,ACAM,SAM,最小表示法
[KMP] 学习KMP,我们先要知道KMP是干什么的. KMP?KMPLAYER?看**? 正如AC自动机,KMP为什么要叫KMP是因为它是由三个人共同研究得到的- .- 啊跑题了. KMP就是给出一 ...
- 字符串匹配—KMP 扩展KMP Manacher
kuangbin字符串专题传送门--http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70325#overview 算法模板: KMP: ; ...
- Kuangbin 带你飞 KMP扩展KMP Manacher
首先是几份模版 KMP void kmp_pre(char x[],int m,int fail[]) { int i,j; j = fail[] = -; i = ; while (i < m ...
- 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 ...
- hdu 4300 Clairewd’s message(kmp/扩展kmp)
题意:真难懂.. 给出26个英文字母的加密表,明文中的'a'会转为加密表中的第一个字母,'b'转为第二个,...依次类推. 然后第二行是一个字符串(str1),形式是密文+明文,其中密文一定完整,而明 ...
- ACM之路(12)—— KMP & 扩展KMP & Manacher
最近做完了kuangbin的一套关于kmp的题目(除了一道字典树的不会,因为还没学字典树所以先放放),做个总结.(kuangbin题目的链接:http://acm.hust.edu.cn/vjudge ...
- kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...
- 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 ...
随机推荐
- Monkey Tradition---LightOj1319(中国剩余定理模板)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1319 题意:有 n 个猴子,n 棵树,树的高度为 L ,每个猴子刚开始的时候都在树的底 ...
- java 调用静态方法和构造函数和静态块执行的先后顺序
构造方法是只有你在new对象的时候才会执行,静态语句块和静态方法在类加载到内存的时候就已经执行了,另外,静态语句块只能给静态变量赋值,里面不能出现方法,同样,静态方法里面也不能出现静态语句块 追问: ...
- 【Javascript】Windows下Node.js与npm的安装与配置
1:先下载Node.js,网站https://nodejs.org/en/,左侧为稳定版,右侧为最新版,推荐稳定版 2:Node.js安装,运行下载后的.msi文件,一路下一步就可以了,我选择的安 ...
- java-mybaits-00602-查询缓存-mybatis整合ehcache
ehcache是一个分布式缓存框架.EhCache 是一个纯Java的进程内缓存框架,是一种广泛使用的开源Java分布式缓存,具有快速.精干等特点,是Hibernate中默认的CacheProvide ...
- jQuery Form
https://github.com/jquery-form/form#type 概念 jQuery表单插件允许您轻松,无差错地升级HTML表单以使用AJAX. 主要方法ajaxForm和ajaxSu ...
- hdu1286(找新朋友)&&POJ2407Relatives(欧拉函数模版题)
http://acm.hdu.edu.cn/showproblem.php?pid=1286 没什么好说的,模板题,主要是弄懂欧拉函数的思想. #include <iostream> #i ...
- js判断用户是在PC端或移动端访问
js如何判断用户是在PC端和还是移动端访问. 最近一直在忙我们团队的项目“咖啡之翼”,在这个项目中,我们为移动平台提供了一个优秀的体验.伴随Android平台的红火发展.不仅带动国内智能手机行业,而 ...
- tornado 入门
Overview FriendFeed是一款使用 Python 编写的,相对简单的 非阻塞式 Web 服务器.其应用程序使用的 Web 框架看起来有些像 web.py 或者 Google 的 weba ...
- placement new--《C++必知必会》 条款35
placement new是重载operator new的一个标准.全局的版本,它不能被自定义的版本代替(不像普通的operator new和operator delete能够被替换成用户自定义的版本 ...
- 系统管理命令之w
区别于who命令,w命令不仅可以看到登录服务器的用户信息,而且可以看到这些用户做了什么 1.查看该命令的帮助信息. # w --help 2.查看该命令的版本信息. # w --version 3 ...