How to remove SIGSEGV runtime error . Please help

Problem code- ITGUY15
This code is working everywhere but codechef is showing runtime error and I am not able to find the reason for this so please help.

#include<bits/stdc++.h>
#include
using namespace std;

void recur(string s)

{

vector <char> v;

int flag=0;

for(int i=0;i<s.length();i++)

{
        v.push_back(s[i]);
 }

vector<char>::iterator it1,it2;

it1=v.begin();

for(int i=0;i<v.size();i++)

{

    if((v[i]=='a')&&(v[i+1]=='b')&&(v[i+2]=='c'))

    {

        it1+=i;

        it2=it1+3;

        v.erase(it1,it2);

        flag=1;

    }

}

if(flag==0)

{
    cout<<s<<endl;
    return;
 }

string str="";

 for(int i=0;i<v.size();i++)
        str+=v[i];

 v.clear();

 recur(str);

}

int main()

{

int t;

cin>>t;

while(t--)

{

   string s;

   cin>>s;

   recur(s);

}

return 0;        

}

In the above line of code , you are accessing out of bound memory address for vector when i=v.size()-1 and v.size()-2 as v.size()-1+1=v.size(), which is out if bound .
To remove SIGSEGV you can push back any two random letter in the vector other than a,b,c

1 Like

@dhruv788
Can you tell me something regarding
What steps did you follow from starting competitive coding to till now to get such higher rating and maintaining it so well
You are genius
:pray: Plz reply that help me to get some idea what path should I follow to become such a good like you or even some close to you

1 Like

I am not . I think I reached 5 star by luck .
I refered to CP algorithms, and youtube channel named Code NCode for learning Algos . Solved problems on these Algos on some sites.Though Now a days , Questions related to Algos have decreased in CP , but still I think you will have to learn some important CP Algos . But do give contest regularly atleast on one platform, Because now a days most of the CP contests question are logic based

1 Like

@dhruv788 I did the same what you told and also another approach was that I ran the loop of i till v.size()-3 i.e i+2<v.size() so that it does not go out of bound. But it is still showing runtime error. Is there any other way or can you once try it yourself and tell.

Can you link the submission

i<v.size()-2
then check

@dhruv788
https://www.codechef.com/viewsolution/40530572
https://www.codechef.com/viewsolution/40530419
these are the two approaches.

The runtime error(SIGSEGV) is caused due to the line 20 . There you are continuously doing it1+=i
Whereas I think either you should it1++ before the if statement or inside if statement make it1=v.begin() and then do it1+=i
Refer the correction I did below
https://www.codechef.com/viewsolution/40548079

Yes it worked. Thank you so much!!