-
Notifications
You must be signed in to change notification settings - Fork 2
/
Key.java
58 lines (49 loc) · 1.33 KB
/
Key.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
import java.util.*;
public class Key{
private static char[][] keyboard = {
{'q','w','e','r','t','y','u','i','o','p'},
{'a','s','d','f','g','h','j','k','l'},
{'z','x','c','v','b','n','m'}
};
public static void main(String[] args) { // TODO: Fix
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while(n-- > 0){
String word = sc.next();
int nc = sc.nextInt();
PriorityQueue<Correct> pq = new PriorityQueue<Correct>(nc);
while(nc-- > 0){
String test = sc.next();
int distance = 0;
for(int i = 0; i < test.length(); i++){
int x = 0, y = 0, X = 0, Y = 0;
find(test.charAt(i), x, y);
find(word.charAt(i), X, Y);
distance += Math.abs(x - X) + Math.abs(y - Y);
}
pq.add(new Correct(test, distance));
}
while(pq.size() != 0) pq.poll();
}
sc.close();
}
private static void find(char c, int x, int y){
for(x = 0; x < keyboard.length; x++){
for(y = 0; y < keyboard[x].length; y++){
if(c == keyboard[x][y])
return;
}
}
}
}
class Correct implements Comparable<Correct>{
protected String s;
protected int n;
public Correct(String word, int count){
s = word;
n = count;
}
public int compareTo(Correct c){
return Integer.compare(n, c.n);
}
}