Holes in a text(easy problem)

The following code is running successfully in the compiler borland c++ and I am getting the correct output also. Then why is the result showing wrong output at codechef? Please I would be very grateful if someone can let me know the reason.(I have already spent 2 hrs on this question.)

#include<iostream.h>
   #include<stdio.h>
   #include<string.h>
   #include<conio.h>

   int main()
   {
      int t,i,j,l,*count;
      char string[100];
      cin>>t;
      count=new int[t];
      for(i=0;i<t;++i)
      {
      	count[i]=0;
      	gets(string);
         l=strlen(string);
      	for(j=0;j<l;++j)
         {
         	if(string[j]=='A')
         		count[i]++;
         	else if(string[j]=='D')
         		count[i]++;
         	else if(string[j]=='O')
         		count[i]++;
         	else if(string[j]=='P')
         		count[i]++;
         	else if(string[j]=='Q')
         		count[i]++;
         	else if(string[j]=='R')
         		count[i]++;
         	else if(string[j]=='B')
         		count[i]+=2;
         }
      }
      for(i=0;i<t;++i)
      	cout<<count[i]<<'\n';

      getch();
   	return(0);

There are few points you need to take note down.

  1. STOP USING TURBO C++/BORLAND for coding. The compiler used by most online judges is gcc, and thus most of the header files which are used in Turbo C++ are deprecated.

  2. So, please see this video to learn how to submit codes on online programming websites. Link

Hope this helps.

1 Like

dont use “gets”,use scanf or cin instead.

and also never use conio

Yes I agree with the two of you. I made the required changes but still it is giving “wrong answer”. This is the modified code:

#include<iostream>

#include<stdio.h>
#include<string.h>
using namespace std;

int main()
{
int t,i,j,l,*count;
char string[100];
cin>>t;
if(t<=40)
{
count=new int[t];
for(i=0;i<t;++i)
{
count[i]=0;
gets(string);
l=strlen(string);
for(j=0;j<l;++j)
{
if(string[j]==‘A’)
count[i]++;
else if(string[j]==‘D’)
count[i]++;
else if(string[j]==‘O’)
count[i]++;
else if(string[j]==‘P’)
count[i]++;
else if(string[j]==‘Q’)
count[i]++;
else if(string[j]==‘R’)
count[i]++;
else if(string[j]==‘B’)
count[i]+=2;
}
}
for(i=0;i<t;++i)
cout<<count[i]<<’\n’;
}
return(0);

}

you are getting wrong answer because you are not using ‘gets’ properly.

here is your AC code with gets

http://www.codechef.com/viewsolution/4561221

Thanks a lot mate! So the statements “gets(string)” and “cin>>string” work equally well?! I didn’t know this.

remove conio.h header file and all related function than it work properly i hope your problem will be solved

Yes but also spot the difference that you have to use ‘gets’ twice, one before ‘for’ loop.

1 Like

Yes I see now…but why is that so?

suppose you are only using one ‘gets’ inside the loop, and when you enter no. of testcase ,it will also consider it as string. so to prevent it just use one more gets before loop.

Remove conio.h header file and getch() and you will also use

 #include< bits/stdc++.h> 
1 Like

Yes do as is said above try using proper header files and you will be done with this problem :slight_smile: .

Happy Coding

1 Like

You can use #include< bits/stdc++.h > to include all header files. All the best!!!

1 Like