-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnamenum.java
More file actions
63 lines (61 loc) · 1.81 KB
/
namenum.java
File metadata and controls
63 lines (61 loc) · 1.81 KB
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
/*
ID: mryuan01
LANG: JAVA
TASK: namenum
*/
public class namenum {
public static String[] map = {"", "", "ABC", "DEF","GHI", "JKL","MNO","PRS","TUV","WXY"};
private static HashSet<String> hs = new HashSet<String>();
public static boolean found = false;
static{
try {
BufferedReader f = new BufferedReader(new FileReader("dict.txt"));
String key = null;
while((key = f.readLine()) != null){
hs.add(key.trim());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void findName(String num, int start, StringBuffer sb, PrintWriter out){
if(start == num.length()){
String name = sb.toString();
if(hs.contains(name)){
out.println(name);
found = true;
}
return ;
}
int len = sb.length();
String s1 = map[num.charAt(start) - '0'];
for(int i = 0; i < s1.length(); i++ ){
sb.append(s1.charAt(i));
findName(num, start+1, sb, out);
sb.deleteCharAt(len);
}
}
public static void main (String [] args) throws IOException {
// Use BufferedReader rather than RandomAccessFile; it's much faster
BufferedReader f = new BufferedReader(new FileReader("namenum.in"));
// input file name goes above
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("namenum.out")));
// Use StringTokenizer vs. readLine/split -- lots faster
String s = f.readLine().trim();
StringBuffer sb = new StringBuffer();
findName(s, 0, sb, out);
if(!found){
out.println("NONE");
}
out.close();
}
}