首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

帮忙看一下,该如何处理

2013-03-26 
帮忙看一下#include stdio.hstruct Seqstring{char c[100]int n}typedef struct Seqstring * PSeqstr

帮忙看一下
#include <stdio.h>
struct Seqstring 
{
  char c[100];
  int n;  
  };
  typedef struct Seqstring * PSeqstring;
   
  void makenext(PSeqstring p , int next[])
  {
  int i=0,k=-1;
  next[0]=-1;
  while(i<p->n-1)
  { while(k>=0&&p->c[i]!=p->c[k]) k=next[i];
  i++;k++;
  if(p->c[i]==p->c[k]) next[i]=next[k];
  else next[i]=k;
  }
  };  
int main()
{struct Seqstring a={"BABBABAB",8};
  int array[100];
  int h;
  makenext(&a,array); 
  printf("array=");
  for(h=0;h<8;h++)
  printf("%d",array[h]);
  return 0;
}
哪里错了,怎么没结果,谢谢。

[解决办法]
void makenext(PSeqstring p , int next[])
{
int i=0,k=-1;
next[0]=-1;
while(i<p->n-1)
{ while(k>=0&&p->c[i]!=p->c[k]) k=next[i]; //这一行有问题
根据下面的代码
 if(p->c[i]==p->c[k]) next[i]=next[k];
else next[i]=k;
和你的输入
可以知道,next[i]是大于0的,这样的话,while(k>=0&&p->c[i]!=p->c[k]) k=next[i]; 就会出现死循环.

热点排行