Why am I getting SIGCONT error on codechef ide? - c++

I am getting SIGCONT error while running the code in codechef ide whereas on my local ide the same code runs fine.
The problem code is "TYPING" and it is from Snackdown Practice Contest: Beginner.
The code is below:
#include <iostream>
#include <iterator>
#include <map>
#include <string>
typedef long long ll;
ll FindCount(const char* str,ll num){
char previous =str[0];
ll individual_count=2;
for(ll i =1 ;i<num;i++){
if(previous=='d'||previous=='f'){
if(str[i]=='d'||str[i]=='f'){
individual_count+=4;
}
else{
individual_count+=2;
}
}
else{
if(str[i]=='j'||str[i]=='k'){
individual_count+=4;
}
else{
individual_count+=2;
}
}
previous=str[i];
}
return individual_count;
}
int main()
{
ll t;
std::cin>>t;
while(t--)
{
std::map<std::string,ll> m;
ll count=0;
ll n=0;
std::cin>>n;
while(n--){
std::string str;
std::cin>>str;
std::map<std::string,ll>::iterator itr;
itr=m.find(str);
if(itr!=m.end()){
count+=(itr->second)/2;
}
else{
ll num = str.size();
ll temp = FindCount(str.c_str(),num);
count+=temp;
m.insert({str,temp});
}
}
std::cout<<count<<"\n";
}
return 0;
}
The link to the problem statement is: https://www.codechef.com/SDPCB21/problems/TYPING
I am getting an infinite number of '0' as output each on new line.
Please tell me if there is any logical error or error of any kind in the code.
Thankyou.

Related

global vairable did not initialize correctly between running multiple test cases in c++

I am still actively learning c++ with a strong background in python3, the point of this question is not seeking any help with solving the problem Decode Variations on leetcode or pramp, but to understand the compilation or syntax related issue in c++.
The following code using dfs runs well if I run it case by case, however on pramp, it failed in RUN TESTS! Very surprising! It seems like in test case #2 int n=0; was not initialized and used the output of n in test case #1 as its value rather than 0, see the console in the attached screenshot at the end.
#include <iostream>
#include <string>
using namespace std;
int n=0;
void dfs(const string& s, int i){
if (i==s.size()){
n++;
return;
}
if ( 0<s[i]-'0' && s[i]-'0'<10)
dfs(s, i+1);
if (i+1<s.size() && 10<=stoi(s.substr(i,2)) && stoi(s.substr(i,2))<=26)
dfs(s, i+2);
}
int decodeVariations(const string& s)
{
dfs(s,0);
cout<<n<<endl;
return n;
}
int main()
{
return 0;
}
Here is the code to run test case #2:
int main()
{
const string s = "26";
dfs(s,0);
cout<<n<<endl;
return 0;
}
If I added another initialization of n=0; to int decodeVariations(const string& s), then everything works fine. I try to become a programmer with a clear mind, please educate me.
Yes, non-const global variable is evil. Even though I don't know how leetcode and pramp (especially, the main function is empty) run a number of test cases, but I get a hunch it runs test case in the main function, which only compile and run the code once. Thus the global did not get reinitialized.
#include <iostream>
#include <string>
using namespace std;
int n=0;
void dfs(const string& s, int i){
if (i==s.size()){
n++;
return;
}
if ( 0<s[i]-'0' && s[i]-'0'<10)
dfs(s, i+1);
if (i+1<s.size() && 10<=stoi(s.substr(i,2)) && stoi(s.substr(i,2))<=26)
dfs(s, i+2);
}
int decodeVariations(const string& s)
{
dfs(s,0);
return n;
}
int main(int argc, char **argv){
for (int i=1;i<argc;i++)
cout<<decodeVariations(argv[i])<<endl;
}
run with ./test 26 26 26
output:
2
4
6
Quick fix is to get rid of global variable
#include <iostream>
#include <string>
using namespace std;
//int n=0;
int dfs(const string& s, int i){
int ans = 0;
if (i==s.size()){
return 1;
}
if ( 0<s[i]-'0' && s[i]-'0'<10)
ans += dfs(s, i+1);
if (i+1<s.size() && 10<=stoi(s.substr(i,2)) && stoi(s.substr(i,2))<=26)
ans += dfs(s, i+2);
return ans;
}
int decodeVariations(const string& s)
{
// your code goes here
int n;
n = dfs(s,0);
cout<<n<<endl;
return n;
}

Why is this code running showing SIGKILL error?

the code runs printing 94601019043208 in endless lines and then shows SIGKILL error.please help me out handling this error.
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
long long int t;
cin>>t;
while(t>0)
{
long long int l,r,x,y;
cin>>x>>y>>l>>r;
long long int res;
if(x>y)
res=x;
else
res=y;
long long int sum,i;
for(i=res;i<=r;i++)
{
sum=((x&i)*(y&i));
if(sum==(x*y))
break;
}
cout<<i<<endl;
t=t-1;
}
return 0;
}

