kmp算法的实现,严蔚敏版本(调试成功)
2009-09-15 15:52
246 查看
学数据结构时看书算法编的
kmp算法的实现,严蔚敏版本(调试成功)
code:
/************/
main.cpp
#include "stdio.h"
#include "setting.h"
void main()
{
int pos,result,count;
SString S,T;
char *p,*q;
p = S;
q = T;
count=1;
printf( "请输入主串S:" );
scanf("%s",p);
printf( "请输入模串T:" );
scanf("%s",q);
printf( "请输入S中第一个进行匹配的字符的位置pos:" );
scanf( "%d",&pos );
result = Index_KMP( S,T,pos,count );
if( result == 0 )
printf( "匹配不成功!/n" );
else
printf( "匹配成功,相匹配的位置是:%d/n",result );
printf( "总共经过%d次匹配/n",count );
}
/********************/
kmp_operation.cpp
#include "setting.h"
int Index_KMP( SString S,SString T,int pos ,int &count)
{
int i,j,Len_s,Len_t;
int next[256];
i = pos;
j = 1;
Len_s = StrLength( S );
Len_t = StrLength( T );
get_next( T,next );
while( i <= Len_s && j <= Len_t )
{
if( j == 0 || S[i - 1] == T[j - 1] )
{
i++;
j++;
}
else
{
j = next[j];
count++;
}
}
if( j > Len_t)
return i - Len_t;
else
return 0;
}
void get_next( SString T,Next next )
{
int i,j,Len_t;
i = 1;
j = 0;
next[1] = 0;
Len_t = StrLength( T );
while( i < Len_t )
{
if( j == 0 || T[i - 1] == T[j - 1] )
{
i++;
j++;
next[i] = j;
}
else
j = next[j];
}
}
status StrLength( SString S )
{
int i;
i = 0;
while( *(S + i) != '/0' )
{
i++;
}
return i;
}
/*************************/
setting.h
#define MAXSTRLEN 255
typedef int status;
typedef char SString[MAXSTRLEN + 1];
typedef int Next[MAXSTRLEN + 1];
int Index_KMP( SString ,SString ,int ,int & );
void get_next( SString ,Next );
status StrLength( SString S );
kmp算法的实现,严蔚敏版本(调试成功)
code:
/************/
main.cpp
#include "stdio.h"
#include "setting.h"
void main()
{
int pos,result,count;
SString S,T;
char *p,*q;
p = S;
q = T;
count=1;
printf( "请输入主串S:" );
scanf("%s",p);
printf( "请输入模串T:" );
scanf("%s",q);
printf( "请输入S中第一个进行匹配的字符的位置pos:" );
scanf( "%d",&pos );
result = Index_KMP( S,T,pos,count );
if( result == 0 )
printf( "匹配不成功!/n" );
else
printf( "匹配成功,相匹配的位置是:%d/n",result );
printf( "总共经过%d次匹配/n",count );
}
/********************/
kmp_operation.cpp
#include "setting.h"
int Index_KMP( SString S,SString T,int pos ,int &count)
{
int i,j,Len_s,Len_t;
int next[256];
i = pos;
j = 1;
Len_s = StrLength( S );
Len_t = StrLength( T );
get_next( T,next );
while( i <= Len_s && j <= Len_t )
{
if( j == 0 || S[i - 1] == T[j - 1] )
{
i++;
j++;
}
else
{
j = next[j];
count++;
}
}
if( j > Len_t)
return i - Len_t;
else
return 0;
}
void get_next( SString T,Next next )
{
int i,j,Len_t;
i = 1;
j = 0;
next[1] = 0;
Len_t = StrLength( T );
while( i < Len_t )
{
if( j == 0 || T[i - 1] == T[j - 1] )
{
i++;
j++;
next[i] = j;
}
else
j = next[j];
}
}
status StrLength( SString S )
{
int i;
i = 0;
while( *(S + i) != '/0' )
{
i++;
}
return i;
}
/*************************/
setting.h
#define MAXSTRLEN 255
typedef int status;
typedef char SString[MAXSTRLEN + 1];
typedef int Next[MAXSTRLEN + 1];
int Index_KMP( SString ,SString ,int ,int & );
void get_next( SString ,Next );
status StrLength( SString S );
相关文章推荐
- 低版本XCode支持高版本iOS真机调试的实现
- IBM X系列手提 成功安装MAC OS 并实现IPhone真机调试 教程
- 文件更新后, 编译也成功,但无法调试,exe运行后还是原来的版本not found or not built by the last incremental link; performing full link
- hibernate 3.6 小实例 经过调试半天终于成功。刚接触3.6版本头疼的看看
- 低版本XCode支持高版本iOS真机调试的实现
- 数据结构 严蔚敏 清华大学出版社 第一章 抽象数据类型 三元组的实现 成功编译并运行
- 数据结构 严蔚敏 清华大学出版社 第二章 抽象数据类型 链表的实现 成功编译并运行
- linux下一个用eXosip实现的sip通信的UAC和UAS的例子(调试成功)
- 《Color Transfer Based on Normalized Cumulative Hue Histograms》论文实现,已调试成功,可直接使用
- 实现Release版本调试功能
- 对KMP的理解,以及kmp算法java版本实现
- 给出一个printk函数的实现版本(用于早期调试用)
- KMP算法实现C和java版本
- Visual Studio 2015实现远程调试(亲测VS2010版本不可以实现)
- C语言实现动态链表的创建、删除、插入、输出(CentOS6.5上调试成功)
- productFlavors 实现多渠道多版本打包调试
- 栈的实现 (数据结构 严蔚敏 清华大学出版社 第三章 抽象数据类型 成功编译并运行)
- 数据结构 严蔚敏 清华大学出版社 第一章 抽象数据类型 三元组的实现 成功编译并运行
- MySQL主从复制与读写分离在windows系统下的实现(amoeba3.0+mysql5.1实现)因版本问题,本文在原来文章基础上有更改并且本人已经测试成功
- window7下vs2010环境成功下载 编译 调试最新版本x265