SCAU华南农业大学数据结构8592 KMP算法

#include <iostream>
using namespace std;
#include "stdio.h"
#include "stdlib.h"
#define TRUE  1
#define FALSE  0
#define OK  1
#define ERROR  0
#define INFEASLBLE  -1
#define OVERFLOW  -2
#define MAXSTRLEN  255 	//用户可在255以内定义最大串长
typedef unsigned char SString[MAXSTRLEN+1];	//0号单元存放串的长度

void get_next(SString T,int next[])
{
// 算法4.7
// 求模式串T的next函数值并存入数组next
   // 请补全代码
   
	int i=1,j=0;
	next[1]=0;
	while(i<T[0])
	{
		if(j==0||T[i]==T[j])
		{
			i++;
			j++;
			next[i]=j;
		}
		else
			j=next[j];
	}
}

int Index_KMP(SString S,SString T,int pos,int next[]){
// 算法4.6
// 利用模式串T的next函数求T在主串S中第pos个字符之后的位置
// KMP算法。请补全代码
	int 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 main()
{
	SString T,S;
	int i,j,n;
	char ch;
	int pos;
	int next[MAXSTRLEN];
	scanf("%d",&n);    // 指定n对需进行模式匹配的字符串
	ch=getchar();
	for(j=1;j<=n;j++)
	{
		ch=getchar();
		for( i=1;i<=MAXSTRLEN&&(ch!='\n');i++)    // 录入主串
		{
			S[i]=ch;
	  		ch=getchar();
		}
		S[0]=i-1;    // S[0]用于存储主串中字符个数
		ch=getchar();
		for( i=1;i<=MAXSTRLEN&&(ch!='\n');i++)    // 录入模式串
		{
	  		T[i]=ch;
	  		ch=getchar();
		}
		T[0]=i-1;    // T[0]用于存储模式串中字符个数
		get_next(T,next);
		pos=Index_KMP(S,T,1,next);    // 请填空
		printf("%d\n",pos);
	}
	return 0;
}

在main函数里面定义了int next[MAXSTRLEN];然后在调用get_next和Index_KMP的时候把数组传进去。

我试过在定义全局数组int next[MAXSTRLEN],然后调用get_next和Index_KMP不传数组。在自己的IDE上面跑是没问题的,但是提交上去就编译错误了。暂时没搞懂。

下面是OJ判编译错误的代码

#include <iostream>
using namespace std;
#include "stdio.h"
#include "stdlib.h"
#define TRUE  1
#define FALSE  0
#define OK  1
#define ERROR  0
#define INFEASLBLE  -1
#define OVERFLOW  -2
#define MAXSTRLEN  255 	//用户可在255以内定义最大串长
typedef unsigned char SString[MAXSTRLEN+1];	//0号单元存放串的长度
int next[MAXSTRLEN];

void get_next(SString T)
{
// 算法4.7
// 求模式串T的next函数值并存入数组next
   // 请补全代码
   
	int i=1,j=0;
	next[1]=0;
	while(i<T[0])
	{
		if(j==0||T[i]==T[j])
		{
			i++;
			j++;
			next[i]=j;
		}
		else
			j=next[j];
	}
}

int Index_KMP(SString S,SString T,int pos){
// 算法4.6
// 利用模式串T的next函数求T在主串S中第pos个字符之后的位置
// KMP算法。请补全代码
	int 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 main()
{
	SString T,S;
	int i,j,n;
	char ch;
	int pos;

	scanf("%d",&n);    // 指定n对需进行模式匹配的字符串
	ch=getchar();
	for(j=1;j<=n;j++)
	{
		ch=getchar();
		for( i=1;i<=MAXSTRLEN&&(ch!='\n');i++)    // 录入主串
		{
			S[i]=ch;
	  		ch=getchar();
		}
		S[0]=i-1;    // S[0]用于存储主串中字符个数
		ch=getchar();
		for( i=1;i<=MAXSTRLEN&&(ch!='\n');i++)    // 录入模式串
		{
	  		T[i]=ch;
	  		ch=getchar();
		}
		T[0]=i-1;    // T[0]用于存储模式串中字符个数
		get_next(T);
		pos=Index_KMP(S,T,1);    // 请填空
		printf("%d\n",pos);
	}
	return 0;
}

OJ上的错误提示:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值