附上一个题目 HDU1711
#include<cstdio>
const int maxn = 1e7;
int next[maxn];
char p[maxn];
char s[maxn];
#include<cstring>
void get_next(int ltp)
{
int i=1,j=0;
next[0] = -1;
while(i<=ltp)
{
if(j==-1||p[i]==p[j])
{
i++;
j++;
next[i] = j;
}
else
{
j = next[j];
}
}
}
int cast(int ltp,int plen)
{
get_next(strlen(p)-1);
int i=0,j=0;
while(i<=ltp)
{
if(j==-1||s[i]==p[j])
{
i++;j++;
}
else
{
j = next[j];
}
if(j==plen)
return i-plen+1;
}
return -1;
}
int main()
{
while(1)
{
scanf("%s%s",p,s);
printf("%d\n",cast(strlen(s)-1,strlen(p)));
}
return 0;
}