PROBLEM LINK:Author: Praveen Dhinwa DIFFICULTY  Easy  Medium PROBLEM  Given a grid of size $2 * n$, where in some cells are marked SPECIAL. You have to put minimum line segments (walls) of any length you want such that each component made by the walls contains at most one cell which is marked SPECIAL. EXPLANATION  The solution is greedy and constructive in approach. There are $3$ cases possible  Case 1  Both the rows has at least one snake. Here we will firstly prove that putting an entire horizontal line segment is better than not putting one. PROOF  Use of NO horizontal line is only possible when there are no two snakes one below the other, so that means that each column has at most one snake, therefore the answer would be no. of snakes  1 in case we do not use horizontal line. But, if we use the horizontal line we can see that the first snake in row 0 and the first snake in row $1$ would be satisfied by only vertical line segment whereas earlier(with NO horizontal line segment) they required $2$. This statement is false for the corner case when the row $0$ has only $1$ snake and also row $1$ has only $1$ snake. So this can be considered as a corner case, but apart from this in all the other cases, we can see that we have actually saved using one vertical line by introducing the horizontal line. Meaning that this horizontal line compensates for the vertical line that we saved. So the introduction of this horizontal line segment is equivalent to us for the first $2$ snakes, but we can see that for the remaining snakes, we have a similar configuration to the previous case (when we did NOT use the horizontal line segment) BUT this time there is a horizontal line segment which is obviously more beneficial and ensures that the answer would either remain same or improve. After the entire horizontal line segment between row $0$ ($0$  based indexing) and row $1$ is made sound proof (add $1$ to the answer for this line segment), a greedy solution is implemented. Notice here that because both the rows have snakes therefore there needs to be a horizontal segment and since we are free to make it of more length without increasing the cost, therefore the horizontal segment is made from one side of the grid to the opposite. Now we can greedily position the vertical segments, we will move from left to right and count the no. of snakes in the current compartment of row $0$ and row $1$, we will place the vertical line segment whenever we observe that the count of snakes in either row becomes greater than $1$ and by placing the line segment, we now have opened a new compartment whose count of both the snakes would be $0$ and updated accordingly. Case 2  Only a single row has all the snakes, then the no. of vertical line segments required is no. of snakes  $1$. Case 3  No snake is there in the grid, then the answer is $0$. Below is the C++ implementation of the above mentioned logic 
Time Complexity  $O(N)$ AUTHOR'S AND TESTER'S SOLUTIONS:
This question is marked "community wiki".
asked 01 Jun '17, 18:15

The main logic was to find that for the test case(s) with 1010 0101 The answer would be 2 (one horizontal and one vertical). Basically, you need to find every alternating snake occupied houses subarray. Count the number of alternating snake occupied houses (say, x) and the number of walls in this subarray would be (x1)/2. (in this case, (41)/2 = 1, plus the horizontal one) You don't need to put a vertical wall between 2 alternating snake occupied houses if the horizontal wall already separates them. answered 01 Jun '17, 21:48

