-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1002. Phone Numbers.java
137 lines (106 loc) · 3.89 KB
/
1002. Phone Numbers.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.util.Scanner;
public class PhoneNumbers {
public static final int INFINITY = -1;
static String N = ""; /* Phone number */
static int W = 0; /* nr of words */
static String[] dict = null; /* dictionary */
static int[][] Q; /* optimization matrix */
static int[][] T; /* memory matrix */
public static void print(int[][] M) {
for (int i = 0; i < W; i++) {
for (int j = 0; j < N.length(); j++) {
System.out.printf("%3d ", M[i][j]);
}
System.out.println();
}
}
public static boolean phoneMatch(char letter, char number) {
switch (number) {
case '1' : return (letter == 'i' || letter == 'j');
case '2' : return (letter == 'a' || letter == 'b' || letter == 'c');
case '3' : return (letter == 'd' || letter == 'e' || letter == 'f');
case '4' : return (letter == 'g' || letter == 'h');
case '5' : return (letter == 'k' || letter == 'l');
case '6' : return (letter == 'm' || letter == 'n');
case '7' : return (letter == 'p' || letter == 'r' || letter == 's');
case '8' : return (letter == 't' || letter == 'u' || letter == 'v');
case '9' : return (letter == 'w' || letter == 'x' || letter == 'y');
case '0' : return (letter == 'o' || letter == 'q' || letter == 'z');
}
return false;
}
public static boolean isMatch(int w, int p) {
if (dict[w].length() > N.length() - p) {
return false; /* no match since word is longer */
}
for (int i = 0, n = dict[w].length(); i < n; i++) {
if (!phoneMatch(dict[w].charAt(i), N.charAt(p + i))) {
return false;
}
}
return true;
}
public static int q(int w, int p) throws Exception {
if (p >= N.length())
return 0;
if (Q[w][p] == 0) {
if (!isMatch(w, p)) {
Q[w][p] = Integer.MAX_VALUE; /* aka infinity */
} else {
int min = Integer.MAX_VALUE;
for (int i = 0; i < W; i++) {
int t = q(i, p + dict[w].length());
if (t < min) {
min = t;
T[w][p] = i;
}
}
if (min != Integer.MAX_VALUE) /* otherwise overflow occurs */
Q[w][p] = 1 + min;
else
Q[w][p] = Integer.MAX_VALUE;
}
}
return Q[w][p];
}
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
while (true) {
N = sc.nextLine();
if (N.equals("-1")) {
System.exit(0);
}
W = Integer.valueOf(sc.nextLine());
dict = new String[W];
for (int i = 0; i < W; i++) {
dict[i] = sc.nextLine();
}
Q = new int[W][N.length()];
T = new int[W][N.length()];
for (int i = 0; i < W; i++) /* run for every word */
q(i, 0);
/* find the minimum starting word */
int min = Integer.MAX_VALUE;
int w = 0;
for (int i = 0; i < W; i++) {
if (Q[i][0] < min) {
min = Q[i][0];
w = i;
}
}
/* print the minimum word sequence */
if (min == Integer.MAX_VALUE) {
System.out.println("No solution.");
continue;
}
int p = 0;
while (p != N.length()) {
System.out.print(dict[w] + " ");
int t = p + dict[w].length();
w = T[w][p];
p = t;
}
System.out.println();
}
}
}