您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 招聘面试 > 简单匹配模式 (2)
#includestdio.h#includestring.h#includestdlib.h#defineOK1#defineERROR0#defineTRUE1#defineFALSE0#defineMAXSIZE40typedefintStatus;typedefcharString[MAXSIZE+1];typedefchar*SString;StatusStrAssign(StringT,char*chars);voidget_next(SStringT,int*next);intIndex_KMP(SStringS,SStringT,intpos);intmain(void){intpos=1;SStringT,S;T=(char*)malloc(40);S=(char*)malloc(40);char*chars;chars=ababcabcacbab;StrAssign(S,chars);chars=abcac;StrAssign(T,chars);printf(%d,Index_KMP(S,T,pos));getchar();return0;}StatusStrAssign(StringT,char*chars){inti;if(strlen(chars)MAXSIZE)returnERROR;else{T[0]=strlen(chars);for(i=1;i=T[0];i++)T[i]=*(chars+i-1);returnOK;}}voidget_next(SStringT,int*next){//算?法¤¡§4.7inti=1;next[1]=0;intj=0;while(iT[0]){if(j==0||T[i]==T[j]){++i;++j;next[i]=j;}elsej=next[j];}}intIndex_KMP(SStringS,SStringT,intpos){//算?法¤¡§4.6//利¤?用®?模¡ê式º?串ä?T的Ì?next函¡¥数ºy求¨®T在¨²主¡Â串ä?S中D第̨²pos个?字Á?符¤?之?后¨®的Ì?位?置?的Ì?//KMP算?法¤¡§。¡ê其?中D,ê?T非¤?空?,ê?1≤¨¹pos≤¨¹StrLength(S)。¡êintnext[255];inti=pos;intj=1;get_next(T,next);while(i=S[0]&&j=T[0]){if(j==0||S[i]==T[j]){//继¨¬续?比À¨¨较?后¨®继¨¬字Á?符¤?++i;++j;}elsej=next[j];//模¡ê式º?串ä?向¨°右®¨°移°?动¡¥}if(jT[0])returni-T[0];//匹£¤配?成¨¦功|elsereturn0;}//Index_KMP
本文标题:简单匹配模式 (2)
链接地址:https://www.777doc.com/doc-2095934 .html