Find segmentation fault while passing map to function C++

I can't find the segmentation fault which is occuring on the line where I am calling calculate_grundy in main. Please help.
I have also added a screenshot of my debugger while debugging the code.
This is a question of a running contest so please don't answer logic problems instead of helping me in finding the segmentation fault.
I am passing the dp map for storing the subproblems, as done in dynamic programming.
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
ll calculate_mex(unordered_set<ll> Set)
{
ll mex = 0;
while(Set.find(mex)!=Set.end())
{
mex++;
}
return mex;
}
ll calculate_grundy(ll n,ll m,map<pair<ll,ll>,ll> &dp)
{
auto it = dp.find(make_pair(n,m));
if(it!=dp.end())
{
return it->second;
}
else
{
ll ans;
ll greater = max(n,m);
ll smaller = min(n,m);
if(n==m || m==0 || n==0)
{
return 0;
}
else if(n==1 || m==1)
{
return n==1?m:n;
}
unordered_set<ll> Set;
ll limit = greater%smaller;
for(ll i=1;i<=limit;i++)
{
ll mult = smaller*i;
Set.insert(calculate_grundy(greater-mult,smaller,dp));
}
ans = calculate_mex(Set);
dp.insert(make_pair(make_pair(n,m),ans));
return ans;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin>>t;
while(t--)
{
ll n,m;
cin>>n>>m;
map<pair<ll,ll>,ll> dp;
if(calculate_grundy(n,m,dp)==0)
{
cout<<"Ari"<<"\n";
}
else
{
cout<<"Rich"<<"\n";
}
}
}

Runtime Error encountered while running on Online Judges

I have been solving this problem on SPOJ . One of the most frequent problems encountered while using online judges is Runtime Error. You never know which case leads to a Segmentation fault. Please help me figure out why does the code below correspond to a Runtime Error or Segmentation Fault even though I have ensured that every possible case runs properly on my linux gcc.
#include <iostream>
#include <math.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
int main ()
{
int t,i;
cin >> t;
string s;
vector<int> A;
while(t--)
{
cin >> s;
int n=s.size();
int temp;
if (n!=1)
{
for(i=0;i<s.size();i++)
{
A.push_back(s.at(i)-'0');
}
if(n%2!=0)
{ i = (n-1)/2;
while(A[i-1]==A[n-i])
i--;
i--;
}
else
{ i=n/2-1;
while(A[i]==A[n-1-i])
i-- ;
}
if (A[i]<A[n-i-1])
{
if ((n%2)!=0)
{
A[n/2] = A[n/2] + 1;
A[n-i-1] = A[i];
}
else
{
A[n/2-1] = A[n/2-1]+1;
A[n/2] = A[n/2-1];
A[n-i-1] = A[i];
}
}
else
A[n-i-1] = A[i];
while(i--)
A[n-i-1] = A[i];
while(!(A.empty()))
{
printf("%d",A.back());
A.pop_back();
}
}
else
cout << s;
}
}
I found one of your problems for segmentation fault , when you give for example "aaaaa" input to s you will get segmentation fault on this part of code
if(n%2!=0)
{ i = (n-1)/2;
while(A[i-1]==A[n-i])
i--;
i--;
}
because you didn't check i < 0 or not

How can I fix a SPOJ time limit exceeded error on the STAMPS challenge?

Here is the link for the problem: http://www.spoj.com/problems/STAMPS/;
here is the ideone link for the current code: http://ideone.com/AcHfc6;
here is the code:
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
int t,x,n,sum,sum2,count,i,j;
scanf("%d",&t);
for(j=1;j<=t;j++)
{
cin>>x>>n;
sum=0;
int offer[n];
for(i=0;i<n;i++)
{
cin>>offer[i];
sum+=offer[i];
sort(offer,offer+n);
}
if(sum>=x)
{
sum2=0;
count=0;
for(i=n-1;i>=0;i--)
{
sum2+=offer[i];
if(sum2<=x)
count++;
else
break;
}
cout<<"Scenario #"<<j<<":"<<endl;
cout<<count<<endl;
cout<<endl;
}
else
{
cout<<"Scenario #"<<j<<":"<<endl;
cout<<"impossible"<<endl;
cout<<endl;
}
}
return 0;
}
The code gives the right answers for the given test cases but it causes TLE. I've tried converting my cin's and cout's to scanf/printf but, weirdly enough, the answers were not the same and I don't know how the answers were different from each other.
What's going wrong?
I suspect your main problem is here:
for(i=0;i<n;i++)
{
cin>>offer[i];
sum+=offer[i];
sort(offer,offer+n);
}
You sort the data for every number that's entered. Also, you sort random data because you only have i rows of valid data in the array, not n rows. The sort should be done once, outside the loop:
for(i=0;i<n;i++)
{
cin>>offer[i];
sum+=offer[i];
}
sort(offer,offer+n);