SPOJ: What is the difference between these two answers for KURUK14 - c++

I have solved this problem and got AC. My problem is related to equivalence of following two approaches. The first code got accepted, while the second didn't.
As far as I can discern, both are completely equivalent for all the (valid) test cases any human can think of. Am I wrong? If so, what test case can differentiate them?
Code#1 (Accepted one):
#include <cstdio>
bool* M;
bool proc(int N){
for(int j=0;j<=N;j++){
M[j]=false;
}
for(int i=0;i<N;i++){
int a=0;
scanf("%d",&a);
if(a>=N)
return false;
else if(!M[a])
M[a]=true;
else if(!M[N-1-a])
M[N-1-a]=true;
}
bool f = true;
for(int k=0;k<N;k++)
{
f = f && M[k];
}
return f;
}
int main() {
M=new bool[1002];
int num=0;
scanf("%d",&num);
while(num){
int N=0;
scanf("%d",&N);
if(proc(N))
printf("YES\n");
else
printf("NO\n");
num--;
}
return 0;
}
Code #2 (WA):
#include <cstdio>
bool* M;
bool proc(int N){
for(int j=0;j<=N;j++){
M[j]=false;
}
for(int i=0;i<N;i++){
int a=0;
scanf("%d",&a);
if(a>=N)
return false;
else if(!M[a])
M[a]=true;
else if(!M[N-1-a])
M[N-1-a]=true;
else
return false;
}
return true;
}
int main() {
//Exactly same as code#1
}

The bug has nothing to do with the algorithm itself—it's very possible both the algorithms are correct. But the second implementation is wrong.
When you reach a test case which should return NO, you exit the function prematurely. Which means there are some numbers from the current test case left unread in the input, which of course confuses further reading thoroughly. This means the bug only manifests when T > 1.

Related

C++: Error in login, identify a valid triangle with greater greater then 0, all three angles provided

I have written this program, it passes all manual test conditions but says "wrong answer" when I submit online on an IDE.
Constraints
0≤a,b,c≤180
#include <iostream>
using namespace std;
int main() {
// your code goes here
double a,b,c;
cin>>a>>b>>c;
if(a+b+c==180)
cout<<"YES";
else
cout<<"NO";
return 0;
}
The above code doesn't give correct answer when either of a,bc is zero and the a+b+c=180.
So,
int main()
{
// your code goes here
double a,b,c;
cin>>a>>b>>c;
//add the below statement
if((a!=0)&&(b!=0)&&(c!=0)){
if(a+b+c==180)
cout<<"YES";
else
cout<<"NO";
}
else{
cout<<"NO";
}
return 0;
}
int main()
{
// your code goes here
double a,b,c;
cin>>a>>b>>c;
//made the changes with the help of suggestion from the forum
if((a>0)&&(b>0)&&(c>0)&&(a+b+c==180))
cout<<"YES";
else
cout<<"NO";
return 0;
}

why doesnt the bool function return false?

I'm soo sorry I searched for and read similar questions but couldn't understand/use them to solve my own.
Im writing a bool function within an if statement but the function doesn't seem to return false, what am I doing wrong.
My bool function just checks if there are more than one of the given number in an array:
bool findsame(int a[], int b){
int k=0;
for(int i=0;i<20;i++){
if(a[i]==b){
k++;
}
}
if(k>1){
return true;
}
else{
return false;
}
}
int main()
{
const int size=20;
int a[size]={4,4};
int b=4;
if(findsame(a,b)){
cout<<"true";
}
}
I think you got confused why "false" is not getting printed on console with the function returning the false value.
You need to add to an extra else statement to print false on the console:
if(findsame(a,b)){
std::cout<<"true";
}else{
std::cout<<"false";
}
Also, there are two 4 values in the array, therefore always true will get printed.
Try passing value of b other than 4 and 0.
Have a look at the following implementation where value of variable b is equal to 1:
#include<iostream>
bool findsame(int a[], int b){
int k=0;
for(int i=0;i<20;i++){
if(a[i]==b){
k++;
}
}
if(k>1){
return true;
}
else{
return false;
}
}
int main()
{
const int size=20;
int a[size]={4,4};
int b=1;
if(findsame(a,b)){
std::cout<<"true";
}else{
std::cout<<"false";
}
}
Output:
false
PS: I have also tested code for the value of b = 4 and it prints true. Check and Run the code here: https://onlinegdb.com/S1LR5PtvD

Similar solutions but different answers