Got the answer at the last moment and i forgot to comment out a debugging print statement and missed out qualifying for the next round, ahhhh i hate this :( answered 01 Jun '17, 21:10

include<bits stdc++.h="">using namespace std; int main() { int a,b,a_i,b_i,a_t,t,co,h1,h2,l,fence,n,f,s,fence2; cin>>t; for(a_t=1;a_t<=t;a_t++) { string str1,str2; cin>>n>>str1>>str2; l=n; h1=0;h2=0;fence=0; for(a_i=0;a_i<l;a_i++) { if(str1.at(a_i)=='') h1++; if(str2.at(a_i)=='') h2++; }
} in which case it's not correct? what's wrong with it?? answered 01 Jun '17, 21:20
Your code raises an error empty character constant because in this condition " if(str1.at(a_i)=='') " your are comparing a character with empty character. If I assume that your counting '*' there then your code fails for input: 5 .*.*. *.*.* Your Output 4 Expected : 3 Your code also fails if I assume that char with you are comparing is'.' instead of '*' as mentioned I my previous assumption. Correct me if I'm wrong.
(01 Jun '17, 22:12)

in which case it is wrong?? can anyone explain??
answered 01 Jun '17, 21:27
Your code gives a compilation error because you have used scanf function for taking test case input without including stdio.h . Correct me if I'm wrong.
(02 Jun '17, 09:05)
@sai_rathan It did work without any compilation error.
(02 Jun '17, 10:14)
I tried running your code in both Codechef ide and codeblocks both raised a compilation error. Ok I will try to find an error test case for your code.
(02 Jun '17, 12:18)
Your Code is not working for this case: Input: 3 .** **. Your code's Output: 2 Expected Output: 3
(02 Jun '17, 12:26)

Please tell the input where it fails https://www.codechef.com/viewsolution/13960603 answered 01 Jun '17, 21:29

What is wrong with my this code, it is working fine for all tests which i could think. Please can someone tell where it is failing ??? https://www.codechef.com/viewsolution/13954302 answered 01 Jun '17, 21:34
I Dare you if you can tell where it will fail.
(01 Jun '17, 23:38)
2
The test cases already found where you were wrong so no point in dare XD. On a side note Request nicely and people will help :)
(01 Jun '17, 23:51)
thanks @vijju123 , I got the failure :( but late . I was tried more then 50 inputs but was not finding the failure .So i was very exited if someone reply as early as possible. Failure: .** **.
(02 Jun '17, 23:06)

what is wrong in my code
answered 01 Jun '17, 21:37

Yes, I tested for that exception at the beginning. @devilhector My code for only one line containing 's was to split on "" with a 1 modifier and subtract 2 from the resulting length. Through numerous copy and pasting, the 2's became 1's from an earlier submission. This is very frustrating. answered 01 Jun '17, 22:01

I have a much simpler solution than this official solution. :) answered 01 Jun '17, 22:21

Please tell what is wrong with this code @sai_rathan http://ideone.com/Ym6D1T answered 01 Jun '17, 22:28
Sorry I'm unable to find any case for which your code is not working.
(01 Jun '17, 23:20)
1
thank you for trying! @sai_rathan Can anyone provide a test case for which the code does not work?
(01 Jun '17, 23:37)
I think in the if condition your trying to access a[0][i+1] but when you are accessing last element it will try to access its next element which is not present. I think this is causing some trouble for larger value of 'n' though I'm not sure.
(01 Jun '17, 23:44)

@arvindpunk Thanks for the testcase :) ..I tried so hard but was not able to submit it !!! answered 01 Jun '17, 22:29

Can any one tell me where my solution is failing.. https://www.codechef.com/viewsolution/13957070 answered 01 Jun '17, 22:51

# whats wrong in This ... ?
answered 01 Jun '17, 22:59

I used a different approach, was getting a wrong answer but couldn't figure a test case for which this will fail. Consider the number of ''s in row 1 as stars1, and the number of stars in row2 as stars2. Also, for columns where both row 1 and row 2 have a '', count them in a variable common. So the approach is that if(common>0), add a fence(the horizontal fence). And then the number of vertical fences that will be added will be= (stars1 + stars2 common 1) . I cant figure out why will it fail.Can someone point out a test case. Here is the code: answered 01 Jun '17, 23:30

Can any one help me out where I went wrong https://www.codechef.com/viewsolution/13933535 I was really working from yesterday night and could not find where it is failing.So please and please help me out answered 02 Jun '17, 00:28
@nishith365 https://ideone.com/mDZwbq answer is 3!
(02 Jun '17, 03:21)

#include<bits stdc++.h=""> define S_ a[0][i]=='*' && a[1][i]=='.'define _S a[0][i]=='.' && a[1][i]=='*'define SS a[0][i]=='' && a[1][i]==''define __ a[0][i]=='.' && a[1][i]=='.'define SNAKE a[0][i+1]==''a[1][i+1]==''using namespace std; answered 02 Jun '17, 00:29

https://www.codechef.com/viewsolution/13933535 This is the link to mmy code.So please help me out where I went wrong answered 02 Jun '17, 00:29

Anyone who can tell me a test case ? Here is my code. Getting WA for no reason.
answered 02 Jun '17, 00:33

doing i in the last loop won't cause the loop to run infinitely? In c++ code given here? answered 02 Jun '17, 00:47

answered 02 Jun '17, 01:48

link text anyhelp on why this code not accepted will be highly appreciated!! answered 02 Jun '17, 01:55
@sauravsinha137 https://ideone.com/W9JMuW answer is 3!
(02 Jun '17, 03:08)

Can anyone please provide a test case where my code fails? Solution link : https://ideone.com/AVh0zv Thanks:) answered 02 Jun '17, 02:49

whats wrong with my solution
answered 02 Jun '17, 08:36

Somebody please correct me where i went wrong? Here's my logic: case 1: if the snakes are in row 1 or 2 or both but not vertically adjacent then i gave solution as total no. of snakes 1. case 2:if the snakes are in row 1 or 2 or both and least 2 snakes are vertically adjacent then i gave solution as 1 + total no. of snakes 1  no. of vertically adjacent pairs. case 3 : if there is 0 or 1 snake in total then i gave solution as 0. https://www.codechef.com/viewsolution/13947900 answered 02 Jun '17, 09:03
There is an error in your logic for case 1. Now consider a test case 5 .*.*. *.*.* Answer according to your logic will be 4 but correct answer is 3
(02 Jun '17, 09:09)
@sai_rathan my program i also giving output as 3. As the 1st and 2nd column have vertical adjacent snakes so this is my case 2.
(02 Jun '17, 09:45)

Can anyone pls explain why is "i" decremented in the Case 1(inside the if condition of for loop)?? answered 02 Jun '17, 09:28

please Suggest in which test case following code fails
answered 02 Jun '17, 10:19

can anyone tell me in what am i doing wrong ???? answered 02 Jun '17, 11:18

what's wrong with my code. answered 02 Jun '17, 14:46

What is wrong with my this code, it is working fine for all tests which i could think. Please can someone tell where it is failing ???https://www.codechef.com/viewsolution/13961222 answered 02 Jun '17, 16:34

Can anyone please tell me why I'm getting WA, here is the link to my code https://www.codechef.com/viewsolution/13962362 answered 02 Jun '17, 17:21

Can someone please tell what is wrong with my code. If possible give the test case where it fails. Here is the link to my code: https://www.codechef.com/viewsolution/13934000 answered 02 Jun '17, 17:51

please someone tell whats wrong with this program https://www.codechef.com/viewsolution/13944522 answered 02 Jun '17, 17:54

what is wrong with my code i couldn't understand why i got a wrong answer... import java.util.*; class snckdwn { public static void main(String[] args) { Scanner ob = new Scanner(System.in); int t = ob.nextInt(); while(t > 0) { int n = ob.nextInt(); char a[][] = new char[2][n]; for(int i=0;i<2;i++) { String s = ob.next(); for(int j=0;j<n;j++) { a[i][j] = s.charAt(j); } }
} answered 02 Jun '17, 23:56

Please tell where the input fails https://www.codechef.com/viewsolution/13971726 answered 03 Jun '17, 00:26

This is absolute solution and it help me a lot thanks . programming assignment help answered 03 Jun '17, 11:19

plz tell me test case for which my program produces wrong output thanks /\ answered 03 Jun '17, 13:58
https://discuss.codechef.com/questions/100275/compilationofedgecasesforsncoup Check here. if that doesnt help, comment further.
(03 Jun '17, 16:25)

Could anyone help me in figuring out the test cases where my solution is failing? i tried hard but could get any.!! Link to solution is https://www.codechef.com/viewsolution/14017280 . answered 03 Jun '17, 17:35

can't find what's wrong in this code include<iostream>using namespace std; int main(){ int t,n; cin>>t;
cout<<fences<<endl;
} answered 03 Jun '17, 19:31

Solution in Python https://www.codechef.com/viewsolution/14011703 answered 03 Jun '17, 22:59

@dpraveen, @kingofnumbers can we have the test cases now that test is over and we cannot even submit to check if our approach is correct.
link
This answer is marked "community wiki".
answered 20 Jun '17, 15:24

why my code is giving wrong answer .. plz help include<iostream>include<cstdlib>using namespace std; int main() { int t,n,i,counta,countb,j,k,ans; cin>>t; while(t) { cin>>n; char a=(char)malloc((n+1)sizeof(char)),b=(char)malloc(sizeof(char)(n+1)); cin>>a>>b; i=0;j=0;counta=0;countb=0;ans=0; for(k=0;k<n;k++) {="" if(a[k]="='*')" {="" counta++;="" i++;="" }="" if(b[k]="='*')" {="" countb++;="" j++;="" }="" if(i="">1&&j>1) { ans++; i=1;j=1; } else if(i>1 && j<2) { ans++; i=1; j=0; } else if(i<2 && j>1) { ans++; j=1;i=0; } } if(counta>0 && countb>0) ans++; cout<<ans<<endl; } return 0; } answered 15 Jul '17, 10:04

Users are requested to provide a submission link instead of pasting the entire code.