详解
#include<iostream>
#include<algorithm>
#include <cstring>
#include <string>
#include<cstdio>
using namespace std;int next[1000],num=0;
void GetNextval(char* p, int next[])
{int pLen = strlen(p);next[0] = -1;int k = -1;int j = 0;while (j < pLen - 1){if (k == -1 || p[j] == p[k]){j++;k++;if (p[j] != p[k])next[j] = k;elsenext[j] = next[k];}else{k = next[k];}}
}
int KmpSearch(char* s, char* p)
{int i = 0,j = 0;int sLen = strlen(s);int pLen = strlen(p);while (i < sLen && j < pLen){if (j == -1 || s[i] == p[j]){i++;j++;}else{j = next[j];}}if (j == pLen)return i - j;elsereturn -1;
}
int main()
{char a[1000],b[1000];while(~scanf("%s %s",a,b)){GetNextval(b,next);cout<<KmpSearch(a,b)<<endl;}return 0;
}