C語言數(shù)據(jù)結(jié)構(gòu)中串的模式匹配
C語言數(shù)據(jù)結(jié)構(gòu)中串的模式匹配
串的模式匹配問題:樸素算法與KMP算法
#include<stdio.h> #include<string.h> int Index(char *S,char *T,int pos){ //返回字串T在主串S中第pos個(gè)字符之后的位置。若不存在,則函數(shù)值為0. //其中,T非空,1<=pos<=StrLength(s). int i=pos; int j=1; while(i<=S[0]&&j<=T[0]){ if(S[i]==T[j]){++i;++j;} else{i=i-j+2;j=1;} } if(j>T[0]) return i-T[0]; else return 0; } int get_next(char *T,int next[]){ //求模式串T的next函數(shù)值并存入數(shù)組next。 int i=1;next[1]=0;int j=0; while(i<T[0]){ if (j==0||T[i]==T[j]){++i;++j;next[i]=j;} else j=next[j]; } return *next; } int Index_KMP(char *S,char *T,int pos){ //利用模式串T的next函數(shù)求T在主串S中第pos個(gè)字符之后的位置的KMP算法,其中T非空,1<=pos<=StrLength(S). int next[100]; *next=get_next(T,next); int j=1,i=pos; while(i<=S[0]&&j<=T[0]){ if(j==0||S[i]==T[j]){++i;++j;} else j=next[j]; } if(j>T[0]) return i-T[0]; else return 0; } void main() { int id,j,k,i,a; printf("輸入主串、子串和匹配起始位置\n"); char A[20];char B[10]; printf("請輸入主字串內(nèi)容\n"); gets(A+1); *A=strlen(A+1); printf("請輸入子字串內(nèi)容\n"); gets(B+1); *B=strlen(B+1); printf("請輸匹配起始位置\n"); scanf("%d",&j); //printf("%d ",k); do{ printf("\n請輸入您需要的任務(wù)的序號"); printf("\n1:樸素的模式匹配算法"); printf("\n2:快速模式匹配算法"); printf("\n3:退出\n"); scanf("%d",&id); switch(id){ case 1: {printf("\n\n你調(diào)用了功能1:"); printf("\n樸素的模式匹配算法"); k=Index(A,B,j); printf("\n該位置為:"); printf("%d\n",k); break;} case 2: {printf("\n\n你調(diào)用了功能2:"); printf("\n 快速模式匹配算法"); a=Index_KMP(A,B,j); printf("\n該位置為:"); printf("%d\n",a); break;} case 3: {printf("\n\n你調(diào)用了功能3:"); printf("\n退出\n"); } } }while(id!=3);
#include<stdio.h> #include<string.h> int Index(char *S,char *T,int pos){ //返回字串T在主串S中第pos個(gè)字符之后的位置。若不存在,則函數(shù)值為0. //其中,T非空,1<=pos<=StrLength(s). int i=pos; int j=1; while(i<=S[0]&&j<=T[0]){ if(S[i]==T[j]){++i;++j;} else{i=i-j+2;j=1;} } if(j>T[0]) return i-T[0]; else return 0; } int get_next(char *T,int next[]){ //求模式串T的next函數(shù)值并存入數(shù)組next。 int i=1;next[1]=0;int j=0; while(i<T[0]){ if (j==0||T[i]==T[j]){++i;++j;next[i]=j;} else j=next[j]; } return *next; } int Index_KMP(char *S,char *T,int pos){ //利用模式串T的next函數(shù)求T在主串S中第pos個(gè)字符之后的位置的KMP算法,其中T非空,1<=pos<=StrLength(S). int next[100]; *next=get_next(T,next); int j=1,i=pos; while(i<=S[0]&&j<=T[0]){ if(j==0||S[i]==T[j]){++i;++j;} else j=next[j]; } if(j>T[0]) return i-T[0]; else return 0; } void main() { int id,j,k,i,a; printf("輸入主串、子串和匹配起始位置\n"); char A[20];char B[10]; printf("請輸入主字串內(nèi)容\n"); gets(A+1); *A=strlen(A+1); printf("請輸入子字串內(nèi)容\n"); gets(B+1); *B=strlen(B+1); printf("請輸匹配起始位置\n"); scanf("%d",&j); //printf("%d ",k); do{ printf("\n請輸入您需要的任務(wù)的序號"); printf("\n1:樸素的模式匹配算法"); printf("\n2:快速模式匹配算法"); printf("\n3:退出\n"); scanf("%d",&id); switch(id){ case 1: {printf("\n\n你調(diào)用了功能1:"); printf("\n樸素的模式匹配算法"); k=Index(A,B,j); printf("\n該位置為:"); printf("%d\n",k); break;} case 2: {printf("\n\n你調(diào)用了功能2:"); printf("\n 快速模式匹配算法"); a=Index_KMP(A,B,j); printf("\n該位置為:"); printf("%d\n",a); break;} case 3: {printf("\n\n你調(diào)用了功能3:"); printf("\n退出\n"); } } }while(id!=3); }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
欄 目:C語言
下一篇:C++實(shí)現(xiàn)一個(gè)線程安全的單例工廠實(shí)現(xiàn)代碼
本文標(biāo)題:C語言數(shù)據(jù)結(jié)構(gòu)中串的模式匹配
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1559.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁面的局部加載