I'm getting a SIGABRT error when I compile the following code(PALIN problem on SPOJ).The objective of the code is to find smallest palindromic number which should be greater than the given number, where given number can have upto 1000000 digits.
Link to the problem is: http://www.spoj.com/problems/PALIN/ It runs well on codeblocks but SPOJ returns SIGABRT error. Can someone explain the reason?
#include<iostream>
#include<string>
using namespace std;
string palinodd(string num)//to find next nearest palindrome for an odd digit number
{
string palin=num;
int flag=0;
for(int i=num.size()/2-1;i>-1;i--)
{
if(flag==0)//checks if the middle most digit should be incremented
{
if(num[i]<palin[num.size()-i-1])
flag=1;
else if(num[i]>palin[num.size()-i-1])
flag=-1;
}
palin[num.size()-1-i]=num[i];
}
if(flag!=-1)
palin[num.size()/2]++;
if(palin[num.size()/2]==':')//if the middle digit goes greater than 9
{
palin[num.size()/2]='0';
palin[num.size()/2-1]++;
palin[num.size()/2+1]++;
}
return palin;
}
string palineve(string num)//to find next nearest palindrome for an even digit number
{
string palin=num;
int flag=0;
for(int i=num.size()/2-1;i>-1;i--)
{
if(flag==0)//checks if middle digit should be incremented
{
if(num[i]<palin[num.size()-i-1])
flag=1;
else if(num[i]>palin[num.size()-i-1])
flag=-1;
}
palin[num.size()-1-i]=num[i];
}
if(flag!=-1)
palin[num.size()/2-1]++;
if(palin[num.size()/2-1]==':')//if the middle digit goes greater than 9
{
palin[num.size()/2-2]++;
palin[num.size()/2+1]++;
palin[num.size()/2-1]='0';
}
palin[num.size()/2]=palin[num.size()/2-1];//updates the middle number
return palin;
}
int main()
{
int t;
cin>>t;
while(t--)
{
string num="912496394";
string ans;
if(num.size()%2!=0)
ans=palinodd(num);
else
ans=palineve(num);
cout<<ans<<endl;
}
return 0;
}
Appreciate your effort, try to match c++ compiler, version in SPOJ and codeblocks. I tried your code with clang++-5.0 after changing num (912496394) to string but i didn't get expected results. I think you might have posted older version of your code, if possible you can post the updated code.
The process will be aborted with SIGABRT if there are any overflows in your code, as you are using more string operations you can refer below link which has some info on SIGABRT,
SIGABRT called when calling find() on a string element in an array
One suggestion on your approach to the problem, you can try to increment the input by one in a while loop until you find a palindrome or you reach largest number and print the results
The solution I found out for above problem is:
#include<iostream>
#include<string>
using namespace std;
string palinodd(string num)
{
string palin=num;
int flag=0;
for(int i=num.size()/2-1;i>-1;i--)
{
if(flag==0)
{
if(num[i]<palin[num.size()-i-1])
flag=1;
else if(num[i]>palin[num.size()-i-1])
flag=-1;
}
palin[num.size()-1-i]=num[i];
}
if(flag!=-1)
palin[num.size()/2]++;
if(palin[num.size()/2]==':')
{
palin[num.size()/2]='0';
palin[num.size()/2-1]++;
palin[num.size()/2+1]++;
}
for(int i=num.size()/2-1;i>0;i--)
{
if(palin[i]==':')
{
palin[i]='0';
palin[i-1]++;
}
}
for(int i=num.size()/2+1;i<num.size()-1;i++)
{
if(palin[i]==':')
{
palin[i]='0';
palin[i+1]++;
}
}
return palin;
}
string palineve(string num)
{
string palin=num;
int flag=0;
for(int i=num.size()/2-1;i>-1;i--)
{
if(flag==0)
{
if(num[i]<palin[num.size()-i-1])
flag=1;
else if(num[i]>palin[num.size()-i-1])
flag=-1;
}
palin[num.size()-1-i]=num[i];
}
if(flag!=-1)
palin[num.size()/2-1]++;
if(palin[num.size()/2-1]==':')
{
palin[num.size()/2-2]++;
palin[num.size()/2+1]++;
palin[num.size()/2-1]='0';
}
palin[num.size()/2]=palin[num.size()/2-1];
/*if any incremented digit becomes greater than 9,it shows a ':' so we make it 0 and increase previous(if ':' comes in the first half of the string)/next(if ':' comes in the last half of the sting) digit by 1.*/
for(int i=num.size()/2-2;i>0;i--)
{
if(palin[i]==':')
{
palin[i]='0';
palin[i-1]++;
}
}
for(int i=num.size()/2+1;i<num.size()-1;i++)
{
if(palin[i]==':')
{
palin[i]='0';
palin[i+1]++;
}
}
return palin;
}
int main()
{
int t;
cin>>t;
while(t--)
{
string num,ans;
cin>>num;
int cnt=0;
/*if all the digits in the string are 9, then the next palindromic number will have one more digit than the original number of digits. Thus, increasing the string length*/
for(int i=0;i<num.size();i++)
{
if(num[i]=='9')
cnt++;
}
if(cnt==num.size())
{
num[0]='1';
for(int i=1;i<num.size();i++)
num[i]='0';
num+='0';
}
if(num.size()%2!=0)
ans=palinodd(num);
else
ans=palineve(num);
cout<<ans<<endl;
}
return 0;
}
Related
Given a frame and generator, no. of bits in generator is n
you have to put n-1 number of zeroes behind frame
then the first n bits of frame are XORed with the generator
this is continued until the remainder has n-1 digits
then last n-1 digits of frame(which were the zeroes we added in step 1) are replaced by n-1 digits of
remainder
this is to be displayed
It executes, takes input but the cursor just blinks instead of giving output
#include<iostream>
#include<string>
using namespace std;
class CRC
{
int frame[32], generator[10],f,g;
public:
void input();
void check();
}obj;
void CRC::input()
{
int i;
cout<<"Enter length of frame:";
cin>>obj.f;
cout<<"Enter frame:";
for(i=0;i<obj.f;i++)
{
cin>>obj.frame[i];
}
cout<<"Enter length of generator:";
cin>>obj.g;
cout<<"Enter generator:";
for(i=0;i<obj.g;i++)
{
cin>>obj.generator[i];
}
obj.f=obj.f+obj.g-1;
obj.check();
}
void CRC::check()
{
int p[32],k=0,i;
for(i=0;i<obj.f;i++)
{
p[i]=obj.frame[i];
}
while((obj.f-k)>=g)
{
for(i=k;i<obj.g+k;i++)
{
p[i]=p[i]^generator[i];
}
for(i=obj.g+k;i<obj.f;i++)
{
p[i]=frame[i];
}
for(i=0;i<obj.f;i++)
{
if(p[i]==1)
{
k=i;
break;
}
}
}
for(i=obj.f-obj.g;i<obj.f;i++)
{
obj.frame[i]=p[i];
}
cout<<"Original message:";
for(i=0;i<obj.f;i++)
{
cout<<obj.frame[i];
}
}
int main()
{
obj.input();
return 0;
}
I was trying to solve Reduce String on codechef which says
Give a string s of length l, and a set S of n sample string(s). We do reduce the string s using the set S by this way:
Wherever Si appears as a consecutive substring of the string s, you can delete (or not) it.
After each deletion, you will get a new string s by joining the part to the left and to the right of the deleted substring.
I wrote a recursive function as follows:-
Basically what i am doing in my code is either don't delete the character or delete it if it is part of any substring but it is giving wrong answer.
#include <bits/stdc++.h>
using namespace std;
#define mx 255
int dp[mx];
unordered_map<string,int> sol;
void init(int n)
{
for(int i=0;i<n;i++)
{
dp[i]=-1;
}
}
int solve(string str,int low,int high,vector<string> smp)
{
if(low>high)
{
return 0;
}
if(dp[low]!=-1)
{
return dp[low];
}
int ans=1+solve(str,low+1,high,smp);
for(int i=low;i<high;i++)
{
string tem=str.substr(low,i-low+1);
for(int j=0;j<smp.size();j++)
{
cout<<"low i high str"<<low<<" "<<i<<" "<<high<<" "<<smp[j]<<" "<<tem<<endl;
if(tem.compare(smp[j])==0)
{
ans=min(ans,solve(str,i+1,high,smp));
}
}
}
return dp[low]=ans;
}
signed main()
{
sol.clear();
string str;
vector<string> smp;
int n;
cin>>str;
cin>>n;
for(int i=0;i<n;i++)
{
string tem;
cin>>tem;
smp.push_back(tem);
}
int len=str.length();
init(len+1);
cout<<solve(str,0,len-1,smp)<<endl;
return 0;
}
PS:
link to the question
This question is toughest(seen so far) and most beautiful(again seen so far) question based on DP ON INTERVALS.
The initial code would definitely not work since it only considers single pass on the string and would not consider remaining string after deleting the patterns again and again.
There are 3 cases:-
Case 1 Either character is not deleted.
Case 2It is deleted as a part of contiguous substring.
Case 3It is deleted as a part of subsequence that matches any word given in the set of patterns and everything that is not part of that subsequence is deleted first as a substring(which again belongs to set of words).
The third part is the most tricky and requires enough thinking and is even tougher to implement too.
So for every substring we need to check whether this substring can be completely destroyed or not.
The function compute_full_recur() is the function that ensures that whether substring can be deleted either in Case 2 or Case 3.
The function compute_full takes care of Case 1.And finally this code will not run on codechef link since all the function are recursive with memoization but to verify the code is working i Have run it on Problem Reducto of Hackerrank which is exact similar with lower constraints.Download test cases and then run on test cases on your PC for verifying.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
#define mx 252
#define nx 40
bool full[mx][mx],vis[mx][mx],full_recur[mx][mx][nx][nx];
int ans[mx];
void init()
{
for(int i=0;i<mx;i++)
{
for(int j=0;j<mx;j++)
{
full[i][j]=false,vis[i][j]=false;
}
}
for(int i=0;i<mx;i++)
{
ans[i]=-1;
}
for(int i=0;i<mx;i++)
{
for(int j=0;j<mx;j++)
{
for(int k=0;k<nx;k++)
{
for(int l=0;l<nx;l++)
{
full_recur[i][j][k][l]=false;
}
}
}
}
}
bool compute_full_recur(string str,int low,int high,vector<string> pat,int idx,int len)
{
if(low>high&&len==pat[idx].length())
{
return true;
}
if(low>high&&len<pat[idx].length())
{
full_recur[low][high][idx][len]=false;
return false;
}
if(str[low]==pat[idx][len]&&compute_full_recur(str,low+1,high,pat,idx,len+1))
{
return full_recur[low][high][idx][len]=true;
}
for(int i=low+1;i<=high;i++)
{
if(str[low]==pat[idx][len]&&full[low+1][i]&&compute_full_recur(str,i+1,high,pat,idx,len+1))
{
return full_recur[low][high][idx][len]=true;
}
}
full_recur[low][high][idx][len]=false;
return false;
}
void compute_full(string str,int low,int high,vector<string> pats)
{
if(low>high)
{
return;
}
if(vis[low][high])
{
return;
}
vis[low][high]=true;
compute_full(str,low+1,high,pats);
compute_full(str,low,high-1,pats);
for(int i=0;i<pats.size();i++)
{
if(!full[low][high])
full[low][high]=compute_full_recur(str,low,high,pats,i,0);
}
}
int compute_ans(string str,int low,int high)
{
if(low>high)
{
return 0;
}
if(ans[low]!=-1)
{
return ans[low];
}
int sol=1+compute_ans(str,low+1,high);
for(int i=low+1;i<=high;i++)
{
if(full[low][i]==true)
{
sol=min(sol,compute_ans(str,i+1,high));
}
}
return ans[low]=sol;
}
signed main()
{
int t;
cin>>t;
while(t--)
{
string str;
int n;
vector<string> pats;
cin>>n>>str;
for(int i=0;i<n;i++)
{
string tem;
cin>>tem;
pats.push_back(tem);
}
init();
compute_full(str,0,str.length()-1,pats);
cout<<compute_ans(str,0,str.length()-1)<<endl;
}
return 0;
}
I have been debugging and seeing this code for nearly a week now still I couldn't find any solution and thus hoping to get one from the fellow developers in stack overflow.
This is the solution of the Faded Palindrome problem of September Challenge Codechef This is the problem page
#include<iostream>
#include<vector>
#include<iterator>
#include<stdlib.h>
#include<cstdio>
#include<string>
using namespace std;
string def="-1";
int main()
{
int t;
cin>>t;
//vector<string> out(t);
string out[100];
for(int i=0;i<t;i++)
{
string inp;
cin>>inp;
if(inp.size()%2) //Checking wether the number of letters is odd
{
int counter=inp.size()/2;
int counter_back=counter-1;
int counter_front=counter+1;
if(inp[counter]=='.')
inp[counter]='a';
int not_fa=0;
while(counter_back>=0&&counter_front<inp.size())
{
if(inp[counter_back]==inp[counter_front])
{counter_back--;counter_front++;}
else if(inp[counter_back]=='.'&&inp[counter_front=='.']){
inp[counter_back]=inp[counter_front]='a';
counter_back--;counter_front++;
}
else if(inp[counter_back]=='.'){
inp[counter_back]=inp[counter_front];
counter_back--;counter_front++;
}
else if(inp[counter_front]=='.'){
inp[counter_front]=inp[counter_back];
counter_back--;counter_front++;
}
else{
not_fa=1;
break;
}
}
if(not_fa)
out[i]=def;
else
out[i]=inp;
}
else{ //Checking the number of letter is even
//cout<<inp.size();
int counter_front=(inp.size())/2;
//cout<<counter_front;
int counter_back=counter_front-1;
//cout<<counter_back;
int not_fa=0;
while(counter_back>=0&&counter_front<inp.size())
{
if(inp[counter_back]==inp[counter_front])
{//cout<<"*-";
counter_back--;counter_front++;}
else if(inp[counter_back]=='.'&&inp[counter_front]=='.'){
inp[counter_back]=inp[counter_front]='a';
counter_back--;counter_front++;
}
else if(inp[counter_back]=='.'){
//cout<<"--";
inp[counter_back]=inp[counter_front];
counter_back--;counter_front++;
}
else if(inp[counter_front]=='.'){
inp[counter_front]=inp[counter_back];
counter_back--;counter_front++;
}
else{
not_fa=1;
break;
}
}
if(not_fa)
out[i]=def;
else
out[i]=inp;
}
}
/*vector<string>::iterator itr;
for(itr=out.begin();itr!=out.end();itr++)
cout<<*itr<<endl;*/
for(int j=0;j<t;j++)
if(j==t-1)
cout<<out[j];
else
cout<<out[j]<<"\n";
return 0;
}
While your code may pass all the test cases presented in the problem statement, there are still more edge cases that your code doesn't cover. Here's a sample test case:
6
..
...
..e
a..a.v.
p..p
t.xzt
This should print:
aa
aaa
eae
avaaava
paap
tzxzt
instead it prints:
..
.a.
aaa
aa.a.aa
p..p
taxat
Hints:
The first if statement is not correctly handling the case where inp[b] and inp[f] are both '.'.
There is no need for doing two different things when |s| is even or odd.
Also, in the first else if statement, you probably meant inp[counter_front]=='.' instead of inp[counter_front=='.'].
if(inp[counter_back]==inp[counter_front]) is true if they both are equal to '.' so you won't fill that gap.
I wrote the following program to find the nearest largest palindrome for each of the entered numbers. The first line gives input of how many integers are to be entered.
My problem is I've tried with test cases on geekforgeeks and spojtoolkit (also random numbers whose nearest palindrome I know) and am getting the right answer. SPOJ however says wrong answer. Can anyone suggest cases where my code may not work or possible corrections in my code.
using namespace std;
void evenmirror(char g[1000000],int n)
{
int k=n/2;
for(int i=n/2-1;i>=0;i--)
{
g[k]=g[i];
k++;
}
cout<<g;
}
void oddmirror(char g[1000000],int n)
{
int k=n/2+1;
for(int i=n/2-1;i>=0;i--)
{
g[k]=g[i]; k++;
}
cout<<g;
}
int main()
{
int n,i,j,m;
char g[1000000];
cin>>m;
for(int t=0;t<m;t++)
{
cin>>g;
n=strlen(g);
if(n==1 && g[0]!='9'){cout<<++g[0]; continue;}//single digits except9
int s=0;
int h=0;
if(g[0]=='9' && g[n-1]=='9')
{
for(i=0;i<n;i++)
{
if(g[i]=='9') {h++; }
}
if(h==n)
{
for(i=0;i<=n;i++)
{
if(i==0 || i==n) { cout<<1;}
else {cout<<0;}
}
s=1;
}
}
if(n%2==0 && s==0)
{
i=n/2-1; j=n/2;
while(g[i]==g[j] && i>0)
{
i--; j++;
if(i==0){break;}
}
//if palindrome
if(j==n-1 && g[j]==g[i])
{
if(g[n/2-1]<'9'){ g[n/2-1]++; }
else if(g[n/2]=='9'){(g[n/2-1])='0'; g[n/2-2]+=1;}
evenmirror(g,n);
}//if even string is not palindrome
else
{
if(g[i]<g[j])
{
if(g[n/2-1]<'9'){ g[n/2-1]++; }
else if(g[n/2]=='9'){ g[n/2-1]='0'; g[n/2-2]+=1;}
evenmirror(g,n);
}
else{ evenmirror(g,n); }
}
}
//if odd number string
else if(n%2!=0 && s==0)
{
i=(n/2)-1; j=(n/2)+1;
while(g[i]==g[j] && i>0)
{
i--; j++;
}
//if palindrome
if(j==n-1 && g[j]==g[i])
{
if(g[n/2]<'9'){ g[n/2]++; }
else if(g[n/2]=='9'){ g[n/2]='0'; g[n/2-1]+=1;}
oddmirror(g,n);
}//if odd not palindrome
else
{
char k=n/2+1;
if(g[i]<g[j])
{
if(g[n/2]<'9'){ g[n/2]++; }
else if(g[n/2]=='9'){(g[n/2])='0'; g[n/2-1]+=1;}
oddmirror(g,n);
}
else{ oddmirror(g,n); }
}
}
cout<<endl;
}
return 0;
}
Here is the question: http://www.spoj.com/problems/PALIN/
I didn't debug your code (you didn't ask for it and I think that it should be done by you anyway) but for the following input I got wrong answer:
Input: 9 (expected output 11)
Output: :
Good luck fixing it :)
Since this is an algorithm question, I'll try to keep it language neutral.
The simple way to find the next palindrome is to keep testing until you find one, stepping up one at a time. This is simply a loop, incrementing one at a time and testing.
The test for a palindrome is quite simple. I think you've overcomplicated it by splitting it into odd and even cases. You can either turn the number into a string and check the reverse() is the same, which is neat and tidy (because it's inside a call), but takes memory, or you can do it in place using two pointers. One pointer at the beginning of the string, and one at the end, each moving towards the middle, with the condition being they have to show the same character as the other, and the loop ending when they both point to the middle, or the have switched relative positions.
#include <iostream>
#include <vector>
#define MAXX 1000
using namespace std;
int number[MAXX], digits=0;
int adjust(int i)
{
if(number[i]<9)
{
cout<<"i = "<<i<<" "; //PROBLEM
(number[i])++;
return i;
}
number[i]=0;
adjust(i-1);
}
void makePalindrome(int head,int tail)
{
int revert;
if(head>tail)
return;
if(number[head]==number[tail])
{
makePalindrome(head+1,tail-1);
}
if(number[tail]<number[head])
{
number[tail]=number[head];
makePalindrome(head+1,tail-1);
}
if (number[tail]>number[head])
{
number[tail]=number[head];
revert=adjust(tail-1);
if(revert<=head)
{
makePalindrome(revert,digits-revert-1);
}
else
{
makePalindrome(head+1,tail-1);
}
}
}
int main(int argc, char const *argv[])
{
long long int num,num_copy;
int head,tail;
int number_reverse[MAXX];
cout<<"Enter the number whose next greater palindrome you want to find" <<endl;
cin>>num;
num_copy=num;
while(num_copy>0)
{
number_reverse[digits]=num_copy%10;
digits++;
num_copy=num_copy/10;
}
//cout<<"Digits = "<<digits<<"\n";
for (int i = digits-1; i >=0; --i)
{
number[digits-i-1]=number_reverse[i];
//cout<<number[digits-i-1]<<" ";
}
head=0; tail=digits-1;
makePalindrome(head,tail);
cout<<"Answer : ";
for (int i = 0; i < digits; ++i)
{
cout<<number[i];
}
cout<<"\n";
return 0;
}
When I am running with an input : 94187978322, it is giving two different answers with and without a "cout" line (line with comment "PROBLEM").
Here's the output:
ishivendra:code shivendraagrawal$ g++ next_palindrome.cpp
ishivendra:code shivendraagrawal$ ./a.out
Enter the number whose next greater palindrome you want to find
94187978322
Answer : 94188078149
ishivendra:code shivendraagrawal$ g++ next_palindrome.cpp
ishivendra:code shivendraagrawal$ ./a.out
Enter the number whose next greater palindrome you want to find
94187978322
i = 7 i = 6 i = 4 Answer : 94188088149
The second output is the desired output. Can you point out the cause of this difference and incorrectness for the first one ?
I figured half of it out, it was a logical error.
I should have written return adjust(i-1).
I still don't know the cause for the peculiarity (There was no compilation or runtime error) but at least my code is running as desired now.