SString
#define MaxLen 255
typedef struct {
char ch[MaxLen];
int length;
} SString;
typedef struct {
char *ch;
int length;
} HString;
int Index(SString S, SString T, int pos) {
int i = pos, j = 1;
while (i <= S.length && j <= T.length) {
if (S.ch[i] == T.ch[j]) {
i++;
j++;
} else {
i = ++pos;
// i = i - j + 2;
j = 1;
}
}
if (j > T.length) return pos;
return 0;
}
void get_next(SString T, int next[]) {
int i = 1, j = 0;
next[1] = 0;
while (i < T.length) {
if (j == 0 || T.ch[i] == T.ch[j]) {
i++;
j++;
next[i] = j;
} else { j = next[j]; }
}
}
int Index_KMP(SString S, SString T, int next[], int pos) {
int i = pos, j = 1;
while (i <= S.length && j <= T.length) {
if (j == 0 || S.ch[i] == T.ch[j]) {
i++;
j++;
} else { j = next[j]; }
}
if (j > T.length) { return i - T.length; }
else { return 0; }
}