解题思路:
注意事项:
参考代码:
#include <cstdio>
#include <cstring>
char s1[1000005],s2[10005];
int next[10005];
int lens1,lens2;
void getnext()
{
int i=0,j=-1;
next[0]=-1;
while(i<lens2)
{
if(j==-1||s2[i]==s2[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
int KMP()
{
getnext();
int i=0,j=0;
while(i<lens1)
{
if(j==-1||s1[i]==s2[j])
{
i++;
j++;
}
else
j=next[j];
if(j==lens2)
{
return i-lens2+1;
}
}
return 0;
}
int main()
{
while(scanf("%s %s",s1,s2)!=EOF)
{
getchar();
lens1=strlen(s1);
lens2=strlen(s2);
printf("%d\n",KMP());
}
return 0;
}
0.0分
0 人评分
C二级辅导-分段函数 (C语言代码)浏览:566 |
C语言程序设计教程(第三版)课后习题12.1 (C语言代码)浏览:987 |
C语言训练-求s=a+aa+aaa+aaaa+aa...a的值 (C语言代码)浏览:591 |
大神老白 (C语言代码)浏览:715 |
【明明的随机数】 (C++代码)浏览:781 |
C语言程序设计教程(第三版)课后习题8.4 (C语言代码)浏览:529 |
字符串输入输出函数 (C语言代码)浏览:2495 |
C语言程序设计教程(第三版)课后习题10.1 (C语言代码)浏览:537 |
整除问题 (C语言代码)浏览:526 |
A+B for Input-Output Practice (I) (C语言代码)浏览:431 |