自写KMP模板

 1 int* Getnext(char *p,int len_p){//p为模式串
 2     int *Next = new int[len_p];
 3     Next[0] = -1;
 4     int j = -1,i=0;
 5     while(i < len_p){//自身与自身匹配
 6         while(j != -1&&p[j]!=p[i])//移动匹配串
 7             j = Next[j];
 8         j+=1;
 9         Next[++i] = j;
10     }
11     return Next;
12 }
13 vector<int>* Kmp(char *s,char *p,int len_s,int len_p){//s为主串,p为匹配串
14     int *Next = Getnext(p,len_p);//求得Next数组
15     int j = 0;//j为匹配串匹配到的位置
16     vector<int>* ans = new vector<int>();//ans为匹配到的主串的开头下标位置
17     for(int i=0;i < len_s;i++){
18         while(j != -1 && s[i] != p[j])
19             j = Next[j];
20         j+=1;
21         if(j == len_p){//匹配串匹配完毕
22             ans->push_back(i - len_p + 1);
23             j = Next[j];//j退回到Next[j]
24         }
25     }
26     return ans;
27 }

原文地址:https://www.cnblogs.com/xfww/p/8585148.html

时间: 2024-10-22 14:55:03

自写KMP模板的相关文章

HDU 1711 Number Sequence(KMP模板)

http://acm.hdu.edu.cn/showproblem.php?pid=1711 这道题就是一个KMP模板. 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 const int maxn = 1000000+5; 6 7 int n,m; 8 9 int next[maxn]; 10 int a[maxn], b[maxn]; 11 12 void get_next() 13 { 1

POJ Oulipo(KMP模板题)

题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack&

最小生成树,POJ和HDU几道题目的解题报告(基于自己写的模板)

首先POJ题目: 链接:1251 Jungle Roads 题目大意:纯求最小生成树,结果为最小权值边的和.采用邻接表 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <queue> 5 using namespace std; 6 7 #define maxn 30 //最大顶点个数 8 int n; //顶点数,边数 9 10 struct arcn

[POJ 3461] Oulipo &amp; KMP模板

Oulipo Time Limit: 1000ms, Memory Limit: 65536K Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book: Tout avait Pair normal, mai

hdu 1711 KMP模板题

// hdu 1711 KMP模板题 // 贴个KMP模板吧~~~ #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int MAX_N = 1000008; const int MAX_M = 10008; int T[MAX_N]; int p[MAX_M]; int f[MAX_M]; int

poj 3461 Oulipo(KMP模板题)

题目链接:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23559   Accepted: 9437 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a

剪花布条---hdu2087(kmp模板)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 kmp模板题: #include <cstdio> #include <cstring> #include <iostream> using namespace std; #define N 1100 char s1[N], s2[N]; int p[N], L1, L2; void Getp() { int i=0, j=-1; p[0] = -1; while(i

hdu 1686 KMP模板

1 // hdu 1686 KMP模板 2 3 // 没啥好说的,KMP裸题,这里是MP模板 4 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <algorithm> 9 10 using namespace std; 11 12 const int MAX_N = 1000008; 13 const int MAX_M = 10008; 14 char T

数据结构实验之串三:KMP应用(KMP模板)

数据结构实验之串三:KMP应用(KMP模板) AC_Code: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <map> 6 #include <stack> 7 using namespace std; 8 typedef long long ll; 9 int Nex[1000000]; 10