Search code examples
c++algorithmc++11sigabrt

Why am i getting a SIGABRT error in this code


I have writen this code in CodeChef but it constantly gives me runtime error called (SIGABRT). I have been trying to resolve this issue yet there is no result. I have reduced memory to my fullest. I need help.

There are N students standing in a row and numbered 1 through N from left to right. You are given a string S with length N, where for each valid i, the i-th character of S is 'x' if the i-th student is a girl or 'y' if this student is a boy. Students standing next to each other in the row are friends.

The students are asked to form pairs for a dance competition. Each pair must consist of a boy and a girl. Two students can only form a pair if they are friends. Each student can only be part of at most one pair. What is the maximum number of pairs that can be formed?

Example Input 3 xy xyxxy yy

Example Output 1 2 0

Explanation Example case 1: There is only one possible pair: (first student, second student).

Example case 2: One of the ways to form two pairs is: (first student, second student) and (fourth student, fifth student).

Another way to form two pairs is: (second student, third student) and (fourth student, fifth student).

#include <iostream>
#include <string>
#define rp(l,p) for(int l = 0; l < p; l+=2)
#define s string 
#define ll long long
using namespace std;

int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    // your code goes here
    int t = 0;
    cin >> t;
    while(t--){
        s n = "";
        ll max = 0;
        cin >> n;
        if(n.size()%2 == 0){
            rp(l, n.size()-1){
                if(n.substr(0+l, 2) == "xy" || n.substr(0+l, 2) == "yx"){
                    max+=1;
                }
            }
        }
        else if(n.size()%2 == 1){
            s x = n.substr(n.size()-1, 1);
            n.resize(n.size()-1);
            for(int m = 0; m <= (n.size()-1); m+=2){
                if(n.substr(0+m, 2) == "xy" || n.substr(0+m, 2) == "yx"){
                    max+=1;
                }
            }
            n.append(x);
            if(n.substr(n.size()-3,2) == "xy" || n.substr(n.size()-3,2) == "yx"){

            }
            else if(n.substr(n.size()-2,2) == "xy" || n.substr(n.size()-2,2)=="yx"){
                max+=1;
            }
        }
        cout << max << endl;
    }
    return 0;
}

Solution

  • I fully aggree with @Sam Varshavchik

    However, I tried you're code with following input:

    1. try

      $ ./test

      input 2

      input ha

      output 0

      input lo

      output 0 => no crash

    2. try

      $ ./test

      input 2

      input ha

      output 0

      input lo

      output 0

    => no crash

    1. try

      $ ./test

      input 2

      input a

      terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr: __pos (which is 2) > this->size() (which is 0) Aborted (core dumped)

    So let's analyze you're code. It seems to crash when string n has size of 1.

    In you're substr calls there are lot of hardcoded integers without any validation if string has the required length (aka substr(...,2))