I am facing difficulty while solving a problem where we have to check whether a string is a subsequence of another string or not.
A man with name M is allowed to marry a woman with name W, only if M is a subsequence of W or W is a subsequence of M.
A is said to be a subsequence of B if A can be obtained by deleting some elements of B without changing the order of the remaining elements.
Example -
john and johanna will give "YES" as output
kayla and jayla will give "NO" as output
johanna and john will give "YES" as output
My code is :
#include <iostream>
#include<string>
using namespace std;
bool checksub(string a, string b)
{
int pos=0;
for(int i=0; i<a.size(); i++)
{
int flag=0;
for(int j=pos; j<b.size(); j++)
{
if(b[j]==a[i])
{
flag=1;
pos=j;
break;
}
}
if(flag==0)
{
return false;
}
}
return true;
}
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
string a,b;
cin>>a>>b;
if(a.size()==b.size())
{
if(a==b)
{
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;}
}
else if(a.size()>b.size()){
if(checksub(b,a))
{
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}else{
if(checksub(a,b))
{
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
}
return 0;
}
The editorial of the question uses a similar approach. Can anybody tell me what's wrong with my code?
The editorial solution is given below :
#include <cstdio>
char M[25005], W[25005];
bool contains(const char *A, const char *B){
while(*A){
if(*B==*A)
B++;
A++;
}
return !*B;
}
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%s %s", M, W);
puts(contains(M, W) || contains(W, M) ? "YES" : "NO");
}
return 0;
}
Link to the problem: https://www.codechef.com/problems/NAME2
Your code produces the wrong result with input AA BAB because it fails to account for the fact that you need to have two A in the second string.
You might be able to fix it by changing pos=j; to pos=j+1; but I'm not certain.
There really is no similarity between your code and the editoral code however. Even with my suggested fix (if it does work) your code is clearly less efficient than the editorial code because it scans the input strings repeatedly.
Got the correct answer just by changing pos=j+1 instead of pos=j. Then code will become similar to the pseudocode in the editorial. Thanks, everybody for answering.

Getting wrong answer in a DP problem although implementation looks correct

I was trying to solve Reduce String on codechef which says
Give a string s of length l, and a set S of n sample string(s). We do reduce the string s using the set S by this way:
Wherever Si appears as a consecutive substring of the string s, you can delete (or not) it.
After each deletion, you will get a new string s by joining the part to the left and to the right of the deleted substring.
I wrote a recursive function as follows:-
Basically what i am doing in my code is either don't delete the character or delete it if it is part of any substring but it is giving wrong answer.
#include <bits/stdc++.h>
using namespace std;
#define mx 255
int dp[mx];
unordered_map<string,int> sol;
void init(int n)
{
for(int i=0;i<n;i++)
{
dp[i]=-1;
}
}
int solve(string str,int low,int high,vector<string> smp)
{
if(low>high)
{
return 0;
}
if(dp[low]!=-1)
{
return dp[low];
}
int ans=1+solve(str,low+1,high,smp);
for(int i=low;i<high;i++)
{
string tem=str.substr(low,i-low+1);
for(int j=0;j<smp.size();j++)
{
cout<<"low i high str"<<low<<" "<<i<<" "<<high<<" "<<smp[j]<<" "<<tem<<endl;
if(tem.compare(smp[j])==0)
{
ans=min(ans,solve(str,i+1,high,smp));
}
}
}
return dp[low]=ans;
}
signed main()
{
sol.clear();
string str;
vector<string> smp;
int n;
cin>>str;
cin>>n;
for(int i=0;i<n;i++)
{
string tem;
cin>>tem;
smp.push_back(tem);
}
int len=str.length();
init(len+1);
cout<<solve(str,0,len-1,smp)<<endl;
return 0;
}
PS:
link to the question
This question is toughest(seen so far) and most beautiful(again seen so far) question based on DP ON INTERVALS.
The initial code would definitely not work since it only considers single pass on the string and would not consider remaining string after deleting the patterns again and again.
There are 3 cases:-
Case 1 Either character is not deleted.
Case 2It is deleted as a part of contiguous substring.
Case 3It is deleted as a part of subsequence that matches any word given in the set of patterns and everything that is not part of that subsequence is deleted first as a substring(which again belongs to set of words).
The third part is the most tricky and requires enough thinking and is even tougher to implement too.
So for every substring we need to check whether this substring can be completely destroyed or not.
The function compute_full_recur() is the function that ensures that whether substring can be deleted either in Case 2 or Case 3.
The function compute_full takes care of Case 1.And finally this code will not run on codechef link since all the function are recursive with memoization but to verify the code is working i Have run it on Problem Reducto of Hackerrank which is exact similar with lower constraints.Download test cases and then run on test cases on your PC for verifying.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
#define mx 252
#define nx 40
bool full[mx][mx],vis[mx][mx],full_recur[mx][mx][nx][nx];
int ans[mx];
void init()
{
for(int i=0;i<mx;i++)
{
for(int j=0;j<mx;j++)
{
full[i][j]=false,vis[i][j]=false;
}
}
for(int i=0;i<mx;i++)
{
ans[i]=-1;
}
for(int i=0;i<mx;i++)
{
for(int j=0;j<mx;j++)
{
for(int k=0;k<nx;k++)
{
for(int l=0;l<nx;l++)
{
full_recur[i][j][k][l]=false;
}
}
}
}
}
bool compute_full_recur(string str,int low,int high,vector<string> pat,int idx,int len)
{
if(low>high&&len==pat[idx].length())
{
return true;
}
if(low>high&&len<pat[idx].length())
{
full_recur[low][high][idx][len]=false;
return false;
}
if(str[low]==pat[idx][len]&&compute_full_recur(str,low+1,high,pat,idx,len+1))
{
return full_recur[low][high][idx][len]=true;
}
for(int i=low+1;i<=high;i++)
{
if(str[low]==pat[idx][len]&&full[low+1][i]&&compute_full_recur(str,i+1,high,pat,idx,len+1))
{
return full_recur[low][high][idx][len]=true;
}
}
full_recur[low][high][idx][len]=false;
return false;
}
void compute_full(string str,int low,int high,vector<string> pats)
{
if(low>high)
{
return;
}
if(vis[low][high])
{
return;
}
vis[low][high]=true;
compute_full(str,low+1,high,pats);
compute_full(str,low,high-1,pats);
for(int i=0;i<pats.size();i++)
{
if(!full[low][high])
full[low][high]=compute_full_recur(str,low,high,pats,i,0);
}
}
int compute_ans(string str,int low,int high)
{
if(low>high)
{
return 0;
}
if(ans[low]!=-1)
{
return ans[low];
}
int sol=1+compute_ans(str,low+1,high);
for(int i=low+1;i<=high;i++)
{
if(full[low][i]==true)
{
sol=min(sol,compute_ans(str,i+1,high));
}
}
return ans[low]=sol;
}
signed main()
{
int t;
cin>>t;
while(t--)
{
string str;
int n;
vector<string> pats;
cin>>n>>str;
for(int i=0;i<n;i++)
{
string tem;
cin>>tem;
pats.push_back(tem);
}
init();
compute_full(str,0,str.length()-1,pats);
cout<<compute_ans(str,0,str.length()-1)<<endl;
}
return 0;
}

