text字符串中找到pat模式串。
//KMP算法
#include <string.h>
void setup(char pat[], int fail[])
{
int length = strlen(pat);
int i, j;
fail[0] = -1;
for (i = 1; i < length; i++) {
for (j=fail[i-1]; j>=0 && pat[j+1]!=pat[i]; j=fail[j])
;
fail[i] = (j < 0 && pat[j+1] != pat[i]) ? -1 : j+1;
}
}
int KMP(char text[], char pat[], int fail[])
{
int t_length = strlen(text);
int p_length = strlen(pat);
int t, p;
setup(pat, fail); /* build up the fail func. */
for (t = p = 0; t < t_length && p < p_length; )
if (text[t] != pat[p]) /* if not match … */
if (p > 0) /* is it the 1st location? */
p = fail[p-1] + 1; /* no, shift right */
else
t++; /* or match next in text[] */
else
t++, p++; /* matched, advance both */
return (p >= p_length) ? t – p_length : -1;
}
/* —————————————————— */
#include <stdio.h>
#define MAXSIZE 100
void main(void)
{
char text[MAXSIZE];
char pat[MAXSIZE];
int fail[MAXSIZE];
int answer, i;
printf(“\nKnuth-Morris-Pratt String Search Program”);
printf(“\n========================================”);
printf(“\n\nText String ——> “);
gets(text);
printf( “\nPattern String —> “);
gets(pat);
if ((answer = KMP(text, pat, fail)) >= 0) {
printf(“\n “);
for (i = 1; i <= 6; i++)
printf(” %1d”, i);
printf(“\n”);
for (i = 1; i <= 6; i++)
printf(“0….5….”);
printf(“0”);
printf(“\n%s\n”, text);
for (i = 0; i < answer; i++)
printf(” “);
printf(“%s”, pat);
printf(“\n\nPattern Found at location %d”, answer);
}
else
printf(“\nPattern NOT found”);
}
//BM算法
#include <string.h> /* for strlen() */
#define NOT_FOUND -1
void get_jump(unsigned char [], int []);
int BM(unsigned char [], unsigned char []);
int BM(unsigned char text[], unsigned char pat[])
{
int jump_tab[256];
int t_len = strlen(text);
int p_len = strlen(pat);
int i, j, k;
get_jump(pat, jump_tab); /* compute the jump table */
for (i = p_len – 1; i < t_len; ) { /* comp. downward */
for (j=p_len-1, k=i; j >= 0 && text[k] == pat[j]; k–,j–)
;
if (j < 0) /* if pat[] exhausted ? */
return k + 1; /* YES, pattern found. */
else /* NO, update using jump_tab*/
i += jump_tab[text[i]];
}
return NOT_FOUND; /* text[] exhausted. */
}
void get_jump(unsigned char pat[], int jump_tab[])
{
int length = strlen(pat);
int i;
for (i = 1; i < 256; i++) /* assume length is the gap*/
jump_tab[i] = length;
for (i = 0; i < length – 1; i++) /* now adjust them. */
jump_tab[(int)pat[i]] = length – i – 1;
}
/* —————————————————— */
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
void main(void)
{
unsigned char text[MAXSIZE];
unsigned char pat[MAXSIZE];
int answer, i;
printf(“\nBoyer-Moore String Searching Program”);
printf(“\n====================================”);
printf(“\n\nText String –> “);
gets(text);
printf( “\nPattern String –> “);
gets(pat);
if ((answer = BM(text, pat)) >= 0) {
printf(“\n “);
for (i = 1; i <= 6; i++)
printf(” %1d”, i);
printf(“\n”);
for (i = 1; i <= 6; i++)
printf(“0….5….”);
printf(“0”);
printf(“\n%s\n”, text);
for (i = 0; i < answer; i++)
printf(” “);
printf(“%s”, pat);
printf(“\n\nPattern Found at location %d”, answer);
}
else
printf(“\nPattern NOT FOUND.”);
}