Please help with my code(PC)? SWAP10HG problem ,December LunchTime

https://www.codechef.com/viewsolution/40796162

Try this test case:

1
2
01
10

Your code would return “Yes”, while the correct answer is “No”.

2 Likes

if((s[0]==‘0’&&p[0]==‘1’)||s[n-1]==‘1’&&p[n-1]==‘0’)
{
flag=false;
}
Thanks for the reply @kboitmanis but even after adding the above snippet ,it is getting partially submit

https://www.codechef.com/viewsolution/40851173

INFACT NOW I AM GETTING ONLY 30% SCORE!!
My algorithm is we will run a loop across both strings simultaneously
We would be declaring 4 variables
cnts1- for counting number of 1’s in string s
cnts0- for counting number of 0’s in string s
cntp1- for counting number of 1’s in string p
cntp0- for counting number of 0’s in string p
then if current character of both strings match,then we will only increment counters and move to next characters
if they do not match and current element is 0 in string s ,then we will check that numbers of 1’s in string s before should be equal to number of 0’s in string p before bcoz we need extra 1 before in string s and extra 0 in string p for swapping
At the end am checking if cnt 1’s and 0’s in bith strings are equal and flag should be true then “Yes” else “No”.

#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int t;
cin>>t;
vector arr;
while(t>0)
{
long long int n;
cin>>n;
string s,p;
cin>>s>>p;
for(long long int i=0;i<n;i++)
{
if(s[i]!=p[i] && s[i]==‘1’)
{

			arr.push_back(1);
		}
		else if(s[i]!=p[i] && s[i]=='0')
		{
			arr.pop_back();
		}
	}
	if(arr.empty())
	{
		cout<<"Yes"<<endl;
		
	}
	else{
		cout<<"No"<<endl;
	}
	arr.clear();
	t--;
}

return 0;

}

whats wrong in my code its returning sigabrt

It looks like the flaw is in this assumption:

“if they do not match and current element is 0 in string s ,then we will check that numbers of 1’s in string s before should be equal to number of 0’s in string p before bcoz we need extra 1 before in string s and extra 0 in string p for swapping”

To fix a misplaced ‘0’ in s, we need a misplaced ‘1’ somewhere before the current position. The existence of such a ‘1’ does not imply that the number of ones in s up to the current position equals the number of zeros in p. Here’s a counterexample.

1
3
010
001

All we need it to maintain the number of misplaced ones in s (not all ones) that haven’t been used for swaps yet.

I’m guessing it is the arr.pop_back() when arr is empty. Just try this test:

1
2
01
10

gives out yes

Ok, that’s the wrong answer. I would expect the code to crash as you are popping from an empty container. Apparently that’s not the case. From the docs (vector::pop_back - C++ Reference), if the container is empty, " it causes undefined behavior".

I suppose you just need to check if arr is empty before popping. If it is empty, the answer is “No”.