Page 13 of 17

Re: 195: anagram, wrong answer

Posted: Sun Apr 19, 2009 3:16 am
by deadangelx
some info about Problem 195
1.there is no blank between characters in the word.
2.the length about 100 characters is enough
3.alphabet order is A -> a -> B -> b -> C -> c ..... -> Y -> y -> Z -> z
4.Problem 146 is similar with Problem 195, but test cases in Problem 146 are weak, and some wrong algorithms(I have tested) also got Accepted.
So make sure your algorithm is correct.

Hope it helps.

Re: 195: anagram, wrong answer

Posted: Wed Mar 31, 2010 4:27 pm
by a13032002
I think my algorithm is right,but I got WA.
Could someone tell me why?please

Code: Select all

AC
    

Re: 195: anagram, wrong answer

Posted: Thu Apr 01, 2010 10:16 pm
by helloneo
I think your compare function is not correct..

m=i>='a'&&i<='a'?i-'a':i-'A';

Shouldn't this be..

m= i>='a' && i <= 'z' ? i - 'a' : i - 'A'

Re: 195: anagram, wrong answer

Posted: Fri Apr 02, 2010 5:00 pm
by a13032002
helloneo wrote:I think your compare function is not correct..

m=i>='a'&&i<='a'?i-'a':i-'A';

Shouldn't this be..

m= i>='a' && i <= 'z' ? i - 'a' : i - 'A'
I got AC.Thank you! :D
That was a stupid mistake

Re: 195: anagram, wrong answer

Posted: Mon Apr 26, 2010 10:19 pm
by Mehadi
What's problem in my Code .PLZ help.

Code: Select all

#include <iostream>
#include<string.h>
#include <algorithm>
using namespace std;

int main () 
{

	long i,j,k,l,n;
	char myints[1000],s[1001];
	scanf("%ld",&n);
	while(n--)
	{
		long a[130]={0};
	
		scanf("%s",&myints);
		
		k=strlen(myints);
 		for(i=0;i<k;i++)
		{
			a[myints[i]]++;
		}
		l=0;
		for(j=65;j<=90;j++)
		{
			if(a[j])
			{
				for(i=0;i<a[j];i++)
					s[l++]=j;
			}
			if(a[j+32])
			{
				for(i=0;i<a[j+32];i++)
					s[l++]=j+32;
			}
		}
		s[l]='\0';
		do 
		{
			printf("%s\n",s);
		} while ( next_permutation (s,s+l) );
	}

  return 0;
}

Re: 195: anagram, wrong answer

Posted: Fri Oct 01, 2010 6:37 pm
by Shafaet_du
next permutation function wont be able to solve it. STL is too slow. again suppose the string is aaaaaaaaaaaaaaa. thats 15 characters,but ans is only one line, "aaaaaaaaaaaaaaa". If you use STL and hashtable to eliminate the duplicates it will stil genarate 15! outputs and you will get TLE. only clever backtracking can solve the problem.

Re: 195: anagram, wrong answer

Posted: Fri Oct 01, 2010 7:39 pm
by helloneo
Mehadi wrote:What's problem in my Code .PLZ help
Try this input..

Code: Select all

1
Ba

My output is..

Code: Select all

aB
Ba

Re: 195: anagram, wrong answer

Posted: Sat Sep 10, 2011 6:22 pm
by plamplam
by Shafaet_du ยป Fri Oct 01, 2010 10:37 pm

next permutation function wont be able to solve it. STL is too slow. again suppose the string is aaaaaaaaaaaaaaa. thats 15 characters,but ans is only one line, "aaaaaaaaaaaaaaa". If you use STL and hashtable to eliminate the duplicates it will stil genarate 15! outputs and you will get TLE. only clever backtracking can solve the problem.
I totally disagree with you. I just solved this using STL's next_permutation(0.044s). It works fine if the string is aaaaaaaaaaaaaaaaaaaaaaaaaaaaa or aaaaaaaaaaaaaabaaaaaaaaaaaaaa etc. :roll:

195 - WA Anagram I am stumped !!

Posted: Tue Apr 24, 2012 6:24 pm
by mathgirl
My code works fine on my system. I have tested it with several inputs..
But i got WA .
Please help !!!

Code: Select all

Code deleted after AC 

Re: 195 - WA Anagram I am stumped !!

Posted: Wed Apr 25, 2012 12:00 am
by brianfry713
On my system this code doesn't work. You shouldn't try to modify the data in a string.
http://www.cplusplus.com/reference/string/string/data/

Instead of pushing word onto your list you can create a new string from letters.

Re: 195 - WA Anagram I am stumped !!

Posted: Wed Apr 25, 2012 4:15 pm
by mathgirl
I got the problem.. it was in the sorting order. I wrote a custom compare function for sort and got accepted. :)
Modifying the original string should work fine on any system. next_permutation is an STL algorithm, so it cant be wrong.

Re: 195 - WA Anagram I am stumped !!

Posted: Thu Apr 26, 2012 1:54 am
by brianfry713
The problem wasn't in next_permutation, the sort function didn't work because you had:
char *letters=(char*)word.data();
sort(letters,letters+len);

Here letters is a pointer to an internal location which should not be modified directly in the program. Its contents are guaranteed to remain unchanged only until the next call to a non-constant member function of the string object.

Instead you could have done:
char letters[len+1];
strcpy(letters,word.c_str());
sort(letters,letters+len);

And instead of pushing back word onto your list, do:
string s(letters);
output.push_back(s);

Re: 195 - WA Anagram I am stumped !!

Posted: Thu Apr 26, 2012 8:31 pm
by mathgirl
Thank brian ! I'll make sure to follow this next time. :)

195 - Anagram

Posted: Wed Jun 06, 2012 6:04 pm
by @ce
Whats wrong with my code...plzz help

Code: Select all

Code removed after AC

Re: 195 - Anagram

Posted: Wed Jun 06, 2012 10:31 pm
by brianfry713
Input:

Code: Select all

1
aAbB
AC output:

Code: Select all

AaBb
AabB
ABab
ABba
AbaB
AbBa
aABb
aAbB
aBAb
aBbA
abAB
abBA
BAab
BAba
BaAb
BabA
BbAa
BbaA
bAaB
bABa
baAB
baBA
bBAa
bBaA