I am trying to solve this problem on SPOJ and I wrote this code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef vector<int> vi;
int vi_cmp(const vi& a, const vi& b);
void palin_vi_inc(vi& a, ll& rt, ll& lt);
int main() {
int T;
cin >> T;
for (int i = 0; i < T; ++i) {
string input;
cin >> input;
if (cin) {
vi inp_dig;
for (char c : input) inp_dig.push_back(c - '0');
vi res_dig = inp_dig;
for (size_t j = 0; j < (res_dig.size()) / 2; j++) {
res_dig[res_dig.size() - j - 1] = res_dig[j];
}
ll rt = (res_dig.size() - 1) / 2;
ll lt = (res_dig.size()) / 2;
while (vi_cmp(res_dig, inp_dig) != 1) {
palin_vi_inc(res_dig, rt, lt);
}
for (vi::iterator it = res_dig.begin(); it < res_dig.end(); ++it) {
cout << *it;
}
cout << endl;
}
}
return 0;
}
int vi_cmp(const vi& a, const vi& b) {
if (a.size() > b.size()) return +1;
if (a.size() < b.size()) return -1;
for (size_t i = 0; i < a.size(); ++i) {
if (a[i] > b[i]) return +1;
if (a[i] < b[i]) return -1;
}
return 0;
}
void palin_vi_inc(vi& a, ll& rt, ll& lt) {
if (rt < 0) {
a[a.size() - 1] = 1;
a.insert(a.begin(), 1);
return;
}
else if (a[rt] < 9) {
a[rt] = a[lt] = a[rt] + 1;
return;
}
else {
a[rt] = a[lt] = 0;
rt--;
lt++;
palin_vi_inc(a, rt, lt);
return;
}
}
and it runs correctly on ideone with my test cases. But when I tried to submit it, it gave me Segmentation Fault aka SIGSEGV. What is the test case that is causing the segmentation fault ?
Here is the link to the problem http://ift.tt/1ocDMWz
Aucun commentaire:
Enregistrer un commentaire