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

wktang
New poster
Posts: 8
Joined: Mon Jul 03, 2006 11:27 pm

494 WA till I changed input from string to char array.. Why?

Post by wktang » Mon Jul 03, 2006 11:40 pm

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...

temper_3243
Experienced poster
Posts: 105
Joined: Wed May 25, 2005 7:23 am

Post by temper_3243 » Tue Jul 04, 2006 1:13 am

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

wktang
New poster
Posts: 8
Joined: Mon Jul 03, 2006 11:27 pm

Post by wktang » Tue Jul 04, 2006 2:54 am

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!!! :D

I never thought of this before! ohh... :o

sklitzz
New poster
Posts: 32
Joined: Fri Dec 03, 2004 5:19 pm

494 very wierd solution?!

Post by sklitzz » Fri Dec 22, 2006 1:14 pm

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;
} 

mogers
New poster
Posts: 29
Joined: Tue May 30, 2006 5:09 pm
Location: Porto, Portugal

Post by mogers » Fri Dec 22, 2006 2:18 pm

Carlos already said that there is a bug.
Anyway , you should receive an email with the result of the submission, it happened to me.
Miguel Oliveira

Waddle
New poster
Posts: 22
Joined: Thu Jan 25, 2007 3:54 pm
Location: Taiwan
Contact:

494 output exceeded limit

Post by Waddle » Sun Feb 11, 2007 6:13 am

#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!

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

Post by helloneo » Sun Feb 11, 2007 6:57 am

Search first.. there is an answer!!
Don't open a new thread if there is one already!!

Waddle
New poster
Posts: 22
Joined: Thu Jan 25, 2007 3:54 pm
Location: Taiwan
Contact:

Post by Waddle » Sun Feb 11, 2007 11:49 am

I add this ''free(tokenPrt); '' at the bottom.
But it got a "output exceeded limit" again.

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

Post by helloneo » Sun Feb 11, 2007 12:24 pm

Hmm.. what I meant was here..

http://online-judge.uva.es/board/viewtopic.php?t=12681

Hope it helps..

Waddle
New poster
Posts: 22
Joined: Thu Jan 25, 2007 3:54 pm
Location: Taiwan
Contact:

Post by Waddle » Mon Feb 12, 2007 8:32 am

#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)".

User avatar
KaDeG
New poster
Posts: 13
Joined: Sun Mar 04, 2007 8:40 pm

494 - Kindergarten Counting Game

Post by KaDeG » Sat Mar 24, 2007 6:35 pm

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?
Last edited by KaDeG on Sun Mar 25, 2007 7:42 pm, edited 1 time in total.
/*No Comment*/

S.M.ferdous
New poster
Posts: 13
Joined: Fri Nov 03, 2006 2:53 pm
Location: bangladesh
Contact:

Post by S.M.ferdous » Sun Mar 25, 2007 6:27 am

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.

User avatar
KaDeG
New poster
Posts: 13
Joined: Sun Mar 04, 2007 8:40 pm

Post by KaDeG » Sun Mar 25, 2007 7:15 pm

ok, thanks
/*No Comment*/

Nakar
New poster
Posts: 16
Joined: Tue Apr 17, 2007 6:37 pm

Post by Nakar » Thu Apr 26, 2007 5:25 pm

Code: Select all

CODE REMOVE

I make another code, and ........ AC
Last edited by Nakar on Fri Apr 27, 2007 12:59 am, edited 3 times in total.


Post Reply

Return to “Volume 4 (400-499)”