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

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.

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

I am Getting this error on an online judge~

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

Segmentation fault in C++ 5.1 but works fine in C++ 4.8.4

I was working on this problem and I had solved it using a trie. It runs fine for the testcases on my computer(uses GNU C++ 4.8.4 compiler), but it gives a segmentation fault(runtime error 11) on Codeforces Judge as well as Ideone for C++11 (G++ 5.1) . Infact, it works fine in C++ 4.3.2 on Ideone for the same testcase
2 3
a
b
This is my code.
#include <bits/stdc++.h>
using namespace std;
typedef struct node
{
int status;
int visited;
struct node *children[26];
}node;
node *gen_node()
{
node *newnode=new node();
newnode->status=0;
newnode->visited=0;
int i=0;
for(i=0;i<26;i++)
newnode->children[i]=NULL;
}
void insert(node *root,string s)
{
int n=s.size();
int i;
for(i=0;i<n;i++)
{
if(root->children[s[i]-'a']==NULL)
{
root->children[s[i]-'a']=gen_node();
}
root=root->children[s[i]-'a'];
}
}
void dfs(node *root)
{
root->visited=1;
int flag=0,i;
for(i=0;i<26;i++)
{
if(root->children[i]!=NULL && (root->children[i])->visited==0)
{
dfs(root->children[i]);
flag=1;
}
}
if(flag==0)
{
root->status=0;
}
else
{
root->status=0;
for(i=0;i<26;i++)
{
if(root->children[i]!=NULL)
{
root->status=(root->status)|(!((root->children[i])->status));
}
}
}
}
int main()
{
int n,k;
cin>>n>>k;
node *root=gen_node();
int i;
for(i=0;i<n;i++)
{
string s;
cin>>s;
insert(root,s);
}
dfs(root);
if(root->status==0)
{
cout<<"Second"<<endl;
}
else
{
int flag=0;
for(i=0;i<26;i++)
{
if(root->children[i]!=NULL)
{
if((root->children[i])->status==1)
{
flag=1; //root has atleast one losing strategy
}
}
}
if(flag==0)
{
if(k%2==0)
{
cout<<"Second"<<endl;
}
else
{
cout<<"First"<<endl;
}
}
else
{
cout<<"First"<<endl;
}
}
return 0;
}
I had tried debugging on Ideone( No way to use gdb :( ), and found that the segfault happens here in insert() function (through commenting and uncommenting).
if(root->children[s[i]-'a']==NULL)
{
root->children[s[i]-'a']=gen_node();
}
I am not sure how this happens. Can anyone help me out?

Segment Tree with lazy propagation Time limit problem

The following is the implementation of http://www.spoj.pl/problems/LITE/ using Segment Tree's with lazy propagation. I am new to segment trees and I cannot understand why I am getting TLE. Could someone please look at it and help me correct my error?
#include <iostream>
#include <iostream>
#include <cstdio>
#include <cstring>
#define MAX 100000
using namespace std;
int M[2*MAX+1];
int flag[2*MAX+1];
int count;
void refresh(int begin,int end,int n)
{
M[n] = end-begin+1 - M[n];
flag[n]=0;
flag[n*2] =!flag[n*2];
flag[n*2+1] =!flag[n*2+1];
}
void update(int begin,int end,int i,int j,int n=1)
{
if(flag[n])
{
refresh(begin,end,n);
}
if(begin>=i && end<=j)
{
if(!flag[n])
{
refresh(begin,end,n);
}
flag[n] = 0;
return;
}
else if(begin>=end)
{
return;
}
else
{
int mid = (begin+end)>>1;
if(i<=mid)
{
update(begin,mid,i,j,n*2);
}
if(j>mid)
{
update(mid+1,end,i,j,n*2+1);
}
if(flag[2*n])
{
refresh(begin,mid,2*n);
}
if(flag[2*n+1])
{
refresh(mid+1,end,2*n+1);
}
M[n] = M[n*2]+ M[n*2+1];
}
}
int query(int begin,int end,int i,int j,int n=1)
{
if(flag[n])
{
refresh(begin,end,n);
}
if(begin>=i && end<=j)
{
return M[n];
}
if(begin>=end)
{
return 0;
}
int mid = (begin+end)>>1;
int l=0,r=0;
if(i<=mid)
{
l = query(begin,mid,i,j,n*2);
}
if(j>mid)
{
r = query(mid+1,end,i,j,n*2+1);
}
if(flag[2*n])
{
refresh(begin,mid,2*n);
}
if(flag[2*n+1])
{
refresh(mid+1,end,2*n+1);
}
M[n] = M[n*2]+ M[n*2+1];
return l+r;
}
int main()
{
memset(M,0,sizeof M);
int n,m,a,b,c;
scanf("%d%d",&n,&m);
for(int i=0; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c);
if(a==0)
{
update(1,n,b,c);
}
else
{
printf("%d\n",query(1,n,b,c));
}
}
return 0;
}
M[node]^=1; might be faster than M[node] = (M[node]==0)?1:0;, and (begin+end)>>1 faster than (begin/end)/2, but not very relevant
LE: Try if making the recursive functions inline will run faster. I think it unravels the recursion a couple of times and works a little bit faster. Maybe sending the parameters as references will make it run faster, try that out. If the test cases are chosen properly you still shouldn't be able to pass the tests with this trickery, but it helps sometimes.