Language:Default Anagram
Description You are to write a program that has to generate all possible words from a given set of letters. Input The input consists of several words. The first line contains a number giving the number of words to follow. Each following line contains one word. A word consists of uppercase or lowercase letters from A to Z. Uppercase and lowercase letters are to be considered different. The length of each word is less than 13. Output For each word in the input, the output should contain all different words that can be generated with the letters of the given word. The words generated from the same input word should be output in alphabetically ascending order. An upper case letter goes before the corresponding lower case letter. Sample Input 3 aAb abc acba Sample Output Aab Aba aAb abA bAa baA abc acb bac bca cab cba aabc aacb abac abca acab acba baac baca bcaa caab caba cbaa Hint An upper case letter goes before the corresponding lower case letter. Source |
#include <bits/stdc++.h>
using namespace std;
char s[1000];
int cmp(char &a, char &b)
{
int x1 = a;
int x2 = b;
if (x1 >= 'A' && x1 <= 'Z')
{
x1 = (x1 - 'A') * 2;
}
else
{
x1 = (x1 - 'a') * 2 + 1;
}
if (x2 >= 'A' && x2 <= 'Z')
{
x2 = (x2 - 'A') * 2;
}
else
{
x2 = (x2 - 'a') * 2 + 1;
}
return (x1 < x2);
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%s", s);
sort(s, s + strlen(s), cmp);
printf("%s\n", s);
while (next_permutation(s, s + strlen(s), cmp))
{
printf("%s\n", s);
}
}
return 0;
}