195 - Anagram
Moderator: Board moderators
195 problem
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;
}
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 );
}
};
-
- Experienced poster
- Posts: 149
- Joined: Mon Feb 07, 2005 10:28 pm
- Location: Northern University, Bangladesh
- Contact:
195 Compile error !!!!!!!!!
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]
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
-
- Experienced poster
- Posts: 149
- Joined: Mon Feb 07, 2005 10:28 pm
- Location: Northern University, Bangladesh
- Contact:
WA 195 please help
After solving the compile error i got WA
. Please give me some critical input and output from your AC program. 


-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
Input:
Output:
Code: Select all
1
bBaA
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