Runtime error SIGSEGV infix to postfix

This code works fine on my machine but when i upload it to codechef it gives me a runtime error SIGSEGV. Can anyone please point out the error in my code? This is the question i made it for http://www.codechef.com/problems/ONP/
#include<iostream>
#include<string>
using namespace std;
class stack
{
public:
void push(char a)
{
++top;
arr[top]=a;
}
void pop()
{
top--;
}
void initialize(int size)
{
top=-1;
max=size;
}
bool chckfull()
{
return (top==max-1);
}
bool chckempty()
{
return (top==-1);
}
char front()
{
return arr[top];
}
private:
int top;
int max;
char arr[404];
};
int chckalphanum(char y)
{
if((y>='a')&&(y<='z'))
return 1;
else if ((y>='A')&&(y<'Z'))
return 1;
else if((y>='0')&&(y<='9'))
return 1;
return 0;
}
int pre(char x)
{
if(chckalphanum(x))
return 0;
if(x=='(')
return -1;
else if(x=='^')
return 3;
else if((x=='/')||(x=='*'))
return 2;
else
return 1;
}
int main ()
{
std::ios::sync_with_stdio(false);
string s, s1=")";
char q[404];
int qmax=0,t;
stack prs;
scanf("%d", &t);
while(t--)
{
cin>>s;
prs.initialize(s.length());
prs.push('(');
s=s+s1;
for(int i=0; i<s.length(); i++)
{
if(s[i]=='(')
prs.push('(');
else if(chckalphanum(s[i]))
{
q[qmax]=s[i];
qmax++;
}
else if(s[i]==')')
{
while(prs.front()!='(')
{
q[qmax]=prs.front();
qmax++;
prs.pop();
}
prs.pop();
}
else
{
while(pre(prs.front())>=pre(s[i]))
{
q[qmax]=prs.front();
qmax++;
prs.pop();
}
prs.push(s[i]);
}
}
for(int i=0; i<qmax; i++)
cout<<q[i];
cout<<"\n";
qmax=0;
}
return 0;
}
I just commented out the below line from your solution and it got accepted in codechef.
std::ios::sync_with_stdio(false);
I am not sure if you are aware of what the above line does but I will try to explain to the best of my knowledge. Better answers will definitely follow in due course from the community.
"With stdio synchronization turned off, iostream standard stream objects may operate independently of the standard C streams (although they are not required to), and mixing operations may result in unexpectedly interleaved characters."
Quoting from cppreference.
"Concurrent access to the same stream object may cause data races."
Since you have turned off the synchronization between stdio (C style I/O) and iostream (C++ style I/O)
and you continued using scanf and cin simultaneously interleaved, I suspect you got a runtime error.
For more research, please go through : http://www.cplusplus.com/reference/ios/ios_base/sync_with_stdio/
Hope it clarifies a bit, if not fully. Thanks!