Boyer Moore - 糟糕的角色规则实现子串搜索

时间:2015-12-06 06:23:15

标签: c++ multidimensional-array substring

我正在尝试用二维数组实现从拜尔摩尔实现不良字符规则进行子字符串搜索,我正在遇到这样的场景,我看到我的arr [0] [1]与arr [1] [0]重叠这引起了问题。我试图在VS中遍历代码,如果我查看内存位置,那么它实际上显示正确的值,但我不确定打印时这种重叠是如何发生的。有人能帮助我理解我做错了吗?

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

新条目是--- f 102 13

内部循环 - 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;
    }

1 个答案:

答案 0 :(得分:1)

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