当前位置:网站首页>KMP! You deserve it!!! Run directly!
KMP! You deserve it!!! Run directly!
2022-06-11 18:41:00 【Little doctor·】
Written this time kmp The algorithm is based on next Array to move and compare pattern strings , according to nextval The comparative movement of is still under consideration , In fact, it is (wo) One (bi) sentence (jiao) generation (cai) Code thing .
Because of the time, I won't hang it out , I'll send it after I've changed it !
#include <iostream>
#include <cstdlib>
#include <vector>
#define maxsize 100
using namespace std;
typedef struct String{
string string;
int length;
}String;
int *get_next(String S,int *next)
{
int i=0;
int j=-1;
next[0]=-1;
while(i<=S.length)
{
if(j==-1||S.string[i]==S.string[j])
{
++i;
++j;
next[i]=j;
}
else
j=next[j];
}
return next;
}
int KMP(String Str,String S,int *next,int *nextval)
{
int i=0;
int j=0;
while(i<Str.length&&j<S.length)
{
if(j==-1||Str.string[i]==S.string[j])
{
i++;
j++;
}
else
{
j=next[j];
}
}
if(j==S.length)
return i-S.length;
else
return -1;
}
int main()
{
struct String Str;// Main string
struct String S; // Pattern string
Str.string="aabghjvacabaababaabo";
Str.length=20;
S.string="abaab";
S.length=5;
//int *next=(int *)malloc(S.length*sizeof(int));
int *next=new int[5];
next=get_next(S,next);
cout<<"next--main"<<endl;
for(int i=0;i<S.length;i++)
{
cout<<next[i]<<" ";
}
cout<<endl;
cout<<"KMP"<<endl;
for(int i=0;i<S.length;i++)
{
cout<<next[i]<<" ";
}
cout<<endl;
int temp=KMP(Str,S,next,nextval);
if(temp==-1)
cout<<" Matching failure "<<endl;
else
cout<<" The match is successful "<<endl<<" Matching position :"<<temp<<endl;
return 0;
}
Buried pit !!!!
It took a whole week to write an algorithm for baked bun slices , What a disgrace , I am ashamed of my award !!
The algorithm is really hand-made without writing ! But I really don't have time !! Is dubious !
This buried pit
1. Comparison of stack area and heap area
2. About the innocent recycling of array memory !!!( In fact, the essence is the first
A problem )
3. I would like to inform you , Be sure to read clearly when you write kmp in next The beginning of the array
The subscript is 0 still 1. It's important !!!
4. There seems to be nothing left !
Good night, ! I wish you nb!
边栏推荐
- Mysql深入完全学习---阶段1---学习总述
- Niuke brush questions part8
- Force deduction 33 questions, search rotation sorting array
- Modern application of LDAP directory server
- Async leads to unexpected function results and changes the intention of the original code; await is only valid in async functions and the top level bodies of modules
- Uploading and downloading of necessary files in development
- Friendly tanks fire bullets
- Two methods for matlab to save imshow drawing pictures to a specified folder
- SA token single sign on SSO mode 2 URL redirection propagation session example
- *Jetpack notes understanding of lifecycle ViewModel and livedata
猜你喜欢
![[c language] limit the number of searches and output the maximum value found in the number of internal searches](/img/e6/cbb8dd54b49ade453251a70c8455e8.png)
[c language] limit the number of searches and output the maximum value found in the number of internal searches

使用mysql判断日期是星期几

JS实现全屏展示的具体方法

力扣23题,合并K个升序链表

信号的处理与捕捉

* Jetpack 笔记 Room 的使用

Force deduction 23 questions, merging K ascending linked lists

记录一下phpstudy配置php8.0和php8.1扩展redis

uni-app 慕客热搜项目实战(一)tabBar的制作

视觉SLAM十四讲笔记-10-1
随机推荐
力扣39题组合总和
[c language] compress strings and add markup characters
学习使用LSTM和IMDB评论数据进行情感分析训练
2023年西安交通大学管理学院MPAcc提前批面试网报通知
BigDecimal基本使用与闭坑介绍
全志科技T3开发板(4核ARM Cortex-A7)——视频开发案例
力扣刷题——二叉树的最近公共祖先
公共字段自动填充,你了解吗
Why is ti's GPMC parallel port more often used to connect FPGA and ADC? I give three reasons
Tips for using apipost
用户组的操作
构造敌方坦克
SAP BTP 上 workflow 和 Business Service 的 project 管理
Common operations of Visio
用户信息管理的功能开发
Ubuntu installs PSQL and runs related commands
Function and principle of key in V-for
Financial bank_ Introduction to collection system
Introduction to basic use and pit closure of BigDecimal
Why is ti's GPMC parallel port more often used to connect FPGA and ADC? I give three reasons