I am working on http://www.spoj.com/problems/WORDCNT/ , for which I have come up with following code.
I feel my code is working fine, but there is some problem in taking input.
I am using getline(cin,string);.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int i,j,test;
cin>>test;
cin.ignore();
while(test--)
{
int d=0,c=0,big=0,cons=1,k=0,t=0;
string name,temp;
getline (cin,name);
name=name+" ";
for(i=0;i<name.length();i++)
{
if(name[i]!=' ')
{
t=1;
++c;
}
else
{
++d;
if(d==1)
{
k=c;
c=0;
}
else
{
if(c==k)
{
cons++;
c=0;
if(cons>big)
{
big=cons;
}
}
else
{
k=c;
c=0;
cons=1;
if(cons>big)
{
big=cons;
}
}
}
}
}
if( (big==0) && (t==1))//to check for blank and single character string
{
big=1;
}
cout<<big<<"\n";
}
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;
}
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;
}
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;
}
I have a problem with my boolean function check_gift. It gives the value false while the gift is in the store...
What am I doing wrong?
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
#include <string>
#include <cassert>
using namespace std;
typedef vector<string> Wishes;
int size(Wishes& w){ return static_cast<int>(w.size()); }
struct Wishlist
{
double budget;
Wishes wishes;
};
struct Gift
{
double price;
string name;
};
typedef vector<Gift> Giftstore;
int size(Giftstore& g) { return static_cast<int>(g.size()); }
void read_wishlist_into_struct(ifstream& infile, Wishlist& wishlist)
{
double b;
infile>>b;
wishlist.budget=b;
int i=0;
string name;
getline(infile,name);
while(infile)
{
wishlist.wishes.push_back(name);
i++;
getline(infile,name);
}
infile.close();
}
void show_wishlist(Wishlist wishlist)
{
cout<<"Budget: "<<wishlist.budget<<endl<<endl;
cout<<"Wishes: "<<endl;
for(int i=0; i<size(wishlist.wishes); i++)
{
cout<<wishlist.wishes[i]<<endl;
}
cout<<endl;
}
void read_giftstore_into_vector(ifstream& infile, Gift& gift, Giftstore& giftstore)
{
double p;
string name;
int i=0;
infile>>p;
while(infile)
{
gift.price=p;
getline(infile,name);
gift.name=name;
giftstore.push_back(gift);
i++;
infile>>p;
}
infile.close();
}
void show_giftstore(Giftstore giftstore)
{
cout<<"All possible gifts in giftstore: "<<endl<<endl;
for(int i=0; i<size(giftstore); i++)
{
cout<<giftstore[i].price<<"\t"<<giftstore[i].name<<endl;
}
cout<<endl;
}
bool check_gift(Giftstore giftstore, string giftname)
{
int i=0;
while(i<size(giftstore))
{
if(giftstore[i].name==giftname)
{
return true;
}
else
{
i++;
}
}
return false;
}
void clear(Wishlist& b)
{
b.budget=0;
while(!b.wishes.empty())
{
b.wishes.pop_back();
}
}
void copy(Wishlist a, Wishlist& b)
{
b.budget=a.budget;
for (int i=0; i<size(b.wishes); i++)
{
b.wishes.push_back(a.wishes[i]);
}
}
int main ()
{
ifstream infile2("giftstore.txt");
Gift gift;
Giftstore giftstore;
read_giftstore_into_vector(infile2, gift, giftstore);
show_giftstore(giftstore);
string giftname;
giftname=("dvd Up van Pixar");
bool x;
x=check_gift(giftstore, giftname);
cout<<"in store?: "<<x<<endl;
return 0;
}
You forgot to return false at the end of your check_gift function
bool check_gift(Giftstore giftstore, string giftname)
{
int i=0;
while(i<size(giftstore))
{
if(giftstore[i].name==giftname)
{
return true;
}
else
{
i++;
}
}
return false;
}
There is no return false; at the end of the function; so if the gift is not found, the program will fall off the end and give undefined behaviour.
The compiler should warn you about this, if you enable warnings.
You need to add return false at the end of check_gift().