这个程序错在哪里啊???

name2001 2003-07-04 01:27:47
#include<iostream.h>
#include< stdlib.h>
const int MAX=50;
typedef struct SString{
char *r;
int length;
}SString;
//一下求出子串在主串中的位置并且返回该位置的值
int Index_KMP(SString s,SString T,int pos,int next[])
{
int i=pos;
int j=1;
while(i<s.length&&j<=T.length)//主串与子串进行比较
{
if (j==0||s.r[i]==s.r[j])
{
i++;j++;
}

else j=next[j];//有next[]的值进行回溯
}
if(j>T.length) return i-T.length;//返回子串的位置
else return 0;
}
//求模式匹配的子串的next[]的值
void get_next(SString T,int next[])
{
int i=1,j;
next[1]=0;
j=0;
while(i<T.length)//求next[]的值
{if(j==0||T.r[i]==T.r[j])

{
i++;j++;
next[j]=j;
}


else j=next[j];
}
}
void main()
{
ios::sync_with_stdio();
int pos ,x;
int n;
int m;
int i,j;
int next[20];
SString s,T;
cout<<"Please enter the size of mainstring"<<endl;//输入主串的大小
cin>>n;
s.r=new char[n];
if(!s.r)
{
cout<<"alloction failed!"<<endl;
exit(0);
}
s.length=n;i=1;
cout<<"Enter mainstring:"<<endl;
while(i<n&&s.r[i++])//输入主串
{
cin>>s.r[i];
}

cout<<"Please Enter the size of sudstring:"<<endl;//输入子串的大小
cin>>m;
T.r=new char[m];
if(!T.r)
{
cout<<"Allcation Failure"<<endl;
exit(0);
}
T.length=m;j=1;
cout<<"enter the substring:"<<endl;//输入子串
while(j<m&&T.r[j++])
{
cin>>T.r[j];
}
cout<<"Enter the find position:"<<endl;
cin>>pos;
get_next(T,next);//求next[]的值

x=Index_KMP(s,T,pos,next);//求子串在主串中的位置
if(x)
{
cout<<"Not Found!"<<endl;
}
else cout<<"The position of substring in mainstring is "<<x<<endl;

}
...全文
82 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