字符串匹配-KMP算法
来源:互联网 发布:华罗庚知乎 编辑:程序博客网 时间:2024/06/05 10:27
#include<iostream>#include<string>#include<cstdio>#include<cstring>using namespace std;const int MAXL=1e6+10;char s[MAXL+50],t[MAXL+50];//s为目标串,t为模式串int Next[MAXL+50];void Getnext(){ int i=0,j=-1; Next[0]=-1; int len=strlen(t); while(i<len-1) { if(j==-1||t[i]==t[j]) { Next[++i]=++j; if(t[i]==t[j]) Next[i]=Next[j]; } else j=Next[j]; }}int index_KMP(){ int i=0,j=0; int len1=strlen(s),len2=strlen(t); while(i<len1&&j<len2) { if(j==-1||s[i]==t[j]) { i++;j++; } else { j=Next[j]; } } if(j>=len2) return i-len2; else return -1;}int main(){ int i; while(~scanf("%s",s)) { scanf("%s",t); memset(Next,0,sizeof(0)); Getnext();//求Next数组的值 int sub=index_KMP();//判断是否匹配 if(sub==-1) cout<<"不匹配"<<endl; else cout<<sub<<endl; }}
阅读全文
0 0
- KMP 字符串匹配算法
- kmp字符串匹配算法
- kmp字符串匹配算法
- KMP字符串匹配算法
- 字符串匹配算法-kmp
- KMP(字符串匹配)算法
- KMP 字符串匹配算法
- 字符串匹配算法:KMP
- KMP算法 字符串匹配
- 字符串匹配 KMP 算法
- KMP字符串匹配算法
- KMP字符串匹配算法
- KMP算法:字符串匹配
- KMP字符串匹配算法
- 字符串匹配--KMP算法
- KMP字符串匹配算法
- KMP字符串匹配算法
- KMP字符串匹配算法
- sqlites3基本操作
- PHP Class Autoload 的几种方法
- Android Studio 3.0 RC 1 下载地址
- gdb 调试子进程
- tomcat solr服务无法搜索中文
- 字符串匹配-KMP算法
- Ted Belytschko的单晶塑性
- 【项目实战】---EasyUI数据表格的行风格
- 创建 VirtualBoxClient COM 对象失败. 应用程序将被中断的解决办法
- 聊聊CDN的那些事儿
- JavaScript里的面向对象心得
- Address already in use: JVM_Bind(端口冲突)
- 树结构
- okhhtp + recyclerview + 上拉刷新下拉加载 + 点击事件