13

Here is my code.

#include <iostream>
#include <vector>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<string> v;

    for(int i=0; i<n; i++)
    {
        string temp;
        cin>>temp;
        v.push_back(temp);

        //if(v.size() == 1)
        //{
        //    continue;
        //}

        //cout << i << endl;
        //cout << endl;

        //cout << v.size();

        int k = i;
        while(v[k-1].length() > v[k].length() && k>-1)
        {
            swap(v[k], v[k-1]);

            k--;
        }
        //cout << endl;
    }

    bool check = true;
    for(int i=0; i<v.size()-1; i++)
    {
        //cout << v[i] << endl;
        //cout << v[i+1] << endl;

        if (v[i+1].find(v[i]) != std::string::npos)
        {
            //std::cout << "found!" << '\n';
            continue;
        }

        //cout << "false" << endl;
        check = false;
    }

    if(check == true)
    {
        cout << "YES" << endl;
        for(int i=0; i<n; i++)
        {
            cout << v[i] << endl;
        }
    }
    else
    {
        cout << "NO" << endl;
    }
}

What’s the reason for this error?

The input was:

100
npugmvzdgfnzyxuyfwbzwktiylhvhwgeqauolidpnbemhgbunpefzsltewkxdcrzxgvmkb
bezfumiguzafxghvcfqmwpopxvazctlftelveayycypjckooxeehyk
ingenqhogs
elhnhxjwrytbmmqdwwrivvljybhnwfgwhvdgjqgqgvunuemdtrgpyvaanovheqbupamzrjxh
rpvktlmyxfshahfgunrhuqtosysymfjruqlzdooauuihtchzqgyrhcoxbtoorkxkwakvdkiakitlqfbgz
tnrnpghjmqumbzfnztiijgwkiygyfevfebuammkwnoinqvhhlsuoqtfkazqhlnuqtthudhhovjqiuykwqtck
mloehzniuwyakgwmopfgknpoiuiyewijmoefjjjsdimkisugehwqefcx
tthmaxtahimxxts
fspoetalxgcgowhjtanerjpqnen
hefsyokneekdgpbicss

It works for other cases.

Diagnostics detected issues [cpp.clang++-diagnose]: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\vector:1802:11: runtime error: addition of unsigned offset to 0x129000a0 overflowed to 0x12900088

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\vector:1802:11 in

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
eskimo ice
  • 161
  • 1
  • 1
  • 5
  • 4
    Unrelated: `#include #include #include ` suggests that you don't know what `#include ` does. This is a good thing because we can nip it in the bud. Give [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) a read. – user4581301 Jun 13 '18 at 19:49

1 Answers1

36

Whats the reason for this error:

You set i to 0

int i=0;

Then you set k to 0.

int k=i;

Then you use k-1 to index std::vector<std::string> v.

while(v[k-1].length()

k-1 is -1, but it gets worse. std::vector::operator[] casts the parameter to an unsigned type, producing an impossibly huge, not-at-all-valid index.

No bounds checking is performed with std::vector::operator[] and you end up treating memory you don't own as if it were a validly constructed std::string.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180