-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path140.java
115 lines (88 loc) · 2.59 KB
/
140.java
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* UVa 140: Bandwidth
*
* Problem Statement: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=76
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.TreeMap;
public class Main {
public static String graph;
public static TreeMap<String, Integer> permutations;
public static TreeMap<Character, HashSet<Character>> edges;
public static void init() {
graph = "";
permutations = new TreeMap<String, Integer>();
edges = new TreeMap<Character, HashSet<Character>>();
}
public static void allPermutations(String nodes) {
permutation("", nodes);
}
private static void permutation(String perm, String nodes) {
int n = nodes.length();
if (n == 0)
permutations.put(perm, permutationMax(perm));
else {
for (int i = 0; i < n; i++) {
permutation(perm + nodes.charAt(i), nodes.substring(0, i) + nodes.substring(i + 1));
}
}
}
private static int nodeMax(char c, String perm) {
HashSet<Character> neighbors = edges.get(c);
int max = -1;
for (char n : neighbors) {
int dist = Math.abs(perm.indexOf(c) - perm.indexOf(n));
max = dist > max ? dist : max;
}
return max;
}
public static int permutationMax(String perm) {
int max = -1;
for (int i = 0; i < perm.length(); i++) {
int b = nodeMax(perm.charAt(i), perm);
max = b > max ? b : max;
}
return max;
}
public static String minBandwidth() {
int min = Integer.MAX_VALUE;
String minPerm = "";
for (String s : permutations.keySet()) {
if (permutations.get(s) < min) {
min = permutations.get(s);
minPerm = s;
}
}
return minPerm.replace("", " ").trim() + " -> " + min;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String raw = br.readLine();
while (!raw.equals("#")) {
String[] spl = raw.split(";");
init();
for (String s : spl) {
char c = s.charAt(0);
if (!edges.containsKey(c)) { // if we have not seen this character yet, add it to edges and graph
edges.put(c, new HashSet<Character>());
graph += c;
}
char[] neighbors = s.substring(2).toCharArray();
for (char n : neighbors) {
if (!edges.containsKey(n)) { // if we have not seen this character yet, add it to edges and graph
edges.put(n, new HashSet<Character>());
graph += n;
}
edges.get(n).add(c);
edges.get(c).add(n);
}
}
allPermutations(graph);
System.out.println(minBandwidth());
raw = br.readLine();
}
}
}