Page 1 of 9

WA for Prg 483!!!!

Posted: Tue Jun 25, 2002 8:56 pm
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]

483 - Word Scramble

Posted: Wed Jul 24, 2002 11:02 am
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]

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

Posted: Tue Aug 20, 2002 2:39 am
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]

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

Posted: Tue Aug 20, 2002 3:41 am
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.

now i get P.E.

Posted: Fri Aug 30, 2002 11:32 pm
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...

ACM-483

Posted: Tue Dec 03, 2002 10:46 am
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;
}

Re: ACM-483

Posted: Tue Dec 03, 2002 11:51 am
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..

Posted: Wed Jan 01, 2003 9:52 pm
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

Posted: Thu Jan 02, 2003 7:03 am
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!

Posted: Fri Jan 03, 2003 11:21 am
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

Posted: Fri Jun 20, 2003 8:20 pm
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.

483 WA

Posted: Thu Apr 22, 2004 1:45 am
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!

WA in 483 - Word Scramble

Posted: Sun Jul 25, 2004 12:56 pm
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

Posted: Tue Jul 27, 2004 10:37 pm
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

483 WA

Posted: Thu Jul 29, 2004 4:30 am
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]