KMP algorithm
来源:互联网 发布:淘宝商品价格走势网站 编辑:程序博客网 时间:2024/04/29 08:22
/**********************************************************************************************/
//KMP C++ version
//FILE: KMP.h
//AUTHOR: shenan
//DATE: 2008.12.25
#ifndef _TEMPLATE_KMP_H_
#define _TEMPLATE_KMP_H_
template<class T>
void get_next(T sub, int next[])
{
int i = 0, j = -1, lenSub = strlen(sub);
next[0] = -1;
while( i < lenSub )
{
if( j == -1 || sub[i] == sub[j] )
{ ++i; ++j; next[i] = j; }
else
j = next[j];
}
}
template<class T>
void get_nextval(T sub, int next[])
{
int i = 0, j = -1, lenSub = strlen(sub);
next[0] = -1;
while( i < lenSub )
{
if( j == -1 || sub[i] == sub[j] )
{
++i; ++j;
if( sub[i] != sub[j] )
next[i] = j;
else
next[i] = next[j];
}
else
j = next[j];
}
}
template<class T>
int KMP(T src, T sub, int next[], int pos)
{
int i = pos, j = 0;
int lenSrc = strlen(src);
int lenSub = strlen(sub);
while( i < lenSrc && j < lenSub )
{
if( j == -1 || src[i] == sub[j] )
{ ++i; ++j; }
else
j = next[j];
}
if( j >= lenSub )
return i - lenSub;
else
return -1;
}
#endif
//FILE: test.cpp
//AUTHOR: shenan
//DATE: 2008.12.25
#include <iostream>
#include <string>
using namespace std;
#include "KMP.h"
int main()
{
char * src = "acabaabaabcaaaabc";
char * sub = "aaaab";//"abaabcac";
int lenSub = strlen(sub);
int * next = new int(lenSub);
get_next(sub, next);
get_nextval(sub, next);
int n = KMP(src, sub, next, 0);
char *p = src+n;
cout<<p<<endl;
delete[]next;
return 0;
}
/**********************************************************************************************/
//KMP C version
//FILE: KMP.c
//AUTHOR: nshe
//DATE: 2009.01.03
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void get_next(char * sub, int next[])
{
int i = 0, j = -1, len = strlen(sub);
next[0] = -1;
while( i < len )
{
if( j == -1 || sub[i] == sub[j])
{ ++i; ++j; next[i] = j; }
else
j = next[j];
}
}
void get_nextval(char * sub, int nextval[])
{
int i = 0, j = -1, len = strlen(sub);
nextval[0] = -1;
while( i < len )
{
if( j == -1 || sub[i] == sub[j])
{
++i; ++j;
if( sub[i] != sub[j] )
nextval[i] = j;
else
nextval[i] = nextval[j];
}
else
j = nextval[j];
}
}
int KMP( char * src, char * sub, int next[], int pos )
{
int i = pos, j = 0;
int lenSrc = strlen(src);
int lenSub = strlen(sub);
while( i < lenSrc && j < lenSub )
{
if( j == -1 || src[i] == sub[j] )
{ ++i; ++j; }
else
j = next[j];
}
if(j >= lenSub)
return i - lenSub;
return -1;
}
int main()
{
char * src = "acabaabaabcaaaabc";
char * sub = "aaaab";//"abaabcac";
int n1,n2;
char *p1, *p2;
int lenSub = strlen(sub);
int * next = (int*)malloc(lenSub);
int * nextval = (int*)malloc(lenSub);
get_next(sub, next);
get_nextval(sub, nextval);
n1 = KMP(src, sub, next, 0);
n2 = KMP(src, sub, nextval, 0);
p1 = src+n1;
printf("n1=%d, substr=%s/r/n", n1, p1);
p2 = src+n2;
printf("n1=%d, substr=%s/r/n", n2, p2);
free(next);
free(nextval);
return 0;
}
- KMP algorithm
- KMP algorithm
- KMP Algorithm I
- KMP Algorithm II
- KMP algorithm ---C++
- KMP string matching algorithm
- 春节特辑 C1 E1 : KMP Algorithm
- Algorithm Gossip (11) KMP字符串匹配
- KMP算法(The Knuth-Morris-Pratt Algorithm)
- KMP算法——Knuth–Morris–Pratt algorithm
- The Knuth-Morris-Pratt Algorithm KMP模式匹配算法
- [Algorithm] 字符串匹配: MP,KMP,暴力搜索等(ZT)
- zoj3957——Knuth-Morris-Pratt Algorithm(KMP)
- KMP 算法(Knuth–Morris–Pratt algorithm)
- KMP (Knuth-Morris-Pratt) algorithm introduction to algorithm 3rd, example 32.4
- hdu, KMP algorithm, linear string search algorithm, a nice reference provided
- Algorithm
- Algorithm
- QuickSort
- 开心网存在重大安全隐患
- Permutation
- 对《专利法》修改后的解读 [综合整理]
- 新年要有新发展
- KMP algorithm
- RFC 连接
- 适配器模式
- 夜晚天空又开始下雪
- 有关乱码的处理---中国程序员永远无法避免的话题
- 在一个数组中实现三个栈,并且数组未满之前栈不能溢出的问题
- DWR简介
- Ant 简介
- GOOGLE,百度网站优化SEO最终详解