Page 2 of 8

at last solve 494

Posted: Tue Dec 23, 2003 9:00 am
by problem
hi everybody at last i solve this problem.i only change char t[30000].

[494] whats wrong?

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

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.

Posted: Mon Jan 26, 2004 2:08 am
by UFP2161
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.

Posted: Mon Jan 26, 2004 4:30 pm
by miko'mized
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.

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

Posted: Mon Jan 31, 2005 8:10 am
by ahmed hasan
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;
}

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

Posted: Sat Apr 02, 2005 6:06 pm
by murkho
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*/



Posted: Sat Apr 02, 2005 6:43 pm
by mf
Your code fails tests like these:

Code: Select all

abc@def
000a1b2c3d010
ch33se
The output should be:

Code: Select all

2
4
2

494 WA

Posted: Mon Apr 11, 2005 11:33 pm
by refa
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

Posted: Fri May 27, 2005 12:31 am
by jaracz
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!

WA 494

Posted: Mon Aug 22, 2005 8:23 am
by Salman
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;
}


Posted: Mon Aug 22, 2005 9:06 pm
by helloneo

Code: Select all

abcd-efg
try this..
my output is 2

494 WA...

Posted: Sat Oct 15, 2005 4:50 am
by thirddawn
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;
}

Posted: Sat Oct 15, 2005 9:27 am
by Timo
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.

494 - Compile Error

Posted: Mon Dec 26, 2005 12:34 am
by xintactox
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;
}

Posted: Mon Dec 26, 2005 12:48 am
by Jan
Remove the following function...

Code: Select all

cur_word.clear(); 
Hope it works.