banaji
June 2, 2017, 12:29am
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;
int main(){
inputSection
//-----------------------------------------------
int firstIndex = 0;
int lastIndex=0;
for(int i=0;i<n;i++)
{
if(a[0][i]==’’ || a[1][i]==’ ’)
{
firstIndex = i;
break;
}
}
for(int i=n-1;i>=firstIndex;i–)
{
if(a[0][i]==’’ || a[1][i]==’ ’)
{
lastIndex = i;
break;
}
}
bool hz = false;
int vt =0;
if(firstIndex==lastIndex && a[0][firstIndex]==’’&&a[1][lastIndex]==’ ’)
{
hz=true;
}
else if(firstIndex==lastIndex)
{
hz = false;
vt=0;
}
for(int i = firstIndex;i<lastIndex;)
{
if(S_)
{
if(!SNAKE)
{i++;continue;}
i=i+1;
if(S_)
vt++;
else if(SS){hz=true;
vt++;}
else if(_S){
hz= true;
}
}
else if(_S){
if(!SNAKE)
{
i++;
continue;
}
i= i+1;
if(_S)
{
vt++;
}
else if(S_)
{
hz=true;
}
else if(SS){
hz= true;
vt++;
}
}
else if( SS){
if(!SNAKE)
{
i++;
continue;
}
i= i+1;
if(_S)
{
vt++;
hz=true;
}
else if(S_)
{
hz=true;
vt++;
}
else if(SS){
hz= true;
vt++;
}
else{
hz=true;
}
}
else // increment i
{i++;}
}
if(hz)
cout<<vt+1<<endl;
else
cout<<vt<<endl;
}
return 0;
}
**Can AnyOne help me in getting error in this code ** Here SOME Macros are used _–>Dot ans S–>Snake I m checking pair wise snake and next snake and then applying the opartion on vertical line and horizontal line i used a boolean for Hz line if it exists then add 1 to vertical lines…
https://www.codechef.com/viewsolution/13933535
This is the link to mmy code.So please help me out where I went wrong
ak_n
June 2, 2017, 12:31am
23
If anyone want’s to check out a correct
[1]
It's quite simple
[1]: https://www.codechef.com/viewsolution/13932694
Anyone who can tell me a test case ? Here is my code. Getting WA for no reason.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,t;
char a[200010];
char b[200010];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
scanf(" %s",a);
scanf(" %s",b);
int cnt=0;
int upper=0;
int lower=0;
for(int i=0;i<n;i++)
{
if(a[i]=='*')
upper++;
if(b[i]=='*')
lower++;
if(upper>=1 && lower>=1)
{
cnt=1;
break;
}
}
upper=lower=0;
for(int i=0;i<n;i++)
{
if(a[i]=='*' && b[i]=='*')
cnt++;
else if(a[i]=='*')
{
upper++;
cnt++;
}
else if(b[i]=='*')
{
lower++;
cnt++;
}
if(upper==1 && lower ==1)
{
cnt--;
lower=upper=0;
}
if(lower>1 || upper>1)
{
if(a[i]=='*' && b[i]=='.')
{
upper=1;
lower=0;
}
else if(b[i]=='*' && a[i]=='.')
{
lower=1;
upper=0;
}
else
upper=lower=0;
}
//printf("at %d count= %d\n",i,cnt);
}
if(cnt)
cnt--;
printf("%d\n",cnt);
}
return 0;
}
doing i-- in the last loop won’t cause the loop to run infinitely? In c++ code given here?
//ANYHELP why this code is not accepted?
#include
#include
#include
#include
#include
using namespace std;
int main(){
int t;
cin>>t;
for(int r=0;r<t;r++){
int q;
cin>>q;
char s[2][q];
char d;
for(int i=0;i<2;i++){
for(int j=0;j<q;j++)
cin>>s[i][j];
}
int c=0;
for(int i=0;i<2;i++){
for(int j=0;j<q;j++)
if(s[i][j]=='*')c++;
}
int count=0;
int s1=0;
int s2=0;
d='a';
for(int k=0;k<q;k++){
if(s[0][k]=='*' || s[1][k]=='*') count++;
if(s[0][k]=='*') s2=1;
if(s[1][k]=='*') s1=1;
if(s[0][k]=='*' && s[1][k]=='*') d='c';
}
int hor=s1+s2;
if(d=='c'){
if(count-1 == 1 && hor==2 && c>2){cout<<2<<endl; }
if(count-1 == 0 && hor==2 && c==2){cout<<1<<endl; }
if(count-1 == 1 && hor==2 && c==2){cout<<1<<endl; }
if(count-1 > 1 && hor==2 ){cout<<count+hor-2<<endl; }
if(count-1 >= 1 && hor!=2 ){cout<<count-1<<endl; }
if(count-1 ==0 && hor!=2 ){cout<<0<<endl; }
if(count-1 ==0 && hor!=2 && c==2){cout<<1<<endl; }
}
else if(d!=c){
if(count>=1)cout<<count-1<<endl;
else cout<<0<<endl;}
}
return 0;
}
Blockquote
link text
anyhelp on why this code not accepted will be highly appreciated!!
Can anyone please provide a test case where my code fails?
Solution link : AVh0zv - Online C++0x Compiler & Debugging Tool - Ideone.com
Thanks:)
whats wrong with my solution-
#include <iostream>
#include <string>
using namespace std;
int main()
{
int t;
cin>>t;
while (t--)
{
int n,i;
cin>>n;
char a[2][n];
for (i=0;i<n;i++)
{
cin>>a[0][i];
}
for (i=0;i<n;i++)
{
cin>>a[1][i];
}
string s="";
for (i=0;i<n;i++)
{
if (a[0][i]=='*'&&a[1][i]=='*')
{
s+="h";
}
else if (a[0][i]=='*'&&a[1][i]=='.')
{
s+="u";
}
else if (a[0][i]=='.'&&a[1][i]=='*')
{
s+="d";
}
else if (a[0][i]=='.'&&a[1][i]=='.')
{
s+="n";
}
}
int fences=0;
int x=n-1;
for (i=0;i<(n);i++)
{
if (s[i]=='h'||s[i]=='u'||s[i]=='d')
{
x=i;
break;
}
}
for (i=x+1;i<n;i++)
{
if (s[i]=='h'||s[i]=='u'||s[i]=='d')
{
fences++;
}
}
int hexist=0;
for (i=0;i<n;i++)
{
if (s[i]=='h')
{
hexist=1;
break;
}
}
if (hexist==1)
fences++;
cout<<fences<<endl;
}
return 0;
}
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
Can anyone pls explain why is “i” decremented in the Case 1(inside the if condition of for loop)??
s_17
June 2, 2017, 10:19am
32
please Suggest in which test case following code fails
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
string arr[2];
cin>>arr[0]>>arr[1];
ll ver=0,hori=0;
ll count=0;
for(ll i=0;i<n;i++)
{
if(arr[0][i]==arr[1][i] && arr[0][i]=='*')
hori=1;
if(arr[0][i]=='*' || arr[1][i]=='*')
count++;
}
ll val=1,up=0,low=0;
for (int i = 0; i < n; ++i)
{
if(arr[0][i]=='*')
up++;
if (arr[1][i]=='*')
low++;
if(up>1)
{
up=1;low=0;val++;
}
if(low>1)
{
low=1;up=0;val++;
}
}
ll k=min(count-1+hori,val);
if(k>0)
cout<<k<<"\n";
else
cout << "0\n";
}
}
#include
using namespace std;
int main()
{
int testcases ;
cin >> testcases ;
for (int t = 0 ; t < testcases ; t++){
int n ;
cin >> n ;
char a[2][n];
for (int i = 0 ; i < 2 ; i++){
for (int j = 0 ; j < n ; j++){
cin >> a[i][j];
}
}
int up_snake = 0 ; // if there is snake up
int down_snake = 0 ; // if there is snake down
int fence = 0 ; // answer
int start_up = 0 ; // first snake up
int start_down = 0 ; // first snake down
for (int i = 0 ; i < n ; i++){
if (a[0][i] == '*'){
up_snake = 1 ;
start_up = i ;
break ;
}
}
for (int i = 0 ; i < n ; i++){
if (a[1][i] == '*'){
down_snake = 1 ;
start_down = i ;
break ;
}
}
if (up_snake == 1 && down_snake == 1){
fence++ ; // for between fence
}
// cout << "basic fence " << fence << endl ;
int start_index = start_up ; // starting index
int count_up = 1 ;
int count_down = 0 ;
if (start_up > start_down){
count_down = 1 ;
count_up = 0 ;
start_index = start_down ;
}
if (start_up == start_down){
count_up = 1 ;
count_down = 1 ;
}
if (start_index == n-1){
cout << 0 << endl ;
}
else {
for (int i = start_index+1 ; i < n ; i++){
if (a[0][i] == '*' && a[1][i] == '*'){
fence ++ ;
count_down = 1 ;
count_up = 1 ;
}
else if (a[0][i] == '*' && count_up == 1){
fence ++ ;
count_down = 0 ;
}
else if (a[0][i] == '*' && count_up == 0){
count_up ++ ;
}
else if (a[1][i] == '*' && count_down == 1){
fence ++ ;
// cout << " fence down at " <<i << endl ;
count_up = 0 ;
}
else if (a[1][i] == '*' && count_down == 0){
count_down ++ ;
}
}
cout << fence << endl ;
}
}
return 0;
}
can anyone tell me in what am i doing wrong ???
#include<stdio.h>
#include<string.h>
int main()
{
int t,n,i,first,second,a,b,count;
char arr[2][100004];
scanf("%d",&t);
while(t–)
{
first=0;
second=0;
count=0;
a=0;b=0;
scanf("%d",&n);
scanf("%s",&arr[0]);
scanf("%s",&arr[1]);
for(i=0;i<n;i++)
{
if(arr[0][i]==’’)
first++;
if(arr[1][i]==’ ’)
second++;
}
if(first+second<2)
{
printf(“0\n”);
continue;
}
if(first==1&&second==1)
{
printf(“1\n”);
continue;
}
if(first==0)
{
printf("%d\n",second-1);
continue;
}
if(second==0)
{
printf("%d\n",first-1);
continue;
}
for(i=0;i<n;i++)
{
if(arr[0][i]==’’)
a++;
if(arr[1][i]==’ ’)
b++;
if(a==2)
{
count++;
a–;
if(b>0)
b–;
}
if(b==2)
{
count++;
b–;
if(a>0)
a–;
}
}
printf("%d\n",count+1);
}
}
this is my code
please tell where it is failing
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 ???CodeChef: Practical coding for everyone
Can anyone please tell me why I’m getting WA, here is the link to my code CodeChef: Practical coding for everyone
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
please someone tell whats wrong with this program
https://www.codechef.com/viewsolution/13944522
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);
}
}
boolean upper = false;
boolean lower = false;
boolean hl = false;
boolean left = false;
boolean leftdiag = false;
boolean rightdiag = false;
int count = 0;
for(int i=0;i<n;i++)
{
char te, te1, be, be1;
te = a[0][i];
be = a[1][i];
if(hl==false)
{
if(te=='*')
upper = true;
if(be=='*')
lower = true;
}
if(upper==true&&lower==true)
{
upper = lower = false;
hl = true;
count++;
}
if(te=='*'&&be=='.')
{
if(rightdiag==true)
{
rightdiag = false;
continue;
}
if(leftdiag==true)
{
count++;
continue;
}
leftdiag = true;
if(left==true)
{
count++;
continue;
}
left = true;
}
else if(te=='.'&&be=='*')
{
if(leftdiag==true)
{
leftdiag = false;
continue;
}
if(rightdiag==true)
{
count++;
continue;
}
rightdiag = true;
if(left==true)
{
count++;
continue;
}
left = true;
}
else if(te=='*'&&be=='*')
{
if(left==false)
left = true;
else
{
count++;
left = false;
}
}
else
continue;
}
System.out.println(count);
}
}
}