下一回文-PALIN SPOJ SIGABRT错误

时间:2019-06-20 05:28:16

标签: c++

我正在尝试解决SPOJ https://www.spoj.com/problems/PALIN/上的PALINDROME问题。我已经尝试过几次提交具有更改的解决方案,但仍然得到

Runtime error: SIGABRT

对于我尝试过的所有情况,输出都是正确的,但是问题仍然存在。

这是我的代码:

#include<bits/stdc++.h>

using namespace std;


int numhasall9(string n){
    int alln=0, nalln=0;
    int val = n.length();
    for(int i=0;i<n.length();i++){
        //cout << n[i] << endl;
        if(n[i] != '9'){
            nalln += 1;
        }
        else{alln += 1;}
    }
    if(alln == val)
    return 1;
        else
        return -1;
}


int main(){
    int testcases;
    scanf("%d",&testcases);
    while(testcases--){
        int l;
        string num;
        cin >> num;
        l = num.length();
        //printf("%d\n",l);
        if (l%2 == 0){
            int temp = numhasall9(num);
            if(temp == 1){
                int seclen = l -1;
                string temp11;
                string dest = string(seclen,'0').append(temp11);
                dest.insert(0,to_string(1));
                dest.insert(num.length(),to_string(1));
                cout << dest << endl;}
            else{
                int half = l/2;
                //printf("%d\n",half);
                string lst = num.substr(0,half);
                string lst_cpy = lst;
                reverse(lst.begin(),lst.end());
                //cout << lst << endl;
                string rst = num.substr(half,l-1);
                //cout << rst << endl;
                //string reverse_l = reverse(l.begin(), l.end());
                if ((lst.compare(rst)) <= 0){

                    long long int  num2  = stoi(lst_cpy) + 1;
                    string three = to_string(num2);
                    string five = three;
                    reverse(three.begin(), three.end());
                    string evenlen1 = five.append(three);
                    cout << evenlen1 << endl;
                }
                else if ((lst.compare(rst)) > 0){

                    string evenlen1 = lst_cpy.append(lst);
                    cout << evenlen1 << endl;}
            }
        }

        else if (l%2 != 0){


            if(num.length() == 1) cout << num << endl;
            else{
                int temp1 = numhasall9(num);
                if(temp1 == 1){
                    //printf("-----ENTERING");

                    int seclen1 = l -1;
                    string temp22;
                    //cout << "Entering the loop" << endl;
                    string dest1 = string(seclen1,'0').append(temp22);
                    //cout << dest1 << endl;
                    dest1.insert(0,to_string(1));

                    dest1.insert(num.length(),to_string(1));
                    cout << dest1 << endl;}



                else{
                    //printf("ENTERINZZZG");
                    //cout << temp1 << endl;
                    int mid_elem = l/2;
                    //cout << num[mid_elem] << endl;
                    string ls_cpy;
                    //cout << num[mid_elem] << endl;
                    string ls = num.substr(0,mid_elem);
                    //cout << ls << endl;
                    ls_cpy = ls;
                    //strcpy(ls,ls_cpy);
                    string rs = num.substr(mid_elem+1,l-1);
                    //cout << rs << endl;
                    reverse(ls.begin(),ls.end());
                    //cout << ls << endl;
                    string mid_elem_str = to_string(mid_elem);
                    if ((ls.compare(rs) <= 0)){

                        if (num[mid_elem] == '9'){
                                                long long int num_ls_cpy = stoi(ls_cpy) + 1;
                        string cp_ls_cpy = to_string(num_ls_cpy);
                        string copy_cp_ls_cpy = cp_ls_cpy;
                        reverse(cp_ls_cpy.begin(),cp_ls_cpy.end());
                        string tot1 = copy_cp_ls_cpy + '0' + cp_ls_cpy;
                                                cout << tot1 << endl;}

                        else{

                        //cout << "Entering this loop" << endl;
                        long long int add_mid_elem = stoi(num.substr(mid_elem,1)) + 1;

                        //cout << add_mid_elem << endl;
                        //string reverse_ls = reverse(ls.begin(),ls.end());
                        string added_mid_elem = to_string(add_mid_elem);
                        string new_str = ls_cpy + added_mid_elem + ls;
                        cout << new_str << endl;}
                    }
                    else if ((ls.compare(rs) > 0)){
                        //printf("Entering this");
                        //reverse(ls.begin(),ls.end());
                        string tot = ls_cpy + num[mid_elem]  + ls;
                        cout << tot << endl;
                    }

                }

            }

        }

    }

    return 0;
}

请建议我是否错过了一些东西!

0 个答案:

没有答案