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

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

Still not working...

Post by xintactox » Mon Dec 26, 2005 3:00 am

I changed the line

cur_word.clear();

by

cur_word = ''";

It compiled and worked fine in my pc under g++ and cygwin, but i'm still getting CE... any suggestions?!

Thanks!

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

Post by Jan » Mon Dec 26, 2005 8:43 am

You used 2..

Code: Select all

cur_word.clear();
I believe you removed only one of them. Remove both of them. I m sure that you will not get CE.

Hope it helps.
Ami ekhono shopno dekhi...
HomePage

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

Still CE... :(

Post by xintactox » Tue Dec 27, 2005 1:52 am

Hi!

I posted the following code, but I'm still getting CE...
I changed the lines you told me but.. it's not getting accepted... what else an be wrong?

Thank you

Code: Select all

#include <string>
#include <iostream>
#include <ctype.h>
//#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 = "";         
      while(char_count < linha.size())
      {
         while(isalpha(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_world = "";   
         char_count++;
      }
      cout<<count<<endl;      
   }
   return 0;
}

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 » Tue Dec 27, 2005 4:36 am

Very careless typo in your code.

You declared the following variables in your code:

Code: Select all

   string linha, cur_word; 
but further down in your code I see this:

Code: Select all

cur_world = "";

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

How could I be so STUPID?!

Post by xintactox » Tue Dec 27, 2005 4:56 am

You we're right....

I was too stupid, im sorry for bothering u all!

Ang I Got AC! Thanks!

abhi
Learning poster
Posts: 94
Joined: Fri Nov 25, 2005 7:29 pm

494

Post by abhi » Mon Jan 09, 2006 3:18 pm

this is my code i get WA .i think it should work ......where am i wrong??

Code: Select all

 CODE DELETED             
Last edited by abhi on Mon Jan 09, 2006 4:54 pm, edited 1 time in total.

Schutzstaffel
New poster
Posts: 37
Joined: Fri Apr 30, 2004 6:52 pm
Location: Portugal

Post by Schutzstaffel » Mon Jan 09, 2006 3:48 pm

At first glance your program doesn't give the correct output if the line starts with a character that's is not in A-Z and a-z like ponctuation and whitespace.
For example:

Code: Select all

..  This is a valid input!
You program would output 6 wich is wrong.

Also you program didn't output the correct values for the example given because of this condition:

Code: Select all

while (str[i]!='\0')
gets function fills the array only until the last character in str position, the rest of the array has garbage for so saying so you should have while (i < strlen(str)) instead.

Good luck, you're near the correct answer :)
Image

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 » Mon Jan 09, 2006 4:31 pm

Did you even test your code with the sample input. Your code gives wrong answer with the sample input.

Change this:

Code: Select all

while(!isalpha(str[i])) i++;

to this:

Code: Select all

while(str[i] != '\0' && !isalpha(str[i])) i++;
This will make your code pass the sample input but your code will still give WA if the sample input contains one word like this:

Input:
a

Correct Output:
1

But your code gives:
0

abhi
Learning poster
Posts: 94
Joined: Fri Nov 25, 2005 7:29 pm

Post by abhi » Mon Jan 09, 2006 4:43 pm

well i made the changes which Schutzstaffel suggested and got AC anyways . but still the time is 0.002 s. i dont know y it is so slow ????
:o

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

Post by Jan » Mon Jan 09, 2006 5:13 pm

I have not read your code. Try submitting 2 or 3 times. I think you will get it Accepted in 0.000 seconds. Otherwise try to avoid printf. Then your runtime will decrease.
Ami ekhono shopno dekhi...
HomePage

abhi
Learning poster
Posts: 94
Joined: Fri Nov 25, 2005 7:29 pm

Post by abhi » Mon Jan 09, 2006 7:28 pm

well anyways ..... i checked on the web and according to C standards gets must terminate the string with a '\0' character. so if i use

Code: Select all

while(str[i]!='\0')
then it should have worked properly i think ????

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:

Post by mamun » Mon Jan 09, 2006 8:27 pm

abhi wrote:well anyways ..... i checked on the web and according to C standards gets must terminate the string with a '\0' character. so if i use

Code: Select all

while(str[i]!='\0')
then it should have worked properly i think ????
It should, depending on the value of i (I didn't get a chance to read your code :-? ).

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 » Tue Jan 10, 2006 5:36 am

You had two while loops in your original code. The first one did this:

while(str != '\0')

This is fine of course.

It is the inner while loop that can cause problems:

while(!isalpha(str)) i++;

If str is '\0' in this loop your code will still increment i, since the null character is not an alphabet character according to the isalpha function.

fixit
New poster
Posts: 5
Joined: Tue Jun 06, 2006 7:39 pm

494 - WA ?

Post by fixit » Tue Jun 06, 2006 8:36 pm

var a:array [0..100] of char;count,i:integer;
function ileslow(s:string):integer;
var k:integer;var slowo_start:boolean;counter:longint;
begin
slowo_start:=false;
counter:=0;
for k:=1 to length(s) do
if (s[k] in ['a'..'z']) or (s[k] in ['A'..'Z']) then
begin
if not slowo_start then
begin
inc(counter); slowo_start:=true;
end;
end else
slowo_start:=false;


ileslow:=counter;

end;

begin
count:=0;
while true do
begin
readln(a[count]);inc(count);
if (a[count-1][length(a[count-1])]=#26) or (a[count-1][1]=#26) then break;
end;
for i:=0 to count-1 do
writeln(ileslow(a));
end.

I don't understand what is wrong :(

farzane
New poster
Posts: 26
Joined: Thu Jun 15, 2006 9:26 am

reply to 494(fixit)

Post by farzane » Thu Jun 15, 2006 11:18 am

Hi dear fixit

First:you didn't say what kind of problem you had with your code.After submition what kind of error you recived.

Your algorithm for this problem is true for sure.I submitted a solution for this problem by this algoritm and I recived an ACCEPTED reply.
Here is my code in c++:
-----------------------------------------------
#include<iostream.h>

int counter(char str[]){
int count=0;
bool inword=false;
int i=0;
for(;str;i++)
if((str>='a' && str<='z')||(str>='A' && str<='Z')){
if(inword==false){
inword=true;
count++;
}

}
else inword=false;
return count;
}



void main(){
char str[100000];
while(cin.getline(str,100000))
cout<<counter(str)<<endl;
}

-------------------------------------------------------------------------
Try to correct your own code.
I saw some mistakes in your code:

1)You recived all of inputs and saved them and then solved each of them.
while you can read and solve them one by one why you did so?

2)I think the size of array a(100) is too small.

3)You define array a so: a:array [0..100] of char;
but you use it in a different model: a[count-1][length(a[count-1])]
(I mean the number of brakets[])


All of the above are my opinions and maybe they are wrong,but I hope I was helpful.

And at the end I excuse if I couldn't express myself good.English isn't my
tongue languge and I'm not good at it.

with the best regards

Post Reply

Return to “Volume 4 (400-499)”