-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHardwoodSpecies.java
51 lines (48 loc) · 1.46 KB
/
HardwoodSpecies.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
/* https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1167
* #greedy #implementation
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
public class HardwoodSpecies {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcases = Integer.parseInt(sc.nextLine());
sc.nextLine();
String s;
int idx = 0;
float rate = 0.0f;
for (int t = 0; t < testcases; t++) {
int numHardWoods = 0;
HashMap<String, Integer> hashMap = new HashMap<>();
ArrayList<String> arrHardWood = new ArrayList<>();
ArrayList<Integer> arrHardWoodNum = new ArrayList<>();
while (sc.hasNextLine()) {
s = sc.nextLine();
if (s.isEmpty()) {
break;
}
if (hashMap.containsKey(s)) {
idx = hashMap.get(s);
arrHardWoodNum.set(idx, arrHardWoodNum.get(idx) + 1);
} else {
arrHardWood.add(s);
arrHardWoodNum.add(1);
idx = arrHardWood.size() - 1;
hashMap.put(s, idx);
}
numHardWoods++;
}
Collections.sort(arrHardWood);
for (String str : arrHardWood) {
idx = hashMap.get(str);
rate = 100.0f * (float) arrHardWoodNum.get(idx) / numHardWoods;
System.out.println(String.format("%s %.4f", str, rate));
}
if (t < testcases - 1) {
System.out.println("");
}
}
}
}