#include const char EOS = '\0'; const int MAXPATLEN = 30; void computePrefix(char * pattern, int overlap[]) { int i,j; i = 0; j = overlap[0] = -1; for (i = 0; pattern[i] != '\0'; i ++) { overlap[i + 1] = overlap[i] + 1; while (overlap[i + 1] > 0 && pattern[i] != pattern[overlap[i + 1] - 1]) overlap[i + 1] = overlap[overlap[i + 1] - 1] + 1; } } char * search(char * pat, char * text) { int next[MAXPATLEN]; int i, j, o; int n = strlen(text); int max(int, int); if (*pat == EOS) return text; computePrefix(pat, next); for (i = 0; pat[i] != 0; i ++) cout << next[i] << " "; cout << endl; i = 0; o = 0; while (i < n) { for (j = 0; text[i + j] != '\0' && pat[j] != '\0' && text[i + j] == pat[j]; j ++); if (pat[j] == '\0') return &(text[i]); o = next[j]; i = i + max(1, j - o); } return NULL; } int max(int i, int j) { if (i > j) return i; else return j; }