Finding next palindrome of a number, getting SIGABRT error - c++

Hi ,I am trying to solve the question PALIN on SPOJ where the idea is to find the next immediate palindrome of a number. I tested my code on the IDE with all possible test cases I found and the program is working fine but I can't figure out why I am getting SIGABRT error when I submit the solution.Please I request someone help me figure out the problem in the code For yor reference I am providing the link of the question:- https://www.spoj.com/problems/PALIN/
#include<iostream>
#include<stdio.h>
#include<vector>
#include<string>
using namespace std;
bool all_nines(string s)
{
for(int i=0;i<s.length();i++)
{
if(s[i]!='9')
return false;
}
return true;
}
int reverse(int a)
{
int rev=0;
while(a)
{
rev=(rev*10)+(a%10);
a/=10;
}
return rev;
}
void convert_and_compare(string &s,int low,int high)
{
int low_num,high_num;
string low_str="",high_str="";
for(int i=0,j=high;i<=low,j<s.size();i++,j++)
{
low_str+=s[i];
high_str+=s[j];
}
low_num=stoi(low_str);
high_num=stoi(high_str);
if(reverse(low_num)<=high_num)
{
low_num++;
low_str=to_string(low_num);
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
}
else
{
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
}
s="";
s+=low_str+high_str;
}
void convert_and_compare(string &s,int low,int high,int mid)
{
int low_num,high_num,mid_num;
string low_str="",high_str="";
char mid_str;
for(int i=0,j=high;i<=low,j<s.size();i++,j++)
{
low_str+=s[i];
high_str+=s[j];
}
low_num=stoi(low_str);
high_num=stoi(high_str);
mid_num=(s[mid]-'0');
if(reverse(low_num)>high_num)
{
low_str=to_string(low_num);
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
mid_str=mid_num+'0';
}
else if(reverse(low_num)<=high_num && mid_num!=9)
{
mid_num++;
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
mid_str=mid_num+'0';
}
else if(reverse(low_num)<=high_num && mid_num==9)
{
mid_num=0;
low_num++;
low_str=to_string(low_num);
high_str="";
for(int i=low_str.length()-1;i>=0;i--)
high_str+=low_str[i];
mid_str=mid_num+'0';
}
s="";
s=low_str+mid_str+high_str;
}
string find_next_palin(string s)
{
if(all_nines(s))
{
s[0]='1';
for(int i=1;i<s.length();i++)
s[i]='0';
s+='1';
return s;
}
else
{
if(s.length()%2==0)
{
int low,high;
low=s.length()/2-1;
high=s.length()/2;
convert_and_compare(s,low,high);
return s;
}
else
{
int low,high,mid;
mid=s.length()/2;
low=mid-1;
high=mid+1;
convert_and_compare(s,low,high,mid);
return s;
}
}
}
int main()
{
int t;
scanf("%d",&t);
vector <string> v(t);
for(int i=0;i<t;i++)
{
cin>>v[i];
}
for(int i=0;i<t;i++)
{
if(v[i].length()==1)
{
if(v[i]=="9")
{ printf("11");
printf("\n");
}
else
{
printf("%d",(v[i][0]-'0')+1);
printf("\n");
}
}
else
{
string temp;
temp=find_next_palin(v[i]);
for(int j=0;j<temp.length();j++)
cout<<temp[j];
printf("\n");
}
}
return 0;
}

Related

codechef isrec wrong answer

