454 - Anagrams

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

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski » Mon Jun 02, 2003 7:58 am

I made one modification - I remember not only distinct phrases, but phrases like "a = a" too.... and got Acc after rejudgement ... So I think, that my last post is correct.

Happy hunting
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)

Almost Human
Learning poster
Posts: 93
Joined: Sun Jan 12, 2003 3:30 pm

454 - WA

Post by Almost Human » Fri Jun 20, 2003 11:20 am

I couldn't figure out why I got WA .... Please give me some special input and output ....

Code: Select all

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

int main ( void )
{
  char input[60000] , ToBePrinted ;
  unsigned i ; int counter , j ;

/*  freopen ( "445.in" , "r" , stdin ) ;
  freopen ( "445.out" , "w" , stdout ) ;*/

  while ( scanf ( "%[^\n!]" , input ) != EOF )
  {
	 fgetc ( stdin ) ;

	 for ( i = counter = 0 ; input[i] ; i ++ )
	 {
		if ( isdigit ( input[i] ) )
		  counter += ( input[i] -'0' ) ;
		else
		{
		  if ( input[i] == 'b' ) ToBePrinted = 32 ;
		  else ToBePrinted = input[i] ;

		  for ( j = 0 ; j < counter ; j ++ )
			 printf ( "%c" , ToBePrinted ) ;

		  counter = 0 ;
		}
	 }
	 printf ( "\n" ) ;
  }

  return 0 ;
}

Junayeed
New poster
Posts: 50
Joined: Sat Oct 26, 2002 9:02 am
Location: Dhaka, Bangladesh

Post by Junayeed » Mon Aug 04, 2003 8:31 pm

Please help me with some mutiple test cases.
Thanks.

User avatar
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 » Thu Aug 07, 2003 2:09 am

Sample Input repeated twice:
1T1b5T!1T2b1T1b2T!1T1b1T2b2T!1T3b1T1b1T!3T3b1T!1T3b1T1b1T!5T1*1T

1T1b5T!1T2b1T1b2T!1T1b1T2b2T!1T3b1T1b1T!3T3b1T!1T3b1T1b1T!5T1*1T
Your output:

Code: Select all

T TTTTT
T  T TT
T T  TT
T   T T
TTT   T
T   T T
TTTTT*T
TTTTT*T
T TTTTT
T  T TT
T T  TT
T   T T
TTT   T
T   T T
TTTTT*T
There should be a newline between to two test cases, instead you're somehow printing the last line again. (This doesn't always happen though, as in putting the second example after the above two produces a blank line, but putting that second example again doesn't...).

P.S. Please fix the subject to reflect the correct problem number. Thanks =)

User avatar
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 » Thu Aug 07, 2003 2:11 am

Unlike the sample input, I think the real input has uppercase letters as well, which should be considered different from their lowercase counterparts. That was the only difference between my WA and AC.

User avatar
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 » Thu Aug 07, 2003 2:25 am

zsepi wrote:the whole issue is that I am not getting WA, but RE, which is really upsetting after X time spent debugging that prog and finally got it working
Without your code, the only thing I can ask is are you checking for uppercase letters as well? Because if you just assume that lowercase letters are being used as in the sample input, you might be doing something trying to use an index like ['A' - 'a'] which is a negative number. The size of the array would thus not matter if that were the case.

Alternatively, if your array is too big and is declared in main, it might also give you a Memory Exception.

jeff1999
New poster
Posts: 4
Joined: Wed Oct 01, 2003 5:35 pm

Post by jeff1999 » Fri Oct 03, 2003 5:47 am

I was hoping that you guys could give me a hand. I am getting "WA" everytime, and my guess is that there is some ambiguity I am not accounting for in the problem statement. Could someone please point out the flaw in the following code?
Many thanks,
-Jeff

[cpp]// 454 - Anagrams

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Word
{
char line[200];
char orig[200];
};

int c_cmp(const void * a, const void * b)
{
return (int)(*((char*)a) - *((char*)b));
}

int w_cmp(const void * a, const void * b)
{
Word * w1 = (Word*)a, * w2 = (Word*)b;
return strcmp(w1->orig, w2->orig);
}

void strscpy(char * dst, char * src) // strcpy but skip if < 0x20
{
while (*src)
{
if (*src > ' ')
{
*dst = *src;
++dst;
}
++src;
}
*dst = 0;
}

