Friday 2 June 2017

                         Snake and Mangooes
There are snakes and mangooes in the lines and a mangooes can eat at most one  neighbour snake at a time. so there is a election on mangooes vs snakes. If there are maximum mangooes than snakes than Mangooes wins the election vice versa.
The question is taken from codechef snackdown elimination B.
URL is https://www.codechef.com/SNCKPB17/problems/SNELECT
If there are same no. of mangooes and snakes then 'tie' will be answer.
There are T testcases in input and then a string containing 's' and 'm'.

In Output If snakes wins the 'Snakes' and if mangooes wins then 'mangooes' as output. for tie simply output as 'tie'.
Input                          OutPut
4                                mangooes
sm                                tie
ssm                               tie
sms                               tie
ssmmmssss                         snakes

So code for this prolem is in C is as following:
#include<stdio.h>
#include<string.h>
int main()
{
 int t,s=0,m=0;
 char str[10000];
 scanf("%d",&t);
 while(t--)
 {
  scanf("%s",str);
  s=0;
  m=0;
  int i,j,k;
  
  int l=strlen(str);
  char pre;
  
  int c=0,f=0;
  
  for(i=0;i<l;i++)
  {
   if(str[i]=='s')
   s++;
   else
   m++;
  }
  int counter=0;
  for(i=0;i<l;i++)
  {
   
if(str[i]=='s' && str[i+1]=='m' || str[i]=='m' && str[i+1]=='s'){
    s--;
    i++;
}
   
    
   }
   //("%d %d\n",c,f); 
  }
  
  if(s==m)
  printf("tie\n");
  else if(s<m)
  printf("mongooses\n");
  else
  printf("snakes\n");
  
  
  
  
  
 }
 
 
 
 return 0;
}











1 comment: