The most common approach is to use the KMP string matching algorithm:
#include <stdio.h> #include <stdlib.h> #include <string.h> int get_nextval (char *pattern, int next[])
{//get the next value of the pattern int i = 0, j =-1;
Next[0] =-1;
int Patlen = strlen (pattern);
while (I < patlen-1) {if (j = = 1 | | pattern[i] = = Pattern[j]) {++i;
++j;
if (Pattern[i]!= pattern[j]) next[i] = j;
else next[i] = next[j];
else J = Next[j];
return (0);
int Kmpindex (char *target, char *pattern, int pos) {int tari = pos, Pati = 0;
int tarlen = strlen (target), Patlen = strlen (pattern);
int *next = (int *) malloc (Patlen * sizeof (int));
Get_nextval (pattern, next); while (Tari < Tarlen && Pati < Patlen) {if (Pati = 1 | |
Target[tari] = = Pattern[pati]) {++tari;
++pati;
}else{Pati = Next[pati];
} if (next!= NULL) free (next);
Next = NULL;
if (Pati = = Patlen) return tari-pati;
else return-1;
}
int main () {char target[50], pattern[50];
printf ("Imput the target:\n");
scanf ("%s", target);
printf ("Imput the pattern:\n");
scanf ("%s", pattern);
int ans = kmpindex (target,pattern,0);
if (ans = = 1) printf ("error\n");
else printf ("index:%d\n", ans);
return 0;
}
Exercises
Topic Description:
Read the data string[] and read a short string. Requires lookup for all matches in string[] and short strings, output line numbers, matching strings. Matches are case-insensitive and can have a pattern match expressed in brackets. such as "AA[123]BB", that is, AA1BB, AA2BB, aa3bb all match.
Input:
Multiple sets of data are entered.
Enter N (1<=n<=1000) in the first row of each set of data, enter n strings (without spaces) from the second line, then enter a matching string.
Output:
The line number of the string that the output matches to and the string (case-insensitive when matched).
Sample input:
4
Aab
A2b
Ab
Abb
A[a2b]b
Sample output:
1 Aab
2 A2B
4 ABB
AC Code
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 1001 #define
LEN struct STR {char name[101];
};
int main () {struct str strs[max];
struct STR T[len];
int i, N, Len, J, K, left, right, count, flag;
Char Text[len], Newtext[len];
while (scanf ("%d", &n)!= EOF) {//Receive data GetChar ();
for (i = 0; i < n; i + +) {scanf ("%s", strs[i].name);
///Receive text string GetChar ();
Gets (text);
Len = strlen (text);
for (i = left = right = 0 i < len; i + +) {if (text[i] = = ' [') {left = i;
else if (text[i] = = ' = ') {right = I;
Break
} count = Right-left-1; if (count <= 0) {//No regular match for (i = j = 0; i < len; i + +) {if (Text[i]!= ' [' && text [i]!= '] {NewText[j + +] = Text[i];
} Newtext[j] = ' I '; for (i = 0; i < n; i + +) {if (strcasecmp (strs[i].name, newtext) = = 0) {printf ("%d%s\n", i +
1, Strs[i].name); }}else {//requires a regular match for (j = 1, k = 0; J <= Count J + +, K + +) {//Build text array memset
(T[k].name, ' sizeof ', t[k].name);
for (i = 0; i < left; I + +) {T[k].name[i] = Text[i];
} T[k].name[i] = Text[left + j];
strcat (t[k].name, text + right + 1);
//Regular match for (i = 0; i < n; i + +) {for (j = flag = 0; J < Count; J + +) {
if (strcasecmp (strs[i].name, t[j].name) = = 0) {flag = 1;
Break
} if (flag) {printf ("%d%s\n", i + 1, strs[i].name);
return 0;
}
/**************************************************************
problem:1165
User:wangzhengyi
& nbsp; language:c
result:accepted
time:0 Ms
memory:948 kb
****************************************************************/