KMP算法
来源:互联网 发布:个人网站 建站 编辑:程序博客网 时间:2024/06/08 05:17
KMP算法:
next函数如图所示:
#include <iostream>using namespace std;#include <cstdio>#include <cstdlib>#include <cstring>int next[100];int getnext(char t[]){ int i = 0,j = -1; next[0] = -1; while(i < strlen(t)-1){ if(j == -1 || t[i] == t[j]){ i++; j++; next[i] = j; }else j = next[j]; } for(int i = 0;i < strlen(t);i++){ printf("%d ",next[i]); } printf("\n");}int main(){ char s[100],t[100]; /* s是原始串,t是被匹配串 */ int i,j; while(scanf("%s%s",s,t) != EOF){ memset(next,0,sizeof(next)); getnext(t); i = 0,j = 0; int lens = strlen(s),lent = strlen(t); while(i < lens){ if((j == -1) || (s[i] == t[j])){ i++; j++; if(j == lent){ printf("%d ",i - lent); j = 0; } } else{ j = next[j]; } } printf("\n"); } system("pause");}
0 0
- KMP算法详解 【KMP】
- 【KMP】KMP算法模板
- KMP hihoCoder1015 KMP算法
- kmp算法
- KMP算法
- KMP算法
- KMP算法
- KMP算法
- KMP 算法
- kmp算法
- KMP算法
- kmp算法
- KMP算法
- KMP算法
- kmp算法
- kmp算法
- KMP算法
- KMP算法
- html5 canvas实现小球绕圆旋转
- Java内部类
- Unity3d中制作Loading场景进度条所遇到的问题
- javascript w3cschool 细读笔记
- SIP与视频监控
- KMP算法
- 图论算法(6) --- Tarjan算法求强连通分量
- 用“动态规划”解决最长滑雪道问题
- Qt5与QML(六)
- Choose a destination with a supported architecture in order to run on this device
- 尤尼3D打印教材 助力基础教育
- Linux内核设计与实现读书笔记九——内核同步介绍(内附思维导图)
- Mysql创建、删除用户
- 虚拟存储器总结