494 - Kindergarten Counting Game

All about problems in Volume 4. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

problem
New poster
Posts: 27
Joined: Mon Nov 10, 2003 12:40 am

at last solve 494

Post by problem » Tue Dec 23, 2003 9:00 am

hi everybody at last i solve this problem.i only change char t[30000].

miko'mized
New poster
Posts: 8
Joined: Thu Dec 04, 2003 4:56 pm

[494] whats wrong?

Post by miko'mized » Sun Jan 25, 2004 10:03 pm

Code: Select all

#include <iostream>

using namespace std;

char buff;
int words;
bool is;

int main()  {
    while(!cin.eof())   {
        buff = cin.get();
        while(buff!='\n' && buff!=EOF)   {
            if( ( buff >= 'a' && buff <= 'z') || (buff >= 'A' && buff <= 'Z') )    {
                if(!is)  {
                    is = true;
                    words++;
                }
            }
            else is = false;
            buff = cin.get();
        }
        cout << words << endl;
        words = 0;
        is = false;
    }
    return 0;
}
im sure that works, but dont know why it gets WA.

User avatar
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 » Mon Jan 26, 2004 2:08 am

The last line of the input will look something like:
word1 word2(\n)
(EOF)
Currently, your program will still parse the EOF, and thus output an extraneous "0".

Another possible problem is that you don't initialize the "words" variable to anything. In theory, it could be just some junk variable, though C++ normally sets everything to 0 for you, it's always better to initialize your own variables and not let the compiler do it.

miko'mized
New poster
Posts: 8
Joined: Thu Dec 04, 2003 4:56 pm

Post by miko'mized » Mon Jan 26, 2004 4:30 pm

thx. i considered that but i thought that input looks like this:

word word word('\n')
word word(EOF)

now i've got AC. thx again.

ahmed hasan
New poster
Posts: 9
Joined: Fri Jan 21, 2005 5:09 pm

494-Kindergarten Counting Game: got WA for such an easy one!

Post by ahmed hasan » Mon Jan 31, 2005 8:10 am

For using getlint(cin, ) it does not run properly in VC++
I used mingw.
I define a word as a consecutive sequence of charecter if its each charecter is a letter (upper/lower). Thats what i can do in computer.
I think the problem is there how I define words.
How can I know that a consecutive sequence of cheraters is an word?

Code: Select all

#include<iostream>
#include<string>
#include<cstring>

using std::string;
using std::cout;
using std::endl;
using std::cin;
using std::getline;

string ip;
int p1,p2;
int count;

void word_check(string word)
{
    char lc=word[word.length()-1];      //lc=last character.
    if(word.length()!=1)
    if(lc=='.'||lc=='?'||lc=='!')
        word.erase(word.length()-1,1);  //I just ignore the last character if its length is >1
    
    int f=1;
    for(int c1=0;c1<word.length();c1++)
    if( !((word[c1]>='A'&&word[c1]<='Z') || (word[c1]>='a'&&word[c1]<='z')) )
    {f=0;break;}                   //I defined word which has all its character between A->z or a->z
    if(f)count++;
}



int main()
{
    while(getline(cin,ip))
    {
        count=0;
        p1=p2=0;
        while(1)
        {
            while(p1<ip.length()&&ip[p1]==' ')p1++;p2=p1;
            if(p1>=ip.length())break;
            while(p2<ip.length()&&ip[p2]!=' ')p2++;
            word_check(ip.substr(p1,p2-p1));
            p1=p2;
        }
        cout<<count<<endl;
    }
    return 0;
}

murkho
New poster
Posts: 33
Joined: Mon Mar 28, 2005 6:41 pm

WA (494) please help me to get the reason......

Post by murkho » Sat Apr 02, 2005 6:06 pm

I have considered tab also. But yet it gets WA. Please help me finding out the reason for WA>>>

Code: Select all

					/*BEGIN_OF_THE_SOURCE_CODE*/

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define N 10000


int main()
{
char str[N];
int i,len,word = 0,j;
while(gets(str) != NULL)
	{
	 word = 0;
	 len = strlen(str);

	 for(i = 0;i<len;i++)
		{
		 if( isalpha(str[i]) )
			{
			 for(j = i+1;j<len;j++)
				{
				 if(str[j] == ' '  || str[j] =='\t')
					{
					 word++;
					 break;
					}
			 
				}
			 if(j == len )
				 word++;
			 i = j;
		 
		 
			}
	 
	 
		}


	printf("%d\n",word);


	}



return 0;
}

				/*END_OF_THE_SOURCE_CODE*/



mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf » Sat Apr 02, 2005 6:43 pm

Your code fails tests like these:

Code: Select all

abc@def
000a1b2c3d010
ch33se
The output should be:

Code: Select all

2
4
2

refa
New poster
Posts: 8
Joined: Thu Mar 17, 2005 9:00 pm
Contact:

494 WA

Post by refa » Mon Apr 11, 2005 11:33 pm

Dear ACM members,

