#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上的错误提示: