用c語言實現數據結構中的幾個經典程序:串的模式匹配

 (2011-06-24 07:48:28)
標籤: 

轉載

 

串的模式匹配問題:樸素算法與KMP算法

#include<stdio.h>
#include<string.h>
int Index(char *S,char *T,int pos){
//返回字串T在主串S中第pos個字符之後的位置。若不存在,則函數值爲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函數值並存入數組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函數求T在主串S中第pos個字符之後的位置的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");
       gets(A+1);
       *A=strlen(A+1);
printf("請輸入子字串內容n");
       gets(B+1);
       *B=strlen(B+1);
printf("請輸匹配起始位置n");
       scanf("%d",&j);
//printf("%d ",k);
       do{
              printf("n請輸入您需要的任務的序號");
              printf("n1:樸素的模式匹配算法");
              printf("n2:快速模式匹配算法");
              printf("n3:退出n");
              scanf("%d",&id);
       switch(id){
              case 1:
                     {printf("nn你調用了功能1:");
                     printf("n樸素的模式匹配算法");
                     k=Index(A,B,j);
                     printf("n該位置爲:");
                     printf("%dn",k);
                     break;}          
              case 2:
                     {printf("nn你調用了功能2:");
                            printf("n 快速模式匹配算法");
                            a=Index_KMP(A,B,j);
                            printf("n該位置爲:");
                            printf("%dn",a);
                            break;}
              case 3:
                     {printf("nn你調用了功能3:");
                            printf("n退出n");
                            }
       }
              }while(id!=3);
#include<stdio.h>
#include<string.h>
int Index(char *S,char *T,int pos){
//返回字串T在主串S中第pos個字符之後的位置。若不存在,則函數值爲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函數值並存入數組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函數求T在主串S中第pos個字符之後的位置的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");
       gets(A+1);
       *A=strlen(A+1);
printf("請輸入子字串內容n");
       gets(B+1);
       *B=strlen(B+1);
printf("請輸匹配起始位置n");
       scanf("%d",&j);
//printf("%d ",k);
       do{
              printf("n請輸入您需要的任務的序號");
              printf("n1:樸素的模式匹配算法");
              printf("n2:快速模式匹配算法");
              printf("n3:退出n");
              scanf("%d",&id);
       switch(id){
              case 1:
                     {printf("nn你調用了功能1:");
                     printf("n樸素的模式匹配算法");
                     k=Index(A,B,j);
                     printf("n該位置爲:");
                     printf("%dn",k);
                     break;}          
              case 2:
                     {printf("nn你調用了功能2:");
                            printf("n 快速模式匹配算法");
                            a=Index_KMP(A,B,j);
                            printf("n該位置爲:");
                            printf("%dn",a);
                            break;}
              case 3:
                     {printf("nn你調用了功能3:");
                            printf("n退出n");
                            }
       }
              }while(id!=3);
 
}
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章