Could you please take a look at my code and tell me what's wrong because I am getting WA all the time and I read all the posts about it on the forum and it works properly with the example inputs. Thank you

Code: Select all

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main()
{
  string line1="";
  long long int duzina=0;
  long long int count = 0;
  while(getline(cin, line1))
  {
    duzina= 0;
    count = 0;

    line1= " " + line1 + " ";
     for(long long int i=0; i<line1.length(); i++)
     {
      if(ispunct(line1[i])!=0 || isspace(line1[i])!=0 || isdigit(line1[i])!=0 || isblank(line1[i])!=0 || iscntrl(line1[i])!=0)
      {
       string word = "";
       word = line1.substr(duzina, i-duzina);
       if(isalpha(word[0]))
       {
           count++;
        }

       duzina=i+1;
      }
     }
    cout << count <<endl;
  };
  return 0;
};
I hope somebody will be able to help me out. Thx :)

Best regards,
Refik

User avatar
jaracz
Learning poster
Posts: 79
Joined: Sun Sep 05, 2004 3:54 pm
Location: Poland

Post by jaracz » Fri May 27, 2005 12:31 am

consider this algo

read string
if(isalpha(string[0]))count++;

and than in some loop until the one before last element of string(excluding '\0')

if( (isspace(string) && isalpha(string[i+1])) || (!isspace(string) && !isalpha(string)) && isalpha(string[i+1]) ) count++;

and you dont have to use long long( i used just int )
Hope it helps!
keep it real!

Salman
New poster
Posts: 25
Joined: Thu Jun 26, 2003 9:45 am

WA 494

Post by Salman » Mon Aug 22, 2005 8:23 am

need some Test case

Code: Select all

/*
Name: kindergarden countiog  
Number: 494
Type :  adhoc
Process : ON
Author :Salman Zaman
Email : zamansalman@gmail.com
Date : 21/08/05 23:27




*/



#include<stdio.h>
//#include<string.h>



//#include<conio.h>



int main(){

       char ch;
       int flag,count;
       
  //     freopen("494.txt","r",stdin);
        
        flag=0;
        count=0;
        while(scanf("%c",&ch)!=EOF){
             

              if((ch>='A' && ch<='Z' )||(ch>='a' && ch<='z')){
                     flag++;   
              }    
              
              else if(ch=='\n'){
                      if(flag>0){
                          count++;
                      }    
                      printf("%d\n",count); 
                    flag=0;
                    count=0;
              }          
              else if(ch==' '){
                  if(flag>0){
                      count++;
                  }    
                  flag=0;
              }    
              
                 
                                                    
                           
   }    
     
   //getch();
   return 0;
}


helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

Post by helloneo » Mon Aug 22, 2005 9:06 pm

Code: Select all

abcd-efg
try this..
my output is 2

thirddawn
New poster
Posts: 9
Joined: Sat Oct 15, 2005 4:41 am

494 WA...

Post by thirddawn » Sat Oct 15, 2005 4:50 am

It's ok with sample input...however it can't work when I submit :(
---
#include <stdio.h>

int main() {
int space=0;
char a;
while (scanf("%c",&a)==1){
if (a==' '){
scanf("%c",&a);
if (a!='.')
space = space + 1;
}
if (a=='\n'){
printf ("%d\n",space+1);
space = 0;
}
}
return 0;
}

Timo
Learning poster
Posts: 70
Joined: Tue Oct 11, 2005 2:44 am
Location: Indonesia

Post by Timo » Sat Oct 15, 2005 9:27 am

Try This Case :

input :
7 is a prime number.
hello world.
timo12345timo

output :
4
2
2

A ``word'' is defined as a consecutive sequence of letters (upper and/or lower case). Only letters -> 'a'-'z' and 'A'-'Z'.

I hope you will get AC.

xintactox
New poster
Posts: 14
Joined: Thu Dec 01, 2005 3:17 pm
Location: Brazil

494 - Compile Error

Post by xintactox » Mon Dec 26, 2005 12:34 am

Hi everybody!
I don't know why it's getting CE, it compiles normally with g++ at cygwin...

Please take a look!

Thanks, my code follows :)

Code: Select all

#include <string>
#include <iostream>
#include <cctype>
//#include <iomanip>

using namespace std;

int main()
{
	string linha, cur_word;
	int count, char_count;	
	while(getline(cin, linha))
	{
		count = 0;
		char_count = 0;
		cur_word.clear();				
		while(char_count < linha.size())
		{
			while(linha[char_count] != ' ' && char_count < linha.size())
			{				
				cur_word += linha[char_count];
				char_count++;
			}			
			for(int i = 0; i < cur_word.size(); i++)
				if(isalpha(cur_word[i]))
				{					
					count++;
					break;
				}
			cur_word.clear();	
			char_count++;
		}
		cout<<count<<endl;		
	}
	return 0;
}

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Mon Dec 26, 2005 12:48 am

Remove the following function...

Code: Select all

cur_word.clear(); 
Hope it works.
Ami ekhono shopno dekhi...
HomePage

Post Reply

Return to “Volume 4 (400-499)”