what is problem in my code for this problem?

link:-(https://www.codechef.com/problems/SNELECT/)

my answer:-

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
	char a[99];
	int i,t;
	cin>>t;
	for(i=0;i<=t;i++)
	{  int s=0,m=0;
	  	gets(a);
	  	int sz=strlen(a);
	  	if(a[0]=='s')
         s++;
         
	  	for(int j=1;j<sz;j++)
	  	{
		 if(a[j]=='s')
		 {
		 	s++;
		 }
		 if(a[j]=='m')
		 {
		 	m++;
		 	if((a[j-1]=='s')||(a[j+1]=='s'))
            {
            	s--;
			}
		 }
		  }
		 
		  if(s>m)
		 {
		   cout<<"snakes\n";}
		  if((s==m)&&(s>0))
		  {
		  cout<<"tie\n";}
		  if(m>s)
		  {
		  cout<<"mongooses\n";}
		  
	  }
	  return 0;
}

first of in the first for loop it should for(int i=0;i<t;i++) instead of for(int i=0;i<=t;i++)

In your algorithm u might consume same snake twice(you are not taking care of it).

for example in the case -“ssssmmsm” the result will be tie , but your answer is mongoose… (due to the last snake is eaten twice…).
so i advise you to make an array and store which snakes are eaten by now, so you don’t eat them twice…

also in the line if((a[j-1]==‘s’)||(a[j+1]==‘s’)) might give some(probably) garbage value as j+1 may exceed the length of the string take care of it…

u are not considering the case when the first position is taken by mongoose failing case - “ms”…

Seems like u are an beginner and don’t worry it’s a common thing.
Happy coding :slight_smile:

feel free to ask any further doubts bro…

//