最一开始学的时候照着板子敲敲不对,,过了这么久了终于照着板子敲能敲对了
还有附上原来写的看了kmp好几天的学习的博客kmp入门
hdu1711
两个数组,找第一次匹配成功的位置。(模版中的模版)
//
// main.cpp
// KMP_模版_hdu1711
//
// Created by 陈冉飞 on 2019/8/17.
// Copyright © 2019 陈冉飞. All rights reserved.
//
#include <iostream>
using namespace std;
int a[1000010],b[10010],nt[10010];
#include <cstring>
#define cl(a,b) memset(a,b,sizeof(a))
int T,n,m;
void getnext(int m){
int i = 0,j = -1;nt[0] = -1;
while (i < m) {
if (j == -1 || b[i] == b[j]) {
i++;j++;nt[i] = j;
}else j = nt[j];
}
}
int kmp(int n,int m){
int i = 0,j = 0;
while (i < n && j < m) {
if (j == -1 || a[i] == b[j]) {
i++;j++;
}else j = nt[j];
}
if (j == m) return i-j+1;
else return -1;
}
int main(int argc, const char * argv[]) {
for (scanf("%d",&T); T; T--) {
scanf("%d%d",&n,&m);
for (int i = 0; i < n; i++) scanf("%d",&a[i]);
for (int i = 0; i < m; i++) scanf("%d",&b[i]);
getnext(m);
cout<<kmp(n, m)<<endl;
}
return 0;
}
hdu2087
模版,在while中添加一个判断条件,只要字串匹配到头,cnt++,索引刷新就完事了(没有重叠部分的)
//
// main.cpp
// kmp_字串_hdu2087
//
// Created by 陈冉飞 on 2019/8/17.
// Copyright © 2019 陈冉飞. All rights reserved.
//
#include <iostream>
using namespace std;
#define maxn 1005
char a[maxn],b[maxn],nt[maxn];
int n,m;
void getnext(int m){
int i = 0,j = -1;nt[0] = -1;
while (i < m) {
if (j == -1 || b[i] == b[j]) {
i++;j++;nt[i] = j;
}else j = nt[j];
}
}
int kmp(int n,int m){
int i = 0,j = 0,cnt =0;
while (i < n && j < m) {
if (j == -1 ||a[i] == b[j]) {
i++;j++;
}else j = nt[j];
if (j == m) {
cnt++;j = 0;
}
}
return cnt;
}
int main(int argc, const char * argv[]) {
while (scanf("%s",a) && a[0] != '#') {
scanf("%s",b);
n = strlen(a);m = strlen(b);
getnext(m);
cout<<kmp(n, m)<<endl;
}
return 0;
}
csu1598
和上面一模一样(也是不允许出现重叠)
//
// main.cpp
// kmp_找相同的部分数量_csu1598
//
// Created by 陈冉飞 on 2019/8/17.
// Copyright © 2019 陈冉飞. All rights reserved.
//
#include <iostream>
#include <cstring>
using namespace std;
#define maxn 100010
int T,nt[maxn];
char a[maxn],b[maxn];
void getnext(int m){
int i = 0,j = -1;nt[0] = -1;
while (i < m) {
if (j == -1 || b[i] == b[j]) {
i++;j++;nt[i] = j;
}else j = nt[j];
}
}
int kmp(int n,int m){
int i = 0,j = 0,cnt = 0;
while (i < n && j < m) {
if (j == -1 || a[i] == b[j]) {
i++;j++;
}else j = nt[j];
if (j == m) {
cnt++;j = 0;
}
}
return cnt;
}
int main(int argc, const char * argv[]) {
for (scanf("%d",&T); T; T--) {
scanf("%s%s",a,b);
int n = strlen(a),m = strlen(b);
getnext(m);
cout<<kmp(n, m)<<endl;
}
return 0;
}
hdu1686
同样是数 子串在 原串中出现的次数,这个题允许重叠。
//
// main.cpp
// kmp_子串出现次数
//
// Created by 陈冉飞 on 2019/8/17.
// Copyright © 2019 陈冉飞. All rights reserved.
//
#include <iostream>
using namespace std;
char b[10010],a[1000010];
int nt[10010],T;
void getnext(int m){
int i = 0,j = -1;nt[0] = -1;
while (i < m) {
if (j == -1 || b[i] == b[j]) {
i++;j++;nt[i] = j;
}else j = nt[j];
}
}
int kmp(int n,int m){
int i = 0,j = 0,cnt = 0;
while (i < n) {
if (j == -1 || a[i] == b[j]) {
i++;j++;
}else j = nt[j];
if (j == m) {
cnt++;
}
}
return cnt;
}
int main(int argc, const char * argv[]) {
for (scanf("%d",&T); T; T--) {
scanf("%s%s",b,a);
int m = strlen(b),n = strlen(a);
getnext(m);
cout<<kmp(n, m)<<endl;
}
return 0;
}