I have been solving this problem on SPOJ . One of the most frequent problems encountered while using online judges is Runtime Error. You never know which case leads to a Segmentation fault. Please help me figure out why does the code below correspond to a Runtime Error or Segmentation Fault even though I have ensured that every possible case runs properly on my linux gcc.
#include <iostream>
#include <math.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
int main ()
{
int t,i;
cin >> t;
string s;
vector<int> A;
while(t--)
{
cin >> s;
int n=s.size();
int temp;
if (n!=1)
{
for(i=0;i<s.size();i++)
{
A.push_back(s.at(i)-'0');
}
if(n%2!=0)
{ i = (n-1)/2;
while(A[i-1]==A[n-i])
i--;
i--;
}
else
{ i=n/2-1;
while(A[i]==A[n-1-i])
i-- ;
}
if (A[i]<A[n-i-1])
{ if ((n%2)!=0)
{A[n/2] = A[n/2] + 1;
A[n-i-1] = A[i];}
else
{
A[n/2-1] = A[n/2-1]+1;
A[n/2] = A[n/2-1];
A[n-i-1] = A[i];
}
}
else
A[n-i-1] = A[i];
while(i--)
A[n-i-1] = A[i];
while(!(A.empty()))
{
printf("%d",A.back());
A.pop_back();
}
}
else
cout << s;
}
}
Aucun commentaire:
Enregistrer un commentaire