I am facing a wrong answer on my submission. I have run all the test cases that came to my mind but unfortunately all seem to give the correct output.
I even checked out others' submissions but didn't get very far. Apparently idea seems to be fine but maybe I am missing some serious detail. I simply have no idea.
The link to the question is:
https://www.codechef.com/CCRC21C/problems/ISREC
My code is:
#include<bits/stdc++.h>
using namespace std;
int num_consec_ones(string row,int m)
{
int c=0;
for(int i=0;i<m;i++)
{
if(row[i]==1) c++;
if(c>1 && row[i-1]==0 && row[i]==1)
{
c=-1;
break;
}
}
return c;
}
int start_index(string row,int m)
{
for (int i=0;i<m;i++)
{
if(row[i]==1)
return i;
}
}
void solve()
{
int n,m,flag=0;;
cin>>n>>m;
vector<string>row;
// row.reserve(n);
for(int i=0;i<n;i++)
{
string str;
cin>>str;
row.push_back(str);
for(int j=0;j<m;j++)
row[i][j]=row[i][j]-'0';
}
set<int>s;
for(int j=0;j<n;j++)
{
if(num_consec_ones(row[j],m)==-1)
{
cout<<"No"<<endl;
return;
}
if(num_consec_ones(row[j],m) && flag==1 && num_consec_ones(row[j-1],m)==0)
{
cout<<"No"<<endl;
return;
}
if(num_consec_ones(row[j],m) && flag==0)
{
s.insert(num_consec_ones(row[j],m));
flag=1;
}
}
if(s.size()==1)
{
set<int>start;
for(int j=0;j<n;j++)
{
if(num_consec_ones(row[j],m))
{
start.insert(start_index(row[j],m));
}
}
if(start.size()==1)
{
cout<<"Yes"<<endl;
return;
}
else{
cout<<"No"<<endl;
return;
}
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
solve();
}
}
Please help me out.
Thank you!

Codechef problem ISHVALA(the promised land)

