forked from yeicol/URI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathURI1401GeneratingFastSortedPermutation.java
More file actions
68 lines (58 loc) · 1.92 KB
/
URI1401GeneratingFastSortedPermutation.java
File metadata and controls
68 lines (58 loc) · 1.92 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* See
* <a href="https://www.urionlinejudge.com.br/judge/en/problems/view/">URI1401GeneratingFastSortedPermutation</a>
*
* @author Brian Yeicol Restrepo Tangarife
*/
public class URI1401GeneratingFastSortedPermutation {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
int N = readInt();
String s;
while (N-- > 0) {
s = read();
Set<String> permutations = new HashSet<>();
permutations = getPermutations("", s, permutations);
List<String> ab = new ArrayList<>(permutations);
Collections.sort(ab, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
for (String string : ab) {
out.println(string);
}
out.println();
}
out.close();
}
private static Set<String> getPermutations(String prefix, String str, Set<String> s) {
int n = str.length();
if (n == 0) {
s.add(prefix);
} else {
for (int i = 0; i < n; i++) {
getPermutations(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n), s);
}
}
return s;
}
private static String read() throws IOException {
return in.readLine();
}
private static int readInt() throws IOException {
return Integer.parseInt(in.readLine());
}
}