The algorithm for substring position is called pattern matching algorithm.
There are many kinds of pattern matching algorithms, which are more common, such as violent matching algorithm and KMP algorithm. The time complexity of the violence matching algorithm is O (m*n) and the KMP time complexity is O (m+n).
Here are the code for the two algorithms
Welcome to the point of lack of code
KMP.cpp: Defines the entry point for a console application.
Ask the question of the substring. #include "stdafx.h" #include <cstring> #include <cstdlib>//violence matching algorithm, time complexity is O (m*n)//To start substring from POS position in parent string s t position static int runtimes = 0;//execution times ... int normal (char * S,char * t,int pos) {int slen = strlen (s);//The position of the parent string int tlen = St
Rlen (t);//substring length int i = POS-1,J = 0;//Compute starting value int first = i;//match start position runtimes = 0;
while (i <slen && J < Tlen) {runtimes++;
if (s[i] = = T[j]) {//equal i++,j++;
else{//is not equal, I = match the starting value +1, j = 0 i = ++first;
j = 0;
} if (J >= tlen) {//Match succeeded.
return first+1;
else{//match unsuccessful, return-1 return-1;
}//Normal to Next void GetNext1 (char * t,int len,int * Next) {Next[0] = -1;//Initial value ...
int i = 0,j =-1;
while (I < len-1) {if (j = = 1 | | t[i] = = T[j]) {i++,j++;
Next[i] = j;
} else{j = next[j];
}}//Find next to improve void GetNext2 (char * t,int len,int * next) {Next[0] = -1;//Initial value ...
int i = 0,j =-1;
while (I < len-1) {if (j = = 1 | | t[i] = = T[j]) {i++,j++; if (T[i]!= t[j]) {next[i] = j;
} else{Next[i] = next[j];
} else{j = next[j];
//KMP pattern matching method.
int KMP (char * s,char * t,int pos,int fun) {int slen = strlen (s);
int tlen = strlen (t);
int i = pos-1,j = 0; int * next = (int *) malloc (sizeof (int) * Tlen);//next array if (fun = 1) {getNext1 (t,tlen,next);//Calculate Next} else{get
NEXT2 (T,tlen,next);//Compute Next} runtimes = 0;
while (I < Slen && J < Tlen) {runtimes + +;
if (j = = 1 | | s[i] = = T[j]) {i++,j++;
} else{//i value does not backtrack, j = next[j] j = next[j];
} free (next)//release space ...
if (J >= tlen) {return i-j + 1;
} return-1; //print Info//kind, 1: Violent match 2:KMP 3. Improved KMP void printmsg (char * string,int index,int kind) {if (index!=-1) {char *
Point = string + index-1;
char *s = NULL;
if (kind = = 1) {s = "violent match";
else if (kind = = 2) {s = "KMP match";
} else{s = "Improved KMP matching";
printf ("%s get string as:%s,\t execution Count%d\n", s,point,runtimes); } int _tmAin (int argc, _tchar* argv[]) {char * string = "ABCDEFGHIJKLMNSDFDSDFSFDSD";
char * sub = "SDFD";
int index = normal (string,sub,1);
Printmsg (string,index,1);
index = KMP (string,sub,1,1);
Printmsg (string,index,2);
index = KMP (string,sub,1,2);
Printmsg (string,index,3);
string = "0000000001100000000011000000000111000000000001111AA";
Sub = "000000000001111";
index = Normal (string,sub,1);
Printmsg (string,index,1);
index = KMP (string,sub,1,1);
Printmsg (string,index,2);
index = KMP (string,sub,1,2);
Printmsg (string,index,3);
return 0;
}
Reference book: Min "Data structure." C language Edition "