In My Program what is logical error,
I don't understand Please Help me,,,I tried different Test case,,in which I found all Correct....
but in Codechef it's Wrong answer.
#include<iostream>
using namespace std;
int main()
{
int T,N,M;
cin>>T;
while(T)
{
cin>>N>>M;
int X,Y,S,c=0,d=0,i1=0,j1=0;
cin>>X>>Y>>S;
int x[X],y[Y];
if(X)
{
for(int i=0;i<X;i++)
cin>>x[i];
}
if(Y)
{
for(int i=0;i<Y;i++)
cin>>y[i];
}
for(int i=1;i<=N;i=i+S)
{
for(int j=1;j<=M;j=j+S)
{
for(int k=0;k<X;k++)
{
if(i<=x[k] && i+S-1>=x[k])
{
c=1;
i1=i-S+1;
}
}
for(int k=0;k<Y;k++)
{
if(j<=y[k] && j+S-1>=y[k] )
{
c=1;
j1=j-S+1;
}
}
if(c==0 && i+S-1<=N && j+S-1<=M)
{
d++;
}
c=0;
if(j1)
j=j1;
j1=0;
}
if(i1)
i=i1;
i1=0;
}
cout<<d<<endl;
T--;
}
}
The program(Question) in Codechef in beginners.
The link is here-enter link description here
Try this:
#include<iostream>
#define gc getchar_unlocked
using namespace std;
void fs(int &x)
{
register int c=gc();
x=0;
for(;c<48||c>57;c=gc());
for(;c>47&&c<58;c=gc())
x=(x<<1)+(x<<3)+c-48;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t,n,m,s,x,y,sum1,sum2,a,b;
fs(t);
while(t--)
{
fs(n);
fs(m);
fs(x);
fs(y);
fs(s);
sum1=0;
sum2=0;
a=0;
while(x--)
{
fs(b);
sum1=sum1+(b-a-1)/s;
a=b;
}
sum1=sum1+(n-a)/s;

Change Karnaugh Map simplifier from SOP to POS result

It's my program to simplify Karnough map. I need to change it from SOP to POS for university project. I changed already signs from + to * and from * to +. All i have to do now is to make it read zero-s from text file instead of one's. i tried everything to solve that problem, but i have no idea. I have got two txt files, one with input and one with output. At this moment i need to put in input 0's instead of 1's and in the other side to, to make it work. Please help me, my life depends of that
#include <iostream>
#include <fstream>
#include <cmath>
#include <windows.h>
using namespace std;
//?????
int pow2(int n) // ??2?????,2^n
{
int result=1
;
while(n>0)
result*=2,n--;
return result;
}
int combination(int n,int r) // ?????C_n^r,? n! / ((n-r)!*(r)!)
{
int fm=1,fz=1;
for(int i=1;i<=r;i++,n--)
{
fm*=i;
fz*=n;
}
return fz/fm;
}
void d2b(int d,char* b,int n) // ???????,??????d???????????b?
{
for(n--;n>=0;n--)
{
b[n]='0'+d%2;
d/=2;
}
}
int ispair(char* a1,char* a2,int n) // ????????????????
{
int x,y=0;
for(int i=0;i<n;i++)
if(a1[i]!=a2[i])
x=i,y++;
if(y==1) return x;
else return-1;
}
bool issame(char* a1,char* a2,int n) // ????
{
for(int i=0;i<n;i++)
if(a1[i]!=a2[i])
return 0;
return 1;
}
int left1(char* a,int N) // ???????????,??????1,??????
{
for(int i=0;i<N;i++)
if(a[i]=='1')
return i;
return -1;
}
void copy(char* a1,char* a2,int n) // ??????
{
for(int i=0;i<n;i++)
a2[i]=a1[i];
}
bool isinside(int x,char* a,int n) // ???x????????a?
{
for(n--;n>=0;n--)
{
if(a[n]!='x' && a[n]!=(char)('0'+x%2))
return 0;
x/=2;
}
return 1;
}
void output(fstream& file,char* a,int n)
{
for(int i=0;i<n;i++)
{
if(i==0)file<<'(';
if(a[i]=='0'||a[i]=='1')
{
if(a[i]=='0') file<<'¬'<<(char)('a'+i);
if(a[i]=='1') file<<(char)('a'+i);
if(a[i+1]=='0'||a[i+1]=='1'||a[i+2]=='0'||a[i+2]=='1'||a[i+3]=='0'||a[i+3]=='1')
{
file<<'+';
}
else{
file<<')';
}
}
else{}
;
}
file<<'*';
}
int count(char* table,char* a,int n,int N) // ??,????????1???????????a?
{
int counter=0;
for(int i=0;i<N;i++)
{
if(table[i]!='1') continue;
if(isinside(i,a,n)) counter++;
}
return counter;
}
void clean(char* table,char* a,int n,int N) // ??????????????????1,???x
{
for(int j=0;j<N;j++)
if(isinside(j,a,n))
table[j]='x';
}
int main()
{
SetConsoleTitle("???-???????????");
system("color 06");
//????
fstream inputFile("input.txt",ios::in); //??????????input.txt
int valNum; //???
inputFile >> valNum; // ???????????
cout<<"\n ????????: "<<valNum;
cout<<"\n ?????????:\n";
int minTermLength=pow2(valNum); // ??????????2^valNum?
char* minTermExpression=new char[minTermLength]; // ???????????????
int lineOff = pow2(ceil(double(valNum)/2));
// ???????
if (inputFile.is_open())
{
for(int i=0;i<minTermLength;i++) // ?????
{
inputFile>>minTermExpression[i];
if(i%lineOff == 0&&(i!=0))
cout<<"\n";
cout<<"\t"<<minTermExpression[i];
}
inputFile.close();
}
// ??????
else{
cout<<"\n ??input.txt??,???????";
return 0;
}
// ?implication????????
char*** implication=new char**[valNum]; // ????
int nonZeroNum=1;
for(int i=0;i<minTermLength;i++)
if(minTermExpression[i]!='0') // ?????ON???DC?????????
nonZeroNum++;
for(int i=0;i<valNum;i++) // i-??,???????3?,i=0???????,i=1???,i=2???
{
if(pow2(i)>nonZeroNum)break;
int x=pow2(i-1)*combination(nonZeroNum,pow2(i));
implication[i]=new char*[x];
for(int j=0;j<x;j++)
implication[i][j]=new char[valNum];
}
//?????????
int* countNum=new int[valNum+1];
countNum[0]=0;
for(int i=0;i<minTermLength;i++)
if(minTermExpression[i]!='0') // ???0?
{
d2b(i,implication[0][countNum[0]],valNum); // ??????????implication[0]???
countNum[0]++;
}
int isOptimal=0;
while(countNum[isOptimal]>0) // ??????????
{
countNum[isOptimal+1]=0;
for(int i=0;i<countNum[isOptimal]-1;i++)
for(int j=i+1;j<countNum[isOptimal];j++)
{
int x=ispair(implication[isOptimal][i],implication[isOptimal][j],valNum); // ??????????
if(x==-1) continue;
copy(implication[isOptimal][i],implication[isOptimal+1][countNum[isOptimal+1]],valNum);// ???implication??
implication[isOptimal+1][countNum[isOptimal+1]][x]='x'; // ??????????,?????????x,???????????
countNum[isOptimal+1]++;
}
for(int i=0;i<countNum[isOptimal+1]-1;i++)
for(int j=i+1;j<countNum[isOptimal+1];j++)
if(issame(implication[isOptimal+1][i],implication[isOptimal+1][j],valNum)) // ??????
{
for(int k=j;k<countNum[isOptimal+1]-1;k++)
copy(implication[isOptimal+1][k+1],implication[isOptimal+1][k],valNum);
countNum[isOptimal+1]--;
}
isOptimal++;
}
isOptimal--;
//???????
fstream outputFile("output.txt",ios::out);
outputFile<<"F=";
while(left1(minTermExpression,minTermLength)>=0) //?minTermExpression???1
{
bool flag=0; // ???,????
for(int i=0;i<minTermLength&&flag==0;i++)
{
if(minTermExpression[i]!='1') continue;
int counter=0,recorder;
for(int j=0;j<countNum[isOptimal];j++)
if(isinside(i,implication[isOptimal][j],valNum))
counter++,recorder=j;
if(counter!=1) continue;
output(outputFile,implication[isOptimal][recorder],valNum);
clean(minTermExpression,implication[isOptimal][recorder],valNum,minTermLength);
flag=1;
}
if(flag==1) continue;
int termMaxInclude=0;
int recorder=0;
for(int i=0;i<countNum[isOptimal];i++) // ??????????,???????????1???,??????
if(count(minTermExpression,implication[isOptimal][i],valNum,minTermLength)>termMaxInclude)
termMaxInclude=count(minTermExpression,implication[isOptimal][i],valNum,minTermLength),recorder=i;
if(termMaxInclude==0) {isOptimal--; continue;}
output(outputFile,implication[isOptimal][recorder],valNum);
clean(minTermExpression,implication[isOptimal][recorder],valNum,minTermLength);
}
outputFile.close();
// ?????
outputFile.open("output.txt",ios::in);
char finalExpression[201];
outputFile.getline(finalExpression,200);
outputFile.close();
int termMaxInclude=0;
for(;finalExpression[termMaxInclude]!='\0';termMaxInclude++);
finalExpression[termMaxInclude-1]='\0';
outputFile.open("output.txt",ios::out);
outputFile<<finalExpression;
outputFile.close();
cout<<"\n??????????????";
cout<<"\n ??????,??????!";
cin.get();
return 0;
}
The zeros are simply those not in the ones list.

pop() is not working properly

Here is a c++ code on stack.Ignore the extra code here
#include<iostream>
using namespace std;
class mystack
{
private:
int top;
int size;
int * s;
public:
void initialize()
{
top=-1;
cin>>size;
s=new int[size];
}
~mystack(){delete [] s;}
void push()
{
int x;
if(top==size-1)
cout<<"stack overflow!"<<endl;
else
{
cout<<"Enter element to be pushed:";
cin>>x;
top++;
s[top]=x;
cout<<s[top]<<endl;
}
}
int pop()
{
int p=s[top];
if(top==-1)
return 0;
else
{
top--;
return p;
}
}
int maxsize()
{
return size;
}
int isempty()
{
if(top==-1)
return 0;
else
return 1;
}
void display()
{
int i,p=top;
cout<<s[0]<<endl;
for(i=0;i<=p;i++)
cout<<s[i]<<endl;
}
};
int main()
{
int n,i;
cout<<"Enter no. of stacks:";
cin>>n;
mystack * st=new mystack[n];
for(i=0;i<n;i++)
{
cout<<"Enter size of stack "<<i+1<<":";
st[i].initialize();
}
int c,s;
while(1)
{
cout<<"*****Operations*****"<<endl;
cout<<"1.Push 2.Pop 3.Maxsize 4.isempty 5.Display 6.Quit"<<endl;
cout<<"Enter your choice:";
cin>>c;
if(n>1)
{
cout<<"Operation on which stack:";
cin>>s;
}
else
s=1;
if(c==1)
st[s-1].push();
else if(c==2)
{
if(st[s-1].pop()==0)
cout<<"stack underflow!"<<endl;
else
cout<<st[s-1].pop()<<endl;
}
else if(c==3)
cout<<st[s-1].maxsize()<<endl;
else if(c==4)
{
if(st[s-1].isempty()==0)
cout<<"True"<<endl;
else
cout<<"False"<<endl;
}
else if(c==5)
st[s-1].display();
else if(c==6)
break;
else
{
cout<<"Wrong input!"<<endl;
continue;
}
}
return 0;
}
Here accessing pop operation gives the element of top-1.I can't understand why.What should I do?When I do return s[top--] same thing is happening.
Since you haven't gotten back to this, I am going to presume you've already found your logic error.
So here is the one error I found. There may be more, I quit looking ...
In the following code, how many times is pop() being called?
else if(c==2)
{
if(st[s-1].pop()==0)
cout<<"stack underflow!"<<endl;
else
cout<<st[s-1].pop()<<endl;
}

SIGABRT Error during runtime (c++)

This is the skyscraper floors problem from spoj. I m getting SIGABRT when I run the code.Link for the problem is http://www.spoj.com/problems/SCRAPER/. When I run this code on my PC, it runs without any error but spoj's online judge shows SIGABRT error.
#include <iostream>
#include <vector>
using namespace std;
struct floor
{
unsigned int floornum;
bool calldone;
vector <floor*> connection;
floor()
{
floornum=0;
calldone=0;
}
};
struct elevator
{
unsigned int x,y;
elevator()
{
x=y=0;
}
};
struct skyscrapper
{
unsigned int f,e,a,b;
vector <elevator> ele;
vector <floor*> flooraddress; //this is a spoj problem
void read()
{
cin>>f>>e>>a>>b;
elevator *temp;
for(unsigned int i=0;i<e;i++)
{
temp=new elevator;
cin>>(*temp).x>>(*temp).y;
ele.push_back(*temp);
}
for (unsigned int i=0;i<f;i++)
{
floor* tempp=new floor;
(*tempp).floornum=i;
flooraddress.push_back(tempp);
}
}
void allotaddress()
{
unsigned int j,k;
for(unsigned int i=0;i<ele.size();i++)
{
j=ele[i].y;
k=ele[i].x;
while(j<f)
{
if(j!=ele[i].y)
{
(*(flooraddress[j])).connection.push_back(flooraddress[j-k]);
(*(flooraddress[j-k])).connection.push_back(flooraddress[j]);
}
j=j+k;
}
}
}
};
bool flag;
bool traverse(floor* m,int destination)
{
if((*m).calldone==1)
{
return 0;
}
(*m).calldone=1;
if((*m).floornum==destination)
return 1;
if((*m).connection.empty())
return 0;
for(int i=0;i<(*m).connection.size();i++)
{
flag=traverse(((*m).connection[i]),destination);
if(flag==1)
return flag;
}
return 0;
}
int main()
{
int n;
cin>>n;
skyscrapper iit[n];
bool ans[n];
for(int i=0;i<n;i++)
{
iit[i].read();
iit[i].allotaddress();
ans[i]=traverse(iit[i].flooraddress[iit[i].a],iit[i].b);
}
for(int i=0;i<n;i++)
{
if(ans[i]==1)
cout<<"It is possible to move the furniture."<<endl;
else
cout<<"The furniture cannot be moved."<<endl;
}
return 0;
}