Pattern matching of strings in C language data structures

  • 2020-05-19 05:18:52
  • OfStack

Pattern matching of strings in C language data structures

String pattern matching problem: naive algorithm and KMP algorithm


#include<stdio.h>
#include<string.h>
int Index(char *S,char *T,int pos){
// Returns the string T In the main string S In the first pos The position after the number of characters. If it does not exist, then the value of the function is 0.
// Among them, T Is not empty, 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[]){
// O pattern string T the next The value of the function is stored in an array 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){
// Leverage pattern strings T the next The function o T In the main string S In the first pos Of the position after the number of characters KMP Algorithm, where T Is not empty, 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(" Enter the main string, substring, and match the starting position \n");
char A[20];char B[10];
printf(" Please enter the main string content \n");
    gets(A+1);
    *A=strlen(A+1);
printf(" Please enter the substring content \n");
    gets(B+1);
    *B=strlen(B+1);
printf(" Please enter a match starting position \n");
    scanf("%d",&j);
//printf("%d ",k);
    do{
       printf("\n Please enter the sequence number of the task you need ");
       printf("\n1: Naive pattern matching algorithm ");
       printf("\n2: Fast pattern matching algorithm ");
       printf("\n3: exit \n");
       scanf("%d",&id);
    switch(id){
       case 1:
           {printf("\n\n You called the function 1:");
           printf("\n Naive pattern matching algorithm ");
           k=Index(A,B,j);
           printf("\n The location is: ");
           printf("%d\n",k);
           break;}     
       case 2:
           {printf("\n\n You called the function 2:");
              printf("\n  Fast pattern matching algorithm ");
              a=Index_KMP(A,B,j);
              printf("\n The location is: ");
              printf("%d\n",a);
              break;}
       case 3:
           {printf("\n\n You called the function 3:");
              printf("\n exit \n");
              }
    }
       }while(id!=3);

#include<stdio.h>
#include<string.h>
int Index(char *S,char *T,int pos){
// Returns the string T In the main string S In the first pos The position after the number of characters. If it does not exist, then the value of the function is 0.
// Among them, T Is not empty, 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[]){
// O pattern string T the next The value of the function is stored in an array 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){
// Leverage pattern strings T the next The function o T In the main string S In the first pos Of the position after the number of characters KMP Algorithm, where T Is not empty, 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(" Enter the main string, substring, and match the starting position \n");
char A[20];char B[10];
printf(" Please enter the main string content \n");
    gets(A+1);
    *A=strlen(A+1);
printf(" Please enter the substring content \n");
    gets(B+1);
    *B=strlen(B+1);
printf(" Please enter a match starting position \n");
    scanf("%d",&j);
//printf("%d ",k);
    do{
       printf("\n Please enter the sequence number of the task you need ");
       printf("\n1: Naive pattern matching algorithm ");
       printf("\n2: Fast pattern matching algorithm ");
       printf("\n3: exit \n");
       scanf("%d",&id);
    switch(id){
       case 1:
           {printf("\n\n You called the function 1:");
           printf("\n Naive pattern matching algorithm ");
           k=Index(A,B,j);
           printf("\n The location is: ");
           printf("%d\n",k);
           break;}     
       case 2:
           {printf("\n\n You called the function 2:");
              printf("\n  Fast pattern matching algorithm ");
              a=Index_KMP(A,B,j);
              printf("\n The location is: ");
              printf("%d\n",a);
              break;}
       case 3:
           {printf("\n\n You called the function 3:");
              printf("\n exit \n");
              }
    }
       }while(id!=3);
 
}

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: