195 - Anagram

All about problems in Volume 1. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

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

Post by UFP2161 » Sun Feb 06, 2005 6:06 pm

The scanf function requires the cstdio include.

Though, you should really never mix calls between scanf and cin (likewise for printf and cout).

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

Post by sklitzz » Sun Feb 06, 2005 6:17 pm

Aaargh! I hate mistakes like this!

Anyway thanks!

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

195 problem

Post by sklitzz » Mon Feb 07, 2005 3:53 pm

I don't know why I get WA, maybe someone sees the error...

Code: Select all

#include <iostream>
#include <string>
#include <set>
#include <algorithm>
using namespace std;

int N;
set < string > perms;
typedef set < string >::iterator SIT;

int main()
{
	cin >> N;
	for( int i = 0; i < N; ++i )
	{
		string t; cin >> t;
		//generate_perms
		sort( t.begin(), t.end() );
		do
		{
			perms.insert( t );
		} while( next_permutation( t.begin(), t.end() ) );
		
		//output_perms
		for( SIT it = perms.begin(); it != perms.end(); ++it  )
			cout << *it << endl;
		perms.clear();
	}
	
	return 0;
}


User avatar
GVahe
New poster
Posts: 17
Joined: Thu Aug 05, 2004 6:04 pm
Location: Armenia
Contact:

Post by GVahe » Mon Feb 07, 2005 5:03 pm

Hi, maybe this small test case will help u

input
1
Ca
ouput
aC
Ca

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

Post by sklitzz » Mon Feb 07, 2005 6:40 pm

Does this mean that 'C'(big c) should be behind 'a'(small a ) although c is capital?

User avatar
GVahe
New poster
Posts: 17
Joined: Thu Aug 05, 2004 6:04 pm
Location: Armenia
Contact:

Post by GVahe » Mon Feb 07, 2005 6:52 pm

sklitzz wrote:Does this mean that 'C'(big c) should be behind 'a'(small a ) although c is capital?
of course, but I don't know if there is such function in C++ for this problem. I think u must do it yourself (like me).

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

Post by sklitzz » Tue Feb 08, 2005 10:12 pm

Could anyone tell me why this comparision doesn't work?

Code: Select all

struct cmp
{
	bool operator () ( const string &a, const string &b )
	{
		string s1 = "", s2 = "";
		for( int i = 0; i < a.length(); ++i ) s1 += toupper( a[i] );
		for( int i = 0; i < b.length(); ++i ) s2 += toupper( b[i] );
		
		if( s1 == s2 ) return ( a < b );
		return ( s1 < s2 ); 
	}
};

User avatar
ibrahim
Experienced poster
Posts: 149
Joined: Mon Feb 07, 2005 10:28 pm
Location: Northern University, Bangladesh
Contact:

195 Compile error !!!!!!!!!

Post by ibrahim » Fri Feb 18, 2005 9:04 am

I got compile error with this code. Anybody help me? :(

Code: Select all

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

long double fact[20] ={1,1,2,6,24,120,720,5020,40320,362880,3628800,39916800,479001600,479001600,87178291200,1307674368000,20922789888000};

long int q;

int permutation(char *a,long int n)
{
	long int j,k,r,s,v;
	char temp;
	j = n - 2;
	while (a[j]>=a[j+1])
	{
		j--;
	}
	if (j<0)
	{
		return 1;
	}
	k = n - 1;
	while (a[j]>=a[k])
	{
		k--;
	}
	temp = a[j];
	a[j] = a[k];
	a[k] = temp;
	r = n - 1;
	s = j + 1;
	while (r>=s)
	{
		temp = a[r];
		a[r] = a[s];
		a[s] = temp;
		r--;
		s++;
	}
	printf ("%s\n",a);
	return 1;
}

void sort(char *arr,long int n)
{
	long int i,j;
	char temp;
	for (i=0;i<n-1;i++)
	{
		for (j=0;j<n-1-i;j++)
		{
			if (arr[j]>arr[j+1])
			{
				temp = arr[j+1];
				arr[j+1] = arr[j];
				arr[j] = temp;
			}
		}
	}
}


int main()
{
	char s1[1000];
	long int len,i,kase,j,v,k,flag;
	scanf ("%ld",&kase);

	for (j=0;j<kase;j++)
	{
		scanf ("%s",s1);
		flag = 0;
		len = strlen(s1);
		for (k=0;k<len;k++)
		{
			if (s1[0] == s1[k]);
			else
			{
				flag = 1;
				break;
			}
		}

		if (flag)
		{
			sort(s1,len);
			printf ("%s\n",s1);
			q = 0;
			for (i=0;i<fact[len]-1;i++)
			{
			v = permutation(s1,len);
			if (!v) break;
			}
		}
		else 
			printf ("%s\n",s1);

		//printf ("\n");

	}
return 0;
}






[/quote]

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Fri Feb 18, 2005 9:35 am

You use numbers that don't fit into an integer. You should tell the compiler that in fact they are doubles by adding .0 at the end of each.

User avatar
ibrahim
Experienced poster
Posts: 149
Joined: Mon Feb 07, 2005 10:28 pm
Location: Northern University, Bangladesh
Contact:

Post by ibrahim » Fri Feb 18, 2005 9:40 am

Dear Duleba,
But i got accept in 10098 using this code with small change. :(

User avatar
ibrahim
Experienced poster
Posts: 149
Joined: Mon Feb 07, 2005 10:28 pm
Location: Northern University, Bangladesh
Contact:

WA 195 please help

Post by ibrahim » Fri Feb 18, 2005 7:45 pm

After solving the compile error i got WA :-? . Please give me some critical input and output from your AC program. :(

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Sat Feb 19, 2005 12:14 pm

Is this C or C++ code? If it's C then you shouldn't use C++-style comments (//).

User avatar
ibrahim
Experienced poster
Posts: 149
Joined: Mon Feb 07, 2005 10:28 pm
Location: Northern University, Bangladesh
Contact:

Post by ibrahim » Sat Feb 19, 2005 12:46 pm

Krzysztof Duleba wrote:Is this C or C++ code? If it's C then you shouldn't use C++-style comments (//).
Yes, I got the mistake. but after that i got WA. Can you help me by giveing input output :( .
And what's diferents between 10098 and this. I did not found anything :-? .
Thanks

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Sat Feb 19, 2005 2:46 pm

Input:

Code: Select all

1
bBaA
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

User avatar
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim » Tue Feb 22, 2005 3:39 pm

input:
AaB
output:
AaB
ABa
aAB
aBA
BAa
BaA

Post Reply

Return to “Volume 1 (100-199)”