Solution to SNAKPROC....It is giving run time error.Can someone tell me the error

#include<stdio.h>
int main()
{
int R,L;
scanf ("%d",&R);
scanf ("%d",&L);
char A[L];
int i,j;

int h=0,t=0,k=0;
for(j=1;j<=R;j++)
{
scanf("%s",A);
for(i=0;i<L;i++)
{

if(A[i]==‘H’)
h++;
else if(A[i]==‘T’)
t++;
if(h<t)
{
k=1;
break;
}

}

if(h!=t)
k=1;

if(k==0)
printf(“Valid”);
else
printf(“Invalid”);
}
return 1;
}

Mate

First thing…

You need to add a loop for different test cases…

like

scanf("%d", &R);
while(R–>0){

scanf("%d", &L);

scanf("%s", A);

// your code here

}

Second thing…

The solution you are trying to make, will fail on test case

T…H…H…T

You need to check the order of Head and Tail as well, which you haven’t…

Give this problem a try,

If still unable to solve, Have a look at my


[1]...

Please Accept answer if you find this helpful.. :)


  [1]: https://www.codechef.com/viewsolution/15468395

h>t will check the order of h and t

(i) You have to take input of length of each procession and not a single time because the length is varying . Take the input of l in the for loop of R.

(ii) As the length of maximum procession is given to be 500 , you can declare a char array of length slightly greater than 500 say 505 .

(iii) You are checking for the number of heads and no. of tails should be equal . Instead you should check that each occurence of head is followed by an occurence of tail . If it satisfies this criteria then the answer would be valid .

(iv) Try to use c++ style of coding and take input using cin because scanf sometimes cause array .

If you have any other query , feel free to comment it .

#include<stdio.h>
int main()
{
int R,L;
scanf ("%d",&R);
int i,j;

int h=0,t=0,k=0;
for(j=1;j<=R;j++)
{
    scanf("%d",&L);
    char A[L+1];
scanf("%s",A);
for(i=0;i<L;i++)
{
 
if(A[i]=='H')
h++;
else if(A[i]=='T')
t++;
if(h<t)
{
k=1;
break;
}
 
}
 
if(h!=t)
k=1;
 
if(k==0)
printf("Valid");
else
printf("Invalid");
}
return 0;
}

Now what is wrong?

You can have a look at my code here

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

It basically explains what I am trying to say

You don’t have to count anything