483 - Word Scramble

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

Post Reply
knapsack
New poster
Posts: 3
Joined: Tue Jun 25, 2002 8:12 pm

WA for Prg 483!!!!

Post by knapsack »

I submitted the below code and received a 'Wrong Answer'. But, it worked for the test cases I tried out. Can anyone help me?? :(
[cpp]
/*"@BEGIN_OF_SOURCE_CODE"*/
#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <ctype.h>

char* reverseToken (char* inputToken)
{
int strlength = strlen(inputToken);
char* reverse = new char[strlength];
int i = 0;
for (i = 0; i < strlength; i++)
{
reverse = inputToken[strlength - i - 1];
}
reverse = '\0';

return (reverse);
}

char* insertToken(char* dest, int atPos, char* source, int numChars)
{
int i = 0;
for (i = atPos; i < (atPos+numChars); i++ )
{
dest = source[i-atPos];
}
return (dest);
}

int skipSpaces(char* input, int fromPos)
{
char ch = input[fromPos];
int len = strlen(input);
int endPos = fromPos;
while (isspace(ch))
{
endPos++;
if (endPos >= len) break;
ch = input[endPos];
}
return (endPos);
}

char* findToken(char* input, int fromPos)
{
char ch = input[fromPos];
int endPos = fromPos;
int len = strlen(input);
while ( !(isspace(ch)) )
{
endPos++;
if (endPos >= len) break;
ch = input[endPos];
}
int tokenLen = endPos - fromPos;
char* token = new char[tokenLen];
int i =0;
for (i = 0; i < tokenLen; i++)
{
token = input[fromPos+i];
}
token = '\0';

return (token);
}

void main () {
char input[1000000];
char tokensep[] = "\t ";
char* output;

while (gets(input))
{
int strlength = strlen(input);
output = new char[strlength];

strcpy(output, input);

int startPos = 0;
int nextPos = skipSpaces(output, startPos);

while (nextPos < strlength) {
char *token = new char[strlength];
token = findToken(output,nextPos);
char *revToken = new char[strlength];
revToken = reverseToken(token);
output = insertToken(output,nextPos,revToken,strlen(revToken));
startPos = nextPos + strlen(revToken);
nextPos = skipSpaces(output,startPos);
}
cout <<output<<endl;
}
}
/*"@END_OF_SOURCE_CODE"*/[/cpp]
htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan

483 - Word Scramble

Post by htl »

Why does this code get WA? Are there just 'white space' between two words?
[c]
#include<stdio.h>
#include<string.h>
void main(void)
{
int x,y;
char s[1000],temp[100];
while(fgets(s,1000,stdin)!=NULL)
{
s[strlen(s)-1]='\0';
for(x=0,y=0;s[x]!='\0';x++)
if(s[x]!=' ')
temp[y++]=s[x];
else
{
for(y--;y>=0;y--)
printf("%c",temp[y]);
y=0;
printf(" ");
}
if(y>0)
for(y--;y>=0;y--)
printf("%c",temp[y]);
printf("\n");
}
}
[/c]
Shahid
Learning poster
Posts: 68
Joined: Fri Oct 26, 2001 2:00 am
Location: Dhaka, Bangladesh
Contact:

i got compile error, whats the problem? can anyone help me..

Post by Shahid »

here is my code, wher the judge always replies compile erroe, but i can't find the bug. thanx in advance.

[c]
/*@BEGIN_OF_SOURCE_CODE*/



#include<stdio.h>
#include<ctype.h>


void main()
{
char in[100], out[100], ch;
int i= 0, j = 0, k = 0, l = 0;


while(gets(in))
{
k = i;

while(in != NULL)
{
while(in != ' ' && in)
i++;

for(j = i-1; j >= k; j--, l++)
out[l] = in[j];

out[l] = ' ';

if(in)
{
i++;
k = i;
l++;
}
}

out[l] = '\0';
puts(out);

i = j = k = l = 0;
}
}

[/c]
arc16
Learning poster
Posts: 62
Joined: Sun Aug 04, 2002 1:05 am
Location: Indonesia

Re: i got compile error, whats the problem? can anyone help

Post by arc16 »

Shahid wrote:here is my code, wher the judge always replies compile erroe, but i can't find the bug. thanx in advance.
your program seems ok to me. I think the small problem is:

1. Do not put a newline in the last case.

Code: Select all

The output will consist of the same lines and words as the input file. However, the letters within each word must be reversed. 
2. Enlarge your array size (i don't know the optimal size, but mine is 50000). Also, you only need 1 array to store the char. That way, you just have to process the output everytime you encounter a blank space or end of line.
Shahid
Learning poster
Posts: 68
Joined: Fri Oct 26, 2001 2:00 am
Location: Dhaka, Bangladesh
Contact:

now i get P.E.

Post by Shahid »

hi, now iget alltime P.E. - whas the problem may be in my code? is it the problem occuring for putting a newline after the last case? if os then how can i fix that? here is my code:


[cpp]
/*@BEGIN_OF_SOURCE_CODE*/


#include<stdio.h>
#include<ctype.h>


void main()
{
char in[50000];
int i = 0, j = 0, k = 0, l = 0, tab = 0;


while(gets(in) != NULL)
{
i = j = k = l = tab = 0;

while(in != NULL)
{
tab = 0;
while(in != ' ' && in != '\t' && in)
i++;

if(in == '\t')
tab = 1;

for(j = i-1; j >= k; j--, l++)
printf("%c", in[j]);

if(tab)
printf("\t");
else
printf(" ");

if(in)
{
i++;
k = i;
l++;
}
}
printf("\n");
}
}


/*@END_OF_SOURCE_CODE*/
[/cpp]

thanx in advance for helping...
supermin
New poster
Posts: 37
Joined: Sat Oct 12, 2002 9:54 am
Location: (Taiwan)
Contact:

ACM-483

Post by supermin »

This is my code. I got WA,but I can't find the bug.
I tested many times. Could someone help me??

Thanks in advance.

Code: Select all

#include<stdio.h>

int main(void)
{
 char str[50000];
 char c;
 int top=0;
 int i;
 while(scanf("%c",&c)==EOF){
	 if(c!=' '&&c!='\n') {
		 str[top]=c;
		 top++;
	 }
	 else  {
	    for(i=top-1;i>=0;i--)
			printf("%c",str[i]);
	    if(c!='\n') printf(" ");
		top=0;
		if(c=='\n') printf("\n");
	 }
	
 }
 return 0;
}
supermin
New poster
Posts: 37
Joined: Sat Oct 12, 2002 9:54 am
Location: (Taiwan)
Contact:

Re: ACM-483

Post by supermin »

I got A.C now.

I got W.A. because I ignore the EOF case.
When entering EOF,it still should print out the last stack.

Anyway,thanks..
deddy one
Experienced poster
Posts: 120
Joined: Tue Nov 12, 2002 7:36 pm

Post by deddy one »

I don't get it .
what do you mean by print the last stack?/?

I got WA and I think it's the same EOF stupid problem
too
supermin
New poster
Posts: 37
Joined: Sat Oct 12, 2002 9:54 am
Location: (Taiwan)
Contact:

Post by supermin »

Ex:
I enter "abc" and use EOF instead of ENTER.
It should print "cba",not print nothing.

my code above is wrong.dont's take it..

good luck!
deddy one
Experienced poster
Posts: 120
Joined: Tue Nov 12, 2002 7:36 pm

Post by deddy one »

thx supermin , finally got ACC after tons of WA,
just found out that using the function isgraph in ctype.h
will make things easier in this one


:P
Taneem
New poster
Posts: 6
Joined: Thu Jun 05, 2003 6:26 pm
Location: Arkansas, USA

Post by Taneem »

Shahid:

Do this trick: read the input with %s first and then do a %c. So you will only read the words and then in the second scanf u will read the white space or newline with the %c. This will reduce a lot of code as well.

Like this:

Code: Select all

while(scanf("%s", word)==1)
{
   scanf("%c", &ctrl);
   ........................
  ......................... 
}
You print the word first and then the ctrl. Hope this helps.
Give a man an answer, and he's satisfied today. Teach him to program, and he will be frustrated for the rest of his life. [Anonymous]
midra
Experienced poster
Posts: 119
Joined: Fri Feb 13, 2004 7:20 am
Contact:

483 WA

Post by midra »

[c]#include <stdio.h>


int main()
{
char letra[1000],letrar[1000];
int i=0,j,k,p=0,x;

while(letra!='\n')
{
i++;
scanf("%c", &letra);

if (letra==' ')
{
for (j=p,k=i-1;k>=p;j++,k--)
letrar[j]=letra[k];
p=i-1;
letrar[j+1]=' ';
p++;
}
}

if (letra=='\n')
{
for (j=p,k=i-1;k>=p;j++,k--)
letrar[j]=letra[k];
p=i-1;
for (j=0;j<=i-1;j++)
printf("%c",letrar[j]);
}
printf("\n");
i=p=0;
return 0;
}[/c]

It is a simple problem but I can't figure out why I always get WA... if someone know about some special input or what is wrong with my code please let me know...
bye!
shellcode
New poster
Posts: 1
Joined: Fri Jul 16, 2004 9:34 pm

WA in 483 - Word Scramble

Post by shellcode »

Greets!

This code runs great but somehow returns WA.. Do you see something wrong?

[c]
#include <stdio.h>

int main(void)
{
char frase[10000], pal[500];
int i, j;

while(fgets(frase, 1000, stdin))
{
i=0;
while(frase!='\n')
{
for(j=0; frase!=' ' && frase!='\n'; i++, j++)
pal[j]=frase;

j--;

for(j; j>=0; j--)
printf("%c", pal[j]);

if(frase==' ')
{
printf(" ");
i++;
}
}
printf("\n");
}
}
[/c]

Thx in advance
\x90
Piotrek Mazur
New poster
Posts: 17
Joined: Thu Jul 15, 2004 10:55 am
Location: Poland, Rzeszow University of Technology

Post by Piotrek Mazur »

I set this as input:
test
longtest
test
In output in third line was error - it looks like this:
tse'\0'tset
oulongbin
Learning poster
Posts: 53
Joined: Sat Jul 10, 2004 5:57 pm
Location: Shanghai China

483 WA

Post by oulongbin »

Could somebody tell me what's wrong with my code? Thanx.
[cpp]
#include <iostream>
using namespace std;
#include <cstdio>
#include <cstring>
int main()
{
int temp;
bool flag;
char *a;
a=new char;
while(gets(a))
{
flag=true;
for(int i=0;i<strlen(a);i++)
{
if(a[i-1]==' '&&a==' ')
cout<<" ";
if(a!=' '&&flag)
{
temp=i;
flag=false;
}
else if(a==' ')
{
for(int j=i-1;j>=temp;j--)
cout<<a[j];
cout<<" ";
flag=true;
}
else if(i==strlen(a)-1)
{
for(int j=i;j>=temp;j--)
cout<<a[j];
break;
}

}
cout<<endl;
}
return 0;
}
[/cpp]
Post Reply

Return to “Volume 4 (400-499)”