繁体   English   中英

不知道为什么会出现细分错误

[英]Don't know why getting segmentation fault

我正在尝试在Xcode中使用C ++解决Grid Search ,并产生一些奇怪的输出。

我的代码:

#include <iostream>
#include <vector>

using namespace std;

int main(){
    int tCases;
    cin >> tCases;
    for(int i = 0; i < tCases; i++){
        bool isFound = false;
        int row = 0, column = 0, tRow = 0, tColumn = 0, p = 0;
        cin >> row >> column;
        vector<string> matrix(row);
        for (int i = 0; i < row; i++) {
            cin >> matrix[i];
        }
        cin >> tRow >> tColumn;
        vector<string> tMatrix(tRow);
        for (int i = 0; i < tRow; i++) {
            cin >> tMatrix[i];
        }
        vector<long> position(tRow);
        for (int i = 0; i <= row - tRow; i++) {
            position[p] = matrix[i].find(tMatrix[p]) ;
            if (position[p] == string::npos) {
                isFound = false;
                p = 0;
            }
            else if (p != tRow && (p == 0 || position[p] == position[p-1])) {
                isFound = true;
                p++;
            }
            else if (p == tRow)
                break;
        }
        cout << (isFound ? "YES" : "NO") << endl;
    }
    return 0;
}

测试用例1:

1
20 20
34889246430321978567
58957542800420926643
35502505614464308821
14858224623252492823
72509980920257761017
22842014894387119401
01112950562348692493
16417403478999610594
79426411112116726706
65175742483779283052
89078730337964397201
13765228547239925167
26113704444636815161
25993216162800952044
88796416233981756034
14416627212117283516
15248825304941012863
88460496662793369385
59727291023618867708
19755940017808628326
7 4
1641
7942
6517
8907
1376
2691
2599

我的输出和预期输出:

没有

测试案例2:

1
25 25
7652157548860692421022503
9283597467877865303553675
4160389485250089289309493
2583470721457150497569300
3220130778636571709490905
3588873017660047694725749
9288991387848870159567061
4840101673383478700737237
8430916536880190158229898
8986106490042260460547150
2591460395957631878779378
1816190871689680423501920
0704047294563387014281341
8544774664056811258209321
9609294756392563447060526
0170173859593369054590795
6088985673796975810221577
7738800757919472437622349
5474120045253009653348388
3930491401877849249410013
1486477041403746396925337
2955579022827592919878713
2625547961868100985291514
3673299809851325174555652
4533398973801647859680907
5 4
5250
1457
8636
7660
7848

我的输出和预期输出:

有趣的是,当我尝试同时检查两个测试用例时,它只是为我提供了第一个测试用例的输出,然后继续运行而不显示任何内容。 HackerRank表示存在细分错误。 但是,我在CLion中运行了组合测试用例,并且运行良好。 您能帮我找到代码中的错误吗?

for (int i = 0; i <= row - tRow; i++)
{
    position[p] = matrix[i].find(tMatrix[p]) ;
    if (position[p] == string::npos)
    {
        isFound = false;
        p = 0;
    }
    else if (p != tRow && (p == 0 || position[p] == position[p-1])) 
    {
        isFound = true;
        p++;
    }
    else if (p == tRow)
        break;
    }
}

这可能会导致段错误:

  • 如果p达到tRow-1 (为什么不!),然后如果position[p] == string::npos (为什么不!),则输入第一个else if语句并执行: isFound = true; p++; isFound = true; p++;

  • 然后,在下一次迭代中, ptRow (随着它的增加),然后将进行测试(position[p] == string::npos) tRow索引处的访问position ,该position已超出范围。

循环条件

for (int i = 0; i <= row - tRow; i++)

应该替换为

for (int i = 0; (i <= row - tRow) && (p < tRow); i++)

防止这种情况。 然后可以简单地删除else语句( if (p == tRow) break; )。

你一个人走。

更换

    else if (p == tRow)
        break;

通过

if (p == tRow)
    break;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM