-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerating_fast_sorted_permutation.cpp
More file actions
68 lines (61 loc) · 1.79 KB
/
generating_fast_sorted_permutation.cpp
File metadata and controls
68 lines (61 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Made by: Romeu I. L. Pires
for "Special topics in programming" course
in UFRJ (Universidade Federal do Rio de Janeiro),
on 2019.1 semester
- Problem PDF:
https://uva.onlinejudge.org/external/100/10098.pdf
*/
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <math.h>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
// OMG no need if we use next_permutation
void backtracking( string& current_word , string& remaining_word , set<string>& all_permutations ){
if( remaining_word.size() == 1 ){
all_permutations.insert( current_word + remaining_word );
}else{
set<char> already_passed;
for( size_t i = 0 ; i < remaining_word.size() ; i++ ){
char c = remaining_word[i];
if( already_passed.find(c) != already_passed.end() ) continue;
string new_current_word = current_word;
string new_remaining_word = remaining_word;
new_current_word.push_back( c );
new_remaining_word.erase( new_remaining_word.begin() + i );
backtracking( new_current_word , new_remaining_word , all_permutations );
already_passed.insert(c);
}
}
}
set<string> allPermutations( string word ){
set<string> ret;
string s = "";
backtracking( s , word , ret );
return ret;
}
int main(){
#ifndef ONLINE_JUDGE
ifstream cin("entrada.txt");
ofstream cout("saida.txt");
#endif
// ==========
int N;
string word;
cin >> N;
while( N-- ){
cin >> word;
sort(word.begin() , word.end());
bool next = true;
while( next ){
cout << word << endl;
next = next_permutation( word.begin() , word.end() );
}
cout << endl;
}
}