int main(int argc, char ** argv)
{
Word words[200];
int i = 0, j, n = 1;
while (n && gets(words.orig) && strlen(words.orig) > 0)
{
strscpy(words.line, words.orig);
qsort(words.line, strlen(words.line), sizeof(char), c_cmp);
++i;
}
qsort(words, i, sizeof(Word), w_cmp);
n = i;
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (strcmp(words.line, words[j].line) == 0)
{
printf("%s = %s\n", words.orig, words[j].orig);
}
}
}
return 0;
}[/cpp]

jeff1999
New poster
Posts: 4
Joined: Wed Oct 01, 2003 5:35 pm

Post by jeff1999 » Fri Oct 03, 2003 5:53 am

Nevermind, after experimenting with something, I got an accepted (With PE). I wrapped my everything after and including my while loop in main inside of a
[cpp]
while (!feof(stdin))
{
i = 1;
// rest of code went here
}
[/cpp]

Nowhere in the problem does it state that their will be multiple lists of anagrams though, yet it seems that their were indeed multiple lists.
Again, my apologies for the large (and unnecessary) post above.
-Jeff

jeff1999
New poster
Posts: 4
Joined: Wed Oct 01, 2003 5:35 pm

Post by jeff1999 » Fri Oct 03, 2003 6:14 am

Nevermind, after experimenting with something, I got an accepted (With PE). I wrapped my everything after and including my while loop in main inside of a
[cpp]
while (!feof(stdin))
{
i = 1;
// rest of code went here
}
[/cpp]

Nowhere in the problem does it state that their will be multiple lists of anagrams though, yet it seems that their were indeed multiple lists.
Again, my apologies for the large (and unnecessary) post above.
-Jeff

InOutMoTo
New poster
Posts: 18
Joined: Sun Aug 10, 2003 12:47 pm

Post by InOutMoTo » Tue Jul 20, 2004 12:05 pm

my approach is :

1: Get all input and sort them. (using qsort + strcmp)
2: store all data (since ASCII range is 0-255) in array[MAXSIZE][300]
3:
[c]for(i = 0; i < input; i++)
for(j = i+1; j < input; j++)
if(check(data, data[j]) == YES)
output(input, input[j]);[/c]

best regards!

Guest
New poster
Posts: 39
Joined: Wed May 19, 2004 5:52 pm
Location: Dhaka, Bangladesh
Contact:

WA

Post by Guest » Sun Aug 15, 2004 9:40 am

Hello,
I'm getting WA for this problem though my code seems to give correct ouput for all sample data i could find. Can anyone give me some more sample i/o?
Thank you,

Edited: 21.08.2004
Comment: I've got AC, :D

Junayeed
New poster
Posts: 50
Joined: Sat Oct 26, 2002 9:02 am
Location: Dhaka, Bangladesh

454 - Anagrams, inputs???

Post by Junayeed » Tue Aug 31, 2004 7:11 pm

Can anyone tell me how the inputs will be for this problem.
As it is multiple input problem.Will it look like this:

6

carthorse
horse
horse cart
i do not know u
ok i now donut
orchestra

3

aaa
aaa
aaa

What will be the output for the 2nd input. Please help me.

Junayeed

wolf
New poster
Posts: 34
Joined: Sun Aug 22, 2004 4:20 am
Location: Poland

454 Invalid memory reference

Post by wolf » Fri Sep 03, 2004 11:35 am

Hi !
My code have Invalid memory reference. Where is the bug in my code ?

[cpp]
//removed for saving the space in the topic
[/cpp]

Thx for future help :-D !
Last edited by wolf on Fri Sep 03, 2004 5:32 pm, edited 1 time in total.

Junayeed
New poster
Posts: 50
Joined: Sat Oct 26, 2002 9:02 am
Location: Dhaka, Bangladesh

Post by Junayeed » Fri Sep 03, 2004 1:30 pm

Use array lenght of 500 instead of 255.
Hope this will work.
Best of luck.

Junayeed

wolf
New poster
Posts: 34
Joined: Sun Aug 22, 2004 4:20 am
Location: Poland

Post by wolf » Fri Sep 03, 2004 5:31 pm

Thanx :-D but this is WA now. I need some I/O.

[cpp]
/*removed, AC*/
[/cpp]
Last edited by wolf on Sat Sep 04, 2004 9:44 pm, edited 2 times in total.

Post Reply

Return to “Volume 4 (400-499)”