KMP算法算字串位置

wea111222333 2014-10-27 08:49:12
#include <iostream>
using namespace std;
#include <conio.h>

/*#include <string>
#include <iomanip>
#include <fstream>*/


#define MAXSTRLEN 255
typedef unsigned char SString[MAXSTRLEN+1];
#define OK 1
#define ERROR 0
#define OVERFLOW -2


typedef struct{ //顺序串定义
char s[MAXSTRLEN+1];
int length;
}HString;



int Index(SString S,SString T,int pos) //BF算法
{
int i,j;
i=pos;
j=1;
while (i<=S[0] && j<=T[0])
{
if (S[i]=T[j]) {++i; ++j; }
else{ i=i-j+2; j=1; }
if (j>T[0]) return i-T[0];
else return 0;
}
}



void get_next(SString T, int next[] ) //计算next
{
int i,j;
i=1;
next[1]=0;
j=0;
while (i<T[0])
{
if (j==0||T[i]==T[j])
{++i; ++j; next[i]=j; }
else j=next[j];
}
}


void get_nextval(SString T,int nextval[]) //计算nextval
{
int i,j;
i=1;
nextval[1]=0;
j=0;
while (i<T[0])
{
if (j==0||T[i]==T[j])
{
++i;
++j;
if (T[i]!=T[j]) nextval[i]=j;
else nextval[i]=nextval[j];
}
else j=nextval[j];
}
}


int Index_KMP(SString S, SString T, int pos, int next[]) //KMP算法
{
int i,j;
i=pos;
j=1;
while (i<=S[0]&&j<=T[0])
{
if (j==0||S[i]==T[j]) { ++i; ++j; }
else j=next[j];
}
if (j>T[0]) return i-T[0];
else return 0;
}


int creat(char S[],int &k) //串的输入
{
cout<<"请输入";
char a;
int i=1;
while((a=getch())!='\r')
{
cout<<a;
S[i]=a;
++i;
}
k=i;
S[0]=i+48;
cout<<endl;
cout<<"**************输入成功*************"<<endl;
return 1;
}


int main()
{

HString H,H1;
int i,*n1;
cout<<"*********************实验三 : 求字串位置*******************"<<endl;
cout<<"************1.创建主串************"<<endl;
cout<<"************2.创建子串************"<<endl;
cout<<"************3.计算next的值********"<<endl;
cout<<"************4.计算nextval的值*****"<<endl;
cout<<"************5.KMP算法计算字串位置*"<<endl;
int choose;
choose=-1;
while(choose!=0)
{
cout<<"请选择";
cin>>choose;
switch(choose)
{
case 1:
creat(H.s,H.length);
break;

case 2: creat(H1.s,H1.length);
break;

case 3:
n1=new int[5];
get_next(H1.s,n1);
break;
case 4: /*for(i=1;i<H1.length;i++)
{ cout<<n1[i]<<endl;
}*/
break;
}
}
return 1;
}

error C2664: 'get_next' : cannot convert parameter 1 from 'char [255]' to 'unsigned char []'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 编译产生问题 请问是什么原因
...全文
76 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
罗博士 2014-10-27
  • 打赏
  • 举报
回复
写的多清楚啊。 error C2664: 'get_next' : cannot convert parameter 1 from 'char [255]' to 'unsigned char []' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 不能做这种类型转换。

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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