-2

When I run my code I get runtime error .I am trying to run this on geeksforgeeks, if it has something to do with the error. It gives segmentation fault as an error.

Segmentation Fault (SIGSEGV)

Here is my code

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

class Solution{

public:
    bool isPalindrome(string str,int start,int end){
        while(start<end){
            if(str[start]!=str[end]) return false;
            start++;
            end--;
        }
        return true;
    }
void solve(vector<string>& v,int &ans,string &str, int i){
    if(i==str.length()){
        if(v.size()<ans) ans=v.size();
        return;
    }
    
    for(int j=i;j<str.length();j++){
        if(isPalindrome(str,i,j)) {
            v.push_back(str.substr(i,j-i+1));
            solve(v,ans,str,i);
            v.pop_back();
        }
    }
}
int palindromicPartition(string str)
{
    vector<string> v;
    int ans = INT_MAX;
    solve(v,ans,str,0);
    return ans-1;
}
};

int main(){
int t;
cin>>t;
while(t--){
    string str;
    cin>>str;
    solution ob;
    cout<<ob.palindromicPartition(str)<<"\n";
   }
return 0;
}
  • 1
    `std::vector::push_back(std::__cxx11::basic_string)` mean you trying to push a string into a int vector – Martin Morterol Sep 15 '22 at 08:06
  • 3
    My guess is that you're using a so-called "competition" or "judge" sites to learn C++. Please note that such sites are ***not*** any kind of learning or teaching resource, no matter what you might have heard. All that seems to be taught by such sites are bad habits, bad code, and often also plain invalid code. If possible please invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to learn properly. And take computer-science classes to learn programming in general. – Some programmer dude Sep 15 '22 at 08:14
  • 2
    *Here is my code I am trying to run this on geeksforgeeks* -- That's your first mistake, and that is using that site to learn C++ from. Most of the example code you see on that site is either 1) not valid C++, or 2) Just plain wrong. There is little to no peer-review done on that site. Instead, learn C++ from reputable books, as already indicated by @Someprogrammerdude. – PaulMcKenzie Sep 15 '22 at 08:15
  • 1
    If you have a new problem, post a new question. Your edit makes the existing answers pointless and confusing. – molbdnilo Sep 15 '22 at 09:31
  • 1
    [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Jesper Juhl Sep 15 '22 at 09:32
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Sep 15 '22 at 12:49

2 Answers2

0

You are trying to push_back() a string into a vector<int>. You have to cast the string to an int - this can be achieved with std::stoi.

v.push_back(std::stoi(str.substr(i, j - i + 1)));

vector<string> v;
int ans = INT_MAX;
solve(v, ans, str, 0);

You are passing a vector<string> to a function that accepts a vector<int>. Declare v to be a vector<int>.


Stack Danny
  • 7,754
  • 2
  • 26
  • 55
0
void solve(vector<string>& v,int &ans,string &str, int i){
    if(i==str.length()){
        if(v.size()<ans) ans=v.size();
        return;
    }
    for(int j=i;j<str.length();j++){
        if(isPalindrome(str,i,j)) {
            v.push_back(str.substr(i,j-i+1));
            solve(v,ans,str,i);
            v.pop_back();
        }
    }
}

I have corrected your code issue was this you were making vector of string data type but in solve function you change the type to int of vector, since the vector passed to function was of string type so your vector argument of function should be of string type.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92