gpt4 book ai didi

c++ - Boyer Moore - 坏字符规则实现子串搜索

转载 作者:行者123 更新时间:2023-11-30 05:34:44 27 4
gpt4 key购买 nike

我正在尝试使用 2D 数组从 boyer moore 实现错误字符规则以进行子字符串搜索,我遇到了我看到我的 arr[0][1] 与 arr[1][0] 重叠的情况这引起了问题。我试图遍历 VS 中的代码,如果我查看内存位置,它实际上会显示正确的值,但我不确定在打印重叠是如何发生的。有人可以帮助我了解我做错了什么吗?

错误输出j 的值为 --- 0

新条目是 --- f 102 13

while 循环内部 -- 102 13

while 循环内部 -- 13 -1 //13 的重叠发生 y ?

 # define ROWS 20
# define COLUMNS 1

int main(void)
{
string str = "firstcharacter";
int strlength = str.length();
int arr[ROWS][COLUMNS];
memset(arr, -1, sizeof(arr[0][0]) * ROWS * COLUMNS);

for (int i = 0; i < strlength; i++)
{
int j = 0;
bool flag = false;
// check till point previous character was inserted
while (arr[j][1] != -1)
{
// if previous character is already present
// just update value dont insert character
if (arr[j][0] == str[i])
{
flag = true;
break;
}
j++;
}

if (flag == true)
{
int value = strlength - i - 1;
arr[j][1] = value;
cout << " Old entry is --- " << str[i] << " " << arr[j][0] << " " << arr[j][1] << endl << endl;
}
else
{
int value = strlength - i - 1;
arr[j][0] = str[i];
arr[j][1] = value;
cout << " New entry is --- " << str[i] << " "<< arr[j][0] << " " << arr[j][1] << endl << endl;
}

for (int x = 0; x < strlength; x++)
{
cout << " Inside while loop -- " << arr[x][0] << " " << arr[x][1] << endl; // --- error case after first iteration only I see value overlapping
}
}

cout << endl << endl << endl << endl;

for (int i = 0; i < strlength; i++)
{
cout << " final table is " << arr[i][0] << " " << arr[i][1] << endl;
}

return 0;
}

最佳答案

因为 COLUMNS1ary[j][1]ary[j+1][0] 将指向相同的内存地址,尽管第一个在技术上是未定义的行为,因为 [1] 下标超出范围,

关于c++ - Boyer Moore - 坏字符规则实现子串搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34114702/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com