C-Language implementation of the string pattern matching algorithm KMP

Source: Internet
Author: User

#include "string.h"
#include "stdio.h"
#include "Stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 100/* Storage space Initial allocation */

typedef int STATUS;/* Status is the type of function whose value is the function result status code, such as OK, etc. */
typedef int elemtype;/* Elemtype Type is based on the actual situation and is assumed to be int */

typedef char STRING[MAXSIZE+1]; /* Unit No. 0 The length of the string */

/* Generate a string whose value equals chars t/*
Status strassign (String t,char *chars)
{
int i;
if (strlen (chars) >maxsize)
return ERROR;
Else
{
T[0]=strlen (chars);
for (i=1;i<=t[0];i++)
t[i]=* (chars+i-1);
return OK;
}
}

Status clearstring (String S)
{
s[0]=0;/* string length is zero */
return OK;
}

/* Output string T. */
void Strprint (String T)
{
int i;
for (i=1;i<=t[0];i++)
printf ("%c", T[i]);
printf ("\ n");
}

/* Output next array value. */
void nextprint (int next[],int length)
{
int i;
for (i=1;i<=length;i++)
printf ("%d", next[i]);
printf ("\ n");
}

/* Returns the number of elements of a string */
int Strlength (String S)
{
return s[0];
}

/* Simple pattern matching method */
int Index (string S, string T, int pos)
{
int i = pos;/* I is used for the current position subscript value in the main string s, and if POS is not 1, the match is started from the POS position */
Int J = 1;/* J for current position subscript value in substring t */
while (i <= s[0] && J <= t[0])/* If I is less than the length of S and J is less than the length of T, the loop continues */
{
if (s[i] = = T[j])//* Two letters equal then continue */
{
++i;
++j;
}
else/* Pointer back to start matching */
{
i = i-j+2;/* I back to the next one in the first match */
j = 1; /* J return to the first of the substring T */
}
}
if (J > t[0])
return i-t[0];
Else
return 0;
}

/* Returns the next array of substring t by calculating. */
void Get_next (String T, int *next)
{
int i,j;
I=1;
j=0;
next[1]=0;
while (i<t[0])/* Here T[0] indicates the length of the string T */
{
if (j==0 | | t[i]== T[j]//T[i] represents a single character of the suffix, t[j] represents a single character of the prefix */
{
++i;
++j;
Next[i] = j;
}
Else
J= next[j];/* If the characters are not the same, the J value goes back */
}
}

/* Returns the position of the substring T after the first POS character in the main string s. If it does not exist, the function returns a value of 0. */
/* t non-empty, 1≤pos≤strlength (S). */
int INDEX_KMP (string S, string T, int pos)
{
int i = pos;/* I is used for the current position subscript value in the main string s, and if POS is not 1, the match is started from the POS position */
Int J = 1;/* J for current position subscript value in substring t */
int next[255];/* defines a next array */
Get_next (t, next);/* for string T analysis, get next array */
while (i <= s[0] && J <= t[0])/* If I is less than the length of S and J is less than the length of T, the loop continues */
{
if (j==0 | | S[i] = = T[j])/* Two letters equal to continue, with the naïve algorithm added j=0 judgment */
{
++i;
++j;
}
else/* Pointer back to start matching */
j = next[j];/* J return to the appropriate position, I value is unchanged */
}
if (J > t[0])
return i-t[0];
Else
return 0;
}

/* The next function of the pattern string T is corrected and deposited into the array nextval */
void Get_nextval (String T, int *nextval)
{
int i,j;
I=1;
J=0;
nextval[1]=0;
while (i<t[0])/* Here T[0] indicates the length of the string T */
{
if (j==0 | | t[i]== T[j]//T[i] represents a single character of the suffix, t[j] represents a single character of the prefix */
{
++i;
++j;
if (t[i]!=t[j])/* If the current character differs from the prefix character */
Nextval[i] = j;/* The current j is the value of nextval at I position */
Else
Nextval[i] = nextval[j];/* If the prefix character is the same, the */
/* nextval value of the prefix character is assigned to the value of nextval at the i position */
}
Else
j= nextval[j];/* If the characters are not the same, then J value backtracking */
}
}

int Index_kmp1 (string S, string T, int pos)
{
int i = pos;/* I is used for the current position subscript value in the main string s, and if POS is not 1, the match is started from the POS position */
Int J = 1;/* J for current position subscript value in substring t */
int next[255];/* defines a next array */
Get_nextval (t, next);/* for string T analysis, get next array */
while (i <= s[0] && J <= t[0])/* If I is less than the length of S and J is less than the length of T, the loop continues */
{
if (j==0 | | S[i] = = T[j])/* Two letters equal to continue, with the naïve algorithm added j=0 judgment */
{
++i;
++j;
}
else/* Pointer back to start matching */
j = next[j];/* J return to the appropriate position, I value is unchanged */
}
if (J > t[0])
return i-t[0];
Else
return 0;
}

int main ()
{
int i,*p;
String s1,s2;

Strassign (S1, "Abcdex");
printf ("Sub-string:");
Strprint (S1);
I=strlength (S1);
p= (int*) malloc ((i+1) *sizeof (int));
Get_next (S1,P);
printf ("Next is:");
Nextprint (P,strlength (S1));
printf ("\ n");

Strassign (S1, "ABCABX");
printf ("Sub-string:");
Strprint (S1);
I=strlength (S1);
p= (int*) malloc ((i+1) *sizeof (int));
Get_next (S1,P);
printf ("Next is:");
Nextprint (P,strlength (S1));
printf ("\ n");

Strassign (S1, "Ababaaaba");
printf ("Sub-string:");
Strprint (S1);
I=strlength (S1);
p= (int*) malloc ((i+1) *sizeof (int));
Get_next (S1,P);
printf ("Next is:");
Nextprint (P,strlength (S1));
printf ("\ n");

Strassign (S1, "Aaaaaaaab");
printf ("Sub-string:");
Strprint (S1);
I=strlength (S1);
p= (int*) malloc ((i+1) *sizeof (int));
Get_next (S1,P);
printf ("Next is:");
Nextprint (P,strlength (S1));
printf ("\ n");

Strassign (S1, "Ababaaaba");
printf ("Sub-string:");
Strprint (S1);
I=strlength (S1);
p= (int*) malloc ((i+1) *sizeof (int));
Get_next (S1,P);
printf ("Next is:");
Nextprint (P,strlength (S1));
Get_nextval (S1,P);
printf ("Nextval for:");
Nextprint (P,strlength (S1));
printf ("\ n");

Strassign (S1, "Aaaaaaaab");
printf ("Sub-string:");
Strprint (S1);
I=strlength (S1);
p= (int*) malloc ((i+1) *sizeof (int));
Get_next (S1,P);
printf ("Next is:");
Nextprint (P,strlength (S1));
Get_nextval (S1,P);
printf ("Nextval for:");
Nextprint (P,strlength (S1));

printf ("\ n");

Strassign (S1, "00000000000000000000000000000000000000000000000001");
printf ("Main string is:");
Strprint (S1);
Strassign (S2, "0000000001");
printf ("Sub-string:");
Strprint (S2);
printf ("\ n");
printf ("Main string and substring prompt first match in%d characters (naïve pattern matching algorithm) \ n", Index (s1,s2,1));
printf ("Main string and substring prompt first match in%d characters (KMP algorithm) \ n", INDEX_KMP (s1,s2,1));
printf ("Main string and substring prompt first match in%d characters (KMP improved algorithm) \ n", Index_kmp1 (s1,s2,1));

return 0;
}

C-Language implementation of the string pattern matching algorithm KMP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.