hope_process
heixia108
全部博文(251)
oldlinux(3)
Perl(1)
学习资料(2)
Enhlish(2)
心情(31)
Windows(2)
硬件相关(1)
软件工程(0)
数据挖掘与AI(1)
OpenGL(4)
2009年(2)
2008年(86)
2007年(163)
kenvifir
ganwei06
prolj
cynthia
浪花小雨
我是小小
zhu_xian
gamelefe
onejacky
zwmeimei
miaosen8
分类: C/C++
2008-03-22 09:18:31
/* -*- C -*- * * KMP.c - * * Author : heixia * Created On : Sat Mar 22 08:54:54 2008 * */ #include <stdio.h> #include <stdlib.h> #define MAX 1000 void GetNextval( char *t,int nextval[ ] ); int KMPIndex( char *s,char *t ); int main( ){ char s[ 50 ] = "abcaabbabcabaacbacba"; char t[ 10 ] = "abcabaa"; int next[ 10 ]; int i = 0; i = KMPIndex( s,t ); if( i != -1 ) printf("The matched position is: %d\n",i); else printf( "Can't be matched.\n" ); } void GetNextval( char *t,int nextval[ ] ){ int j = 0,k = -1,len = strlen( t ); nextval[ 0 ] = -1; while( j < len ){ if( k == -1 || t[ j ] == t[ k ] ){ j ++; k ++; if( t[ j ] != t[ k ] ) nextval[ j ] = k; else nextval[ j ] = nextval[ k ]; } else k = nextval[ k ]; } } int KMPIndex( char *s,char *t ){ int next[ MAX ],i = 0,j = 0,v; int len1 = strlen( s ),len2 = strlen( t ); GetNextval( t,next ); while( i < len1 && j < len2 ){ if( j == -1 || s[ i ] == t[ j ] ){ i ++; j ++; } else j = next[ j ]; } if( j >= len2 ) v = i- len2; else v = -1; return v; }
上一篇:8数码问题
下一篇:生活的另一面
登录 注册