Page 4 of 8
494 WA till I changed input from string to char array.. Why?
Posted: Mon Jul 03, 2006 11:40 pm
by wktang
Hey guys,
I've tried submitting this code so many times, until I changed the input from string to char array, and it got AC.
Here is my code which got WA (it's exactly the same output of the code I got AC, the only difference is input by char array instead of string)...
Code: Select all
#include <iostream>
#include <string>
using namespace std;
int wordCounter(string str)
{
bool inWord = false; // flag to check whether the current position is in a word (contains alphabets)
int wordCount = 0;
for (int i = 0; str[i] != '\0' ; i++)
{
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
{
if (!inWord)
{
inWord = true;
wordCount++;
}
}
else
{
inWord = false;
}
}
return wordCount;
}
int main()
{
string str;
while(getline(cin, str))
{
cout << wordCounter(str) << "\n";
}
}
Can anyone who's kind enough to test this for me, as I'm really frustrated about this not working well with C++ strings...
Posted: Tue Jul 04, 2006 1:13 am
by temper_3243
C strings and c++ strings are not the same. C++ strings neednot be terminated by '\0'
I changed the linke s
!='\0' to i < str.size()
and it got accepted.
Better use standard string functions for C++. In C it is valid. Anyhow the judge contains a good test case. I would be amazed to see it.
http://groups.google.com/group/comp.lan ... 38e6bea315
Posted: Tue Jul 04, 2006 2:54 am
by wktang
Thanks temper_3243!
I really appreciate your time for answering my question. And thanks for posting the link for more information. I did learn something new today!!!
I never thought of this before! ohh...

494 very wierd solution?!
Posted: Fri Dec 22, 2006 1:14 pm
by sklitzz
I sent my code and the judge just keeps saying Recieved and nothing happens it just hangs like that?!
Code: Select all
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
#define pb(x) push_back(x)
#define mp(a,b) make_pair(a,b)
int main() {
string s;
while( getline( cin, s ) ) {
int c = 0;
bool delim = true;
for( int i = 0; i < s.size(); ++i ) {
if( isalpha( s[i] ) && delim ) { c++; delim = false; }
if( s[i] == ' ' ) delim = true;
}
cout << c << endl;
}
return 0;
}
Posted: Fri Dec 22, 2006 2:18 pm
by mogers
Carlos already said that there is a bug.
Anyway , you should receive an email with the result of the submission, it happened to me.
494 output exceeded limit
Posted: Sun Feb 11, 2007 6:13 am
by Waddle
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
int i,n,m;
char sl[80];
char *tokenPrt;
while(1)
{
i=0;
gets(sl);
tokenPrt= strtok(sl," ");
while(tokenPrt!= NULL)
{
i++;
tokenPrt=strtok(NULL," ");
if(tokenPrt != NULL)
{
m=tokenPrt[0];
n=isalpha(m);
if(n == 0)
i--;
}
}
printf("%d\n",i);
}
return 0;
}
The following is acm's judgement:
Dear Waddle:
Your program output is greater (4236247 bytes) than the maximum
allowed for any problem by this judge (4194304 bytes).
You must interprete this as a Wrong Answer (in fact, is wrong!).
By some reason your program is writing an unlimited output.
Help me ,please!
Posted: Sun Feb 11, 2007 6:57 am
by helloneo
Search first.. there is an answer!!
Don't open a new thread if there is one already!!
Posted: Sun Feb 11, 2007 11:49 am
by Waddle
I add this ''free(tokenPrt); '' at the bottom.
But it got a "output exceeded limit" again.
Posted: Sun Feb 11, 2007 12:24 pm
by helloneo
Posted: Mon Feb 12, 2007 8:32 am
by Waddle
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
int i,n,m;
char sl[40];
while(gets(sl))
{
n=0;
i=0;
if(isalpha(sl[0]) != 0)
i++;
while(sl[n] != '\0')
{
if(isalpha(sl[n+1]) != 0 && sl[n] == ' ')
i++ ;
if(n != 0 && isalpha(sl[n]) != 0 && isalpha(sl[n-1]) == 0)
i++;
n++;
}
printf("%d\n",i);
}
return 0;
}
Now .... it beomes "Runtime Error (Signal 11)".
494 - Kindergarten Counting Game
Posted: Sat Mar 24, 2007 6:35 pm
by KaDeG
Hello,
I checked the forums for same test cases over 494 Kindergarten Countring problem...
and everything works fine! but i got WA!
Here is my C Code:
code deleted
Any ideas why i got wa?
Posted: Sun Mar 25, 2007 6:27 am
by S.M.ferdous
you have to take input till EOF. your code just take one line as input and then break.
just change these portion
scanf("%c", &ins); to while( scanf("%c", &ins)==1){
.......;
......;
printf("%ld\n", words);
words=0;
}
also use return 0; as successful terminator.
use long as words because the length of word may be arbitarily long.
I think this will help you.
thanks.
Posted: Sun Mar 25, 2007 7:15 pm
by KaDeG
ok, thanks
Posted: Thu Apr 26, 2007 5:25 pm
by Nakar
Code: Select all
CODE REMOVE
I make another code, and ........ AC
Posted: Thu Apr 26, 2007 7:51 pm
by Nakar
I read all thread about 494
But, i down undestand my WA.
It works properly with the example inputs.
Any person take a look at my code and tell me what's wrong.
Thanks.