#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <string.h>

#define INITSIZE 1000 #define INCRE 20 #define OK 1 #define ERROR 0

typedef struct { char *data; int length, stringsize; } SqString;

//串初始化 int initString(SqString *S) { S->data = (char *)malloc(sizeof(char) * INITSIZE); if (!S->data) return 0; S->length = 0; S->stringsize = INITSIZE; return 1;

}

//串赋值 int strAssign(SqString *s, char *str ) { int i = 0; while (*str) { s->data[i] = *str++; i++; } s->data[i] = '\0'; s->length = i; return 1;

}

//基本模式匹配算法 int index_bf(SqString *s, SqString *t, int start) { int i = start - 1, j = 0; while (j < t->length && i < s->length) { if (s->data[i] == t->data[j]) { j++; i++; } else { i = i - j + 1; j = 0; } } if (j >= t->length) return i - t->length + 1; else return 0; }

int main() { //利用模式匹配算法完成子串查找 SqString s, t; char str[INITSIZE]; char t1[INITSIZE]; if (initString(&s) && initString(&t)) { scanf("%s", str); strAssign(&s, &str[0]); scanf("%s", t1); strAssign(&t, &t1[0]); int pos = 1; int i = 0; int count = 0; while (pos <= s.length - t.length + 1 && pos != 0) { i = index_bf(&s, &t, pos); if (i == 0) { break; } printf("%d ", i); pos = ++i; count = 1; } if (count == 0) printf("0"); }

return 0;

}