I did it using and also checked previously it's been answered (Recursively remove all adjacent duplicates) using the same logic but when I applied that it's showing the segmentation fault! Please help in correcting the code. The link to the question is https://www.geeksforgeeks.org/recursively-remove-adjacent-duplicates-given-string/
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
string s,ans;
cin>>s;
stack<char>st;
int n=s.size();
cout<<n;
int i=0;
while(n--)
{
char b;
if(s[i]==st.top())
{
b=st.top();
st.pop();
i++;
continue;
}
if(s[i]==b)
{
i++;
continue;
}
st.push(s[i]);
b='1';
i++;
}
// char a[st.size()];
int j=0;
while(!st.empty())
{
// a[j]=st.top();
ans.push_back(st.top());
// cout<<st.top();
st.pop();
j++;
}
// cout<<ans<<endl;
}
//code
return 0;
}
Related
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!
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;
}
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;
Given a string consisting of spaces,\t,\n and alphabets,task is to count the number of words where spaces,\t and \n work as separators.
For this problem, I wrote following code, which doesn't works properly, and suggestions why?
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
bool isalpha(char ch)
{
if((ch>='A'&& ch<='Z')||(ch>='a'&& ch<='z'))
return true;
return false;
}
int main()
{
int t;
cin>>t;
while(t--)
{
string str;
int count=0;
getline(cin,str);
for(int i=0;i<str.length();i++)
{
if(isalpha(str[i]))
{
count++;
while(str[i]!='\n'||str[i]!='\t'||str[i]!=' ')
{
i++;
}
}
else
continue;
}
cout<<count<<endl;
}
return 0;
}
So I was trying to count the total number of negative numbers in the stack and I wrote this code but something went wrong with it and its showing no output. I am a beginner in c++ so I am sure it will be quite wrong.
#include <iostream>
#include <stack>
using namespace std;
size_t i(stack<int> s){
int count=0;
while(s.size() !=0){
if(s.top()<0){
count++;
s.pop();
} else if(s.top()>0){
s.pop();
} else{}
cout<<count<<endl;
}
return count;
}
int main(){
stack<int> s;
s.push(-1);
s.push(2);
s.push(-2);
size_t i(stack<int> s);
return 0;
}
In your main() function you do not call i() you simply redeclare it.
#include <iostream>
#include <stack>
using namespace std;
size_t i(stack<int> s){
int count=0;
while(s.size() !=0){
if(s.top()<0){
count++;
s.pop();
} else if(s.top()>0){
s.pop();
} else{}
cout<<count<<endl;
}
return count;
}
int main(){
stack<int> s;
s.push(-1);
s.push(2);
s.push(-2);
size_t i(stack<int> s); // this DOES NOT call the function
i(s); // <== THIS calls the function!!!
return 0;
}
Your statement size_t i(stack<int> s); does not call the function, it simply tells the compiler what parameters it accepts and what its return type is.