729063706 2017-04-12 09:33 采纳率: 0%
浏览 837

c++ 的kmp代码,大神求指导哪里错了

#include
#include
#include
using namespace std;
char s1[1000000];
char s2[1000];
int next[1000];
void read()
{
char temp[1000000];
scanf("%s",&temp);
int len=strlen (temp);
s1[0]=len;
for (int i=0;i<len;i++)
s1[i+1]=temp[i];
scanf("%s",&temp);
len=strlen (temp);
s2[0]=len;
for (int i=0;i<len;i++)
s2[i+1]=temp[i];
return;
}
void kmp_next()
{
int i=1;
int j=0;
next[1]=0;
while (i<s2[0])
{
if (j==0||s2[i]==s2[j])
{
i++;
j++;
next[i]=j;
}
else j=next[j];
}
return;
}
void kmp()
{
int i=1;
int j=1;
while (j<=s1[0])
{
if (j==1||s1[i]==s1[j])
{
i++;
j++;
}
else i=next[i];
if (i==s2[0]&&s1[i]==s1[j])
{
printf ("%d\n",j);
}

}
return;

}
void pri_next()
{
for (int i=0;i<s2[0];i++)
printf ("%d ",next[i]);
}
int main()
{
read();
kmp_next();
kmp();
pri_next();
return 0;
}
//要求输出next数组

  • 写回答

1条回答 默认 最新

  • AlbertS 博客专家认证 2017-04-12 12:53
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog