#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int a;
cin>>a;
string b[a];
for(int i=0;i<a;i++)
{
cin>>b[i];
sort(b[i].begin(),b[i].end());
do
{
for(int j=0;j<b[i].length();j++)
{
cout<<b[i][j];
}
cout<<endl;
}while(next_permutation(b[i].begin(),b[i].end()));
cout<<endl;
}
return 0;
}
#include
#include
#include
using namespace std;
int main()
{
int a;
cin>>a;
string b[a];
for(int i=0;i<a;i++)
{
cin>>b[i];
sort(b[i].begin(),b[i].end());
do
{
for(int j=0;j<b[i].length();j++)
{
cout<<b[i][j];
}
cout<<endl;
}while(next_permutation(b[i].begin(),b[i].end()));
cout<<endl;
}return 0;
}
WA (line:380236) python版
from sys import stdin, stdout
def gen_permuation(letters, start_index, current_str):
if start_index >= len(letters):
yield current_str
else:
unique_letters = set()
for i in range(start_index, len(letters)):
if not letters[i] in unique_letters:
unique_letters.add(letters[i])
if i != start_index:
letters[start_index], letters[i] = letters[i], letters[start_index]
yield from gen_permuation(letters, start_index + 1, current_str + letters[start_index])
if i != start_index:
letters[start_index], letters[i] = letters[i], letters[start_index]
def gen_permuation_sorted(letters, current_str):
if len(letters) == 0:
yield current_str
else:
unique_letters = set()
for i in range(len(letters)):
if not letters[i] in unique_letters:
unique_letters.add(letters[i])
yield from gen_permuation_sorted(letters[:i]+letters[i+1:], current_str + letters[i])
num_tests = int(input())
for i in range(num_tests):
word = stdin.readline().strip()
word = sorted(word)
for word in gen_permuation_sorted(word, ""):
stdout.write("{}\n".format(word))
print()