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 ...
随机推荐
- 浅谈vuex
很多技术,刚接触的时候:这是啥?用的时候:哟嚯,是挺好用的!加以研究:卧槽,就是这么个逼玩意儿! 最近接手了一个别人写了1/5的vue项目(页面画了1/3,接口啥都没对); 对于表格中的数据项操作以及 ...
- Python-Select/Poll/Epoll使用
select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当select()返回后,该数组中就绪的文件描述符便会被内核修改标志位,使 ...
- angular自定义指令命名的那个坑
Directive 先从定义一个简单的指令开始. 定义一个指令本质上是在HTML中通过元素.属性.类或注释来添加功能.AngularJS的内置指令都是以ng开头,如果想自定义指令,建议自定义一个前缀代 ...
- My Emacs Writing Setup
My Emacs Writing Setup Table of Contents 1. About this Document 1.1. Related Materials 1.2. Change H ...
- centos linux系统日常管理3 服务管理ntsysv,chkconfig,系统日志rsyslog,last ,lastb ,exec,xargs,dmesg,screen,nohup,curl,ping ,telnet,traceroute ,dig ,nc,nmap,host,nethogs 第十六节课
centos linux系统日常管理3 服务管理ntsysv,chkconfig,系统日志rsyslog,last ,lastb ,exec,xargs,dmesg,screen,nohup,cur ...
- PAT 1147 Heaps[难]
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...
- cocos代码研究(11)ActionManager类学习笔记
理论部分 ActionManager是一个单例类,管理所有动作. 通常你不需要直接使用这个类.大多情况下,你将使用Node的接口,它提供了更友好的封装 但也有一些情况下,你可能需要使用这个单例. 示例 ...
- #C++初学记录(阶乘#递归)
练习题目三 用递归进行阶乘 运行代码 #include<iostream> using namespace std; int f(int n); int n; int main() { c ...
- smart基础
主要是libs里面的smarty类,和init.inc.php配置文件 剩下的是php文件夹.模板文件夹,临时文件夹.缓存文件夹.配置文件夹.插件文件夹 调用main文件夹中的php文件,通过libs ...
- java第五天
p37: 练习1 /** * Created by xkfx on 2017/2/22. */ public class DataOnly { int anInt; char aChar; publi ...