1
+ import java .io .IOException ;
2
+ import java .util .HashMap ;
3
+ import java .util .Map ;
4
+
5
+ public class Main {
6
+ private static String left ;
7
+ private static String right ;
8
+ private static String str ;
9
+
10
+ private static int answer ;
11
+ private static Map <String , Pair > consonant = new HashMap <>();
12
+ private static Map <String , Pair > vowel = new HashMap <>();
13
+
14
+ static class Pair {
15
+ int x ;
16
+ int y ;
17
+
18
+ public Pair (int x , int y ) {
19
+ this .x = x ;
20
+ this .y = y ;
21
+ }
22
+ }
23
+
24
+ public static void main (String [] args ) throws Exception {
25
+ input ();
26
+
27
+ answer = solution ();
28
+
29
+ output ();
30
+ }
31
+
32
+ private static void input () throws Exception {
33
+ left = readString ();
34
+ right = readString ();
35
+ str = readString ();
36
+ }
37
+
38
+ private static int solution () {
39
+ consonant .put ("q" , new Pair (0 , 0 ));
40
+ consonant .put ("w" , new Pair (0 , 1 ));
41
+ consonant .put ("e" , new Pair (0 , 2 ));
42
+ consonant .put ("r" , new Pair (0 , 3 ));
43
+ consonant .put ("t" , new Pair (0 , 4 ));
44
+ consonant .put ("a" , new Pair (1 , 0 ));
45
+ consonant .put ("s" , new Pair (1 , 1 ));
46
+ consonant .put ("d" , new Pair (1 , 2 ));
47
+ consonant .put ("f" , new Pair (1 , 3 ));
48
+ consonant .put ("g" , new Pair (1 , 4 ));
49
+ consonant .put ("z" , new Pair (2 , 0 ));
50
+ consonant .put ("x" , new Pair (2 , 1 ));
51
+ consonant .put ("c" , new Pair (2 , 2 ));
52
+ consonant .put ("v" , new Pair (2 , 3 ));
53
+
54
+ vowel .put ("y" , new Pair (0 , 5 ));
55
+ vowel .put ("u" , new Pair (0 , 6 ));
56
+ vowel .put ("i" , new Pair (0 , 7 ));
57
+ vowel .put ("o" , new Pair (0 , 8 ));
58
+ vowel .put ("p" , new Pair (0 , 9 ));
59
+ vowel .put ("h" , new Pair (1 , 5 ));
60
+ vowel .put ("j" , new Pair (1 , 6 ));
61
+ vowel .put ("k" , new Pair (1 , 7 ));
62
+ vowel .put ("l" , new Pair (1 , 8 ));
63
+ vowel .put ("b" , new Pair (2 , 4 ));
64
+ vowel .put ("n" , new Pair (2 , 5 ));
65
+ vowel .put ("m" , new Pair (2 , 6 ));
66
+
67
+ int distance = 0 ;
68
+ for (int i = 0 ; i < str .length (); ++i ) {
69
+ String c = String .valueOf (str .charAt (i ));
70
+ if (consonant .containsKey (c )) {
71
+ Pair positionL = consonant .get (left );
72
+ Pair positionC = consonant .get (c );
73
+ distance += Math .abs (positionL .x - positionC .x ) + Math .abs (positionL .y - positionC .y );
74
+ left = c ;
75
+ } else {
76
+ Pair positionR = vowel .get (right );
77
+ Pair positionC = vowel .get (c );
78
+ distance += Math .abs (positionR .x - positionC .x ) + Math .abs (positionR .y - positionC .y );
79
+ right = c ;
80
+ }
81
+ }
82
+
83
+ return str .length () + distance ;
84
+ }
85
+
86
+ private static void output () {
87
+ System .out .println (answer );
88
+ }
89
+
90
+ static int readInt () throws IOException {
91
+ int val = 0 ;
92
+ boolean negative = false ;
93
+
94
+ while (true ) {
95
+ int c = System .in .read ();
96
+ if (c == ' ' || c == '\n' || c == -1 ) {
97
+ break ;
98
+ }
99
+ if (c == '-' ) {
100
+ negative = true ;
101
+ continue ;
102
+ }
103
+ val = 10 * val + c - 48 ;
104
+ }
105
+
106
+ return negative ? -val : val ;
107
+ }
108
+
109
+ static String readString () throws IOException {
110
+ StringBuilder sb = new StringBuilder ();
111
+ while (true ) {
112
+ int c = System .in .read ();
113
+ if (c == ' ' || c == '\n' || c == -1 ) {
114
+ break ;
115
+ }
116
+ sb .append ((char ) c );
117
+ }
118
+ return sb .toString ();
119
+ }
120
+ }
0 commit comments