问题见代码中的注释,小问题
看了看下面的回答,都是AI,gpt能解决,我来这里干什么?给我气笑了,就没人认真回答下吗?
问题解决了,在初始化t数组是最后一位没有加上'\0',虽然不知道这样为什么会出错,但也比下面回答中一直用chatgpt的答案浪费时间强
#include <bits/stdc++.h>
#define MAXSIZE 10000
using namespace std;
int BF(const char* s, const char* t,int pos)//BF算法函数
{//匹配到返回位置,匹配不到返回-1
int i = pos;
int j = 0;
while (i < strlen(s) && j < strlen(t))/
{
if (s[i] == t[j]) { i++; j++; }
else { i = i - j + 1; j = 0; }
}
if (j >= strlen(t))
{
return i - j;
}
else return -1;
}
bool EntityRecognition(const char* S, const char* T) {
char t[6]={0};
int sum=0;
if(T[0]!='*')
{
for(int i=0;i<6;i++)
{
t[i]=T[i];
}
for(int i=0;i<strlen(T);i++)
{
if(T[i]=='*')
{
sum++;
}
}
int pos=0;
int pos1;
//cout<<BF(S,t,0);
while(BF(S,t,pos)!=-1)
{
pos1=BF(S,t,pos);
for(int i=pos1+6;i<=pos1+6+3*sum-1;i++)
{
cout<<S[i];
}
cout<<endl;
cout<<BF(S,t,0);//问题是在这里为什么输出的会是-1呢?在已经进入了这个循环的情况下
pos=pos1+6+3*sum;
cout<<BF(S,t,pos);//这里为什么输出的也是-1?
}
//cout<<BF(S,t,0);
if(BF(S,t,0)==-1)
{return false;}
}
return true;
}