簡體   English   中英

當我運行我的程序時,我將此作為運行時錯誤分段錯誤(SIGSEGV)

[英]When i run my program I get this as a run time error Segmentation Fault (SIGSEGV)

當我運行我的代碼時,我得到運行時錯誤。我試圖在 geeksforgeeks 上運行它,如果它與錯誤有關。 它將分段錯誤視為錯誤。

分段錯誤 (SIGSEGV)

這是我的代碼

#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;
}

您正在嘗試將一個string push_back()放入vector<int>中。 您必須將字符串轉換為int - 這可以通過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);

您正在將vector<string>傳遞給接受vector<int>的 function 。 v聲明為vector<int>


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();
    }
}
}

Bro 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 ,Thankyou 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM