[Java] Name That Number
程序员文章站
2022-03-27 08:54:09
/* Use the slash-star style comments or the system won't see your identification information *//*ID: lincans1LANG: JAVATASK: namenum*/import java.io.*;import java.util.*;public class namenum {private String number;private Map
/* Use the slash-star style comments or the system won't see your
identification information */
/*
ID: lincans1
LANG: JAVA
TASK: namenum
*/
import java.io.*;
import java.util.*;
public class namenum {
private String number;
private Map<Character, String> map;
private Set<String> set;
private boolean flag;
private void dfs(int i, StringBuilder b, PrintWriter out) {
if (i == number.length()) {
if (set.contains(b.toString())) {
flag = false;
out.println(b.toString());
}
return;
}
for (char ch : map.get(number.charAt(i)).toCharArray()) {
b.setCharAt(i, ch);
dfs(i + 1, b, out);
}
}
public namenum() throws IOException {
map = new HashMap<>();
map.put('2', "ABC");
map.put('3', "DEF");
map.put('4', "GHI");
map.put('5', "JKL");
map.put('6', "MNO");
map.put('7', "PRS");
map.put('8', "TUV");
map.put('9', "WXY");
// Use BufferedReader rather than RandomAccessFile; it's much faster
BufferedReader f = new BufferedReader(new FileReader("namenum.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("namenum.out")));
this.number = f.readLine();
set = new HashSet<>();
BufferedReader reader = new BufferedReader(new FileReader("dict.txt"));
String word = null;
while ((word = reader.readLine()) != null) {
if (word.length() == number.length()) {
set.add(word);
}
}
flag = true;
dfs(0, new StringBuilder(number), out);
if (flag) {
out.println("NONE");
}
out.close(); // close the output file
}
public static void main (String [] args) throws IOException {
new namenum();
}
}
本文地址:https://blog.csdn.net/weixin_41714373/article/details/111992674
下一篇: 安卓问题