I am Getting this error on an online judge~ - c++

I am getting this error on an online judge:
no matching function for call to 'find(std::vector<char>::iterator, std::vector<char>::iterator, __gnu_cxx::__alloc_traits<std::allocator<char> >::value_type&)'
it=find(s.begin(),s.end(),words[i]);
What is my mistake?
This is my code-
#include<iostream>
using namespace std;
#include<vector>
int main()
{
string guest,host,words;
cin>>guest>>host>>words;
vector<char>s;
vector<char>::iterator it;
for(int i=0;i<guest.length();i++)
{
s.push_back(guest[i]);
}
for(int i=0;i<host.length();i++)
{
s.push_back(host[i]);
}
for(int i=0;i<words.length();i++)
{
it=find(s.begin(),s.end(),words[i]);
if(it!=s.end())
{
s.erase(it);
}
else{
cout<<"NO";
return 0;
}
}
cout<<"YES";
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!

Finding next palindrome of a number, getting SIGABRT error

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;
}

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;

SIGTSTP error in some online ide but the program works fine in my terminal

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,n;
cin>>t;
for(int i=1;i<=t;i++)
{
int c=0;int C[t]={};
cin>>n;
int P[n]={};int G[n]={};
for(int j=0;j<n;j++)
{
cin>>P[j];
}
for(int k=0;k<n;k++)
{
int asn=1;int b=5;
if(k!=0)
{
if(k<5)
{
for(int l=0;l<k;l++)
{
if(P[l]<P[k])
{
asn*=0;
}
}
}
else
{
while(b>0)
{
if(P[k-b]<P[k])
{
asn*=0;
}
b--;
}
}
}
G[k]=asn;
}
for(int f=0;f<n;f++)
{
if(G[f]==1)
{
c++;
}
}
cout<<c<<"\n";
}
}
Can anyone please help me in finding the SIGTSTP error in this code.In my PC this program works fine but gives a run-time error while compiling in Codechef's online IDE.
The link to the problem is https://www.codechef.com/OCT19B/problems/S10E

Getting Error in this program: Checking if two array equal?

While compiling the program an error is popping up which I'm not able to figure out what it is: "No matching function call to _distance_fw(int&,int&)".
Here in this problem I'm trying to check if two arrays are equal in terms of the values they contain.
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
unordered_map<int,int> bucket;
unordered_map<int,int>::const_iterator it;
int main()
{
int T,N,val,counter;
int def=1;
bool bug;
scanf("%d",&T);
{
for(int i=0;i<T;i++)
{
scanf("%d",&N); //N is the size of array
counter=N;
bucket.erase(bucket.begin(),bucket.end());
for(int j=0;j<2;j++)
//scanning the first array
{
while(counter>0)
{
if(j==0)
{
scanf("%d",&val);
it=bucket.find(val);
if(it==bucket.end())
{
bucket.insert(val,def);
}
else
{
bucket[val]++;
}
}
else
{
scanf("%d",&val);
it=bucket.find(val);
if(it!=bucket.end())
{
bucket[val]--;
}
else
{
bug=true;
}
}
counter--;
}
if(bug==true)
{
cout<<"0"<<endl;
}
else
{
for(it=bucket.begin();it!=bucket.end();it++)
{
if(it->second!=0)
{
cout<<"0"<<endl;bug=true;break;
}
}
if(bug==false)
{
cout<<"1"<<endl;
}
}
}
}
}
}
You simply need to change
bucket.insert(val,def)
Into:
bucket.insert({val,def})
std::unordered_map::insert needs a std::unordered_map::value_type which in this case is std::pair<const int int>.
And please format your code better.