数据结构实验之串一:KMP简单应用
Problem Description 给定两个字符串string1和string2,判断string2是否为string1的子串。 Input 输入包含多组数据,每组测试数据包含两行,第一行代表string1(长度小于1000000),第二行代表string2(长度小于1000000),string1和string2中保证不出现空格。 Output 对于每组输入数据,若string2是string1的子串,则输出string2在string1中的位置,若不是,输出-1。 Sample Input abc a 123456 45 abc ddd Sample Output 1 4 -1 Hint Source #include <stdio.h> #include <stdlib.h> #include <string.h> int next[1000001]; void GetNextarr(char *p) // 计算next数组 { int len = strlen(p); next[0] = -1; // 初始化next[0]为-1 int k = -1; int j = 0; while(j < len - 1) { // p[k]表示前缀,p[j]表示后缀 if (k == -1 || p[j] == p[k]) { k++; j++; next[j] = k; //