第一行是一个整数T(1 ≤ T ≤ 100)表示测试样例数;接下来T行,每行给定一个分身后的电话号码的分身(长度在3到10000之间)。
输出T行,分别对应输入中每行字符串对应的分身前的最小电话号码(允许前导0)。
4 EIGHT ZEROTWOONE OHWETENRTEO OHEWTIEGTHENRTEO
0 234 345 0345
1 import java.util.ArrayList; 2 import java.util.Arrays; 3 import java.util.HashMap; 4 import java.util.List; 5 import java.util.Map; 6 import java.util.Scanner; 7 import java.util.Set; 8 9 /** 10 * 11 * 电话号 分身 12 * 13 * @author Dell 14 * 15 */ 16 public class Main { 17 18 static public Map mapping = new HashMap<String,String>(); 19 static { 20 // 由于要加8 所以 数字对应错位 21 mapping.put("ZERO", "2"); 22 mapping.put("ONE", "3"); 23 mapping.put("TWO", "4"); 24 mapping.put("THREE", "5"); 25 mapping.put("FOUR", "6"); 26 mapping.put("FIVE", "7"); 27 mapping.put("SIX", "8"); 28 mapping.put("SEVENT", "9"); 29 mapping.put("EIGHT", "0"); 30 mapping.put("NINE", "1"); 31 32 } 33 34 /** 35 * 分批 根据 特殊字符进行 分析 36 * @param res 37 */ 38 static public String analysisedStr = ""; //存储分析结果 39 // 1、 ZERO(Z) TWO(W) FOUR(U) SIX(X) EIGHT(G) 40 static public String analysis1(String s) { 41 while(true) { 42 if (s.contains("X")) { 43 s=s.replaceFirst("S",""); 44 s=s.replaceFirst("I",""); 45 s=s.replaceFirst("X",""); 46 analysisedStr += "8"; 47 } 48 if (s.contains("U")) { 49 s=s.replaceFirst("F",""); 50 s=s.replaceFirst("O",""); 51 s=s.replaceFirst("U",""); 52 s=s.replaceFirst("R",""); 53 analysisedStr += "6"; 54 } 55 if (s.contains("W")) { 56 s=s.replaceFirst("T",""); 57 s=s.replaceFirst("W",""); 58 s=s.replaceFirst("O",""); 59 analysisedStr += "4"; 60 } 61 62 if (s.contains("Z")) { 63 s=s.replaceFirst("Z",""); 64 s=s.replaceFirst("E",""); 65 s=s.replaceFirst("R",""); 66 s=s.replaceFirst("O",""); 67 analysisedStr += "2"; 68 } 69 if (s.contains("G")) { 70 s=s.replaceFirst("E",""); 71 s=s.replaceFirst("I",""); 72 s=s.replaceFirst("G",""); 73 s=s.replaceFirst("H",""); 74 s=s.replaceFirst("T",""); 75 analysisedStr += "0"; 76 } 77 if (!s.contains("U")&&!s.contains("X") 78 &&!s.contains("W")&&!s.contains("G")&&!s.contains("Z")) { 79 return s; 80 } 81 } 82 } 83 // 2、 THREE(T) FIVE(F) SEVEN(S) 84 static public String analysis2(String s) { 85 while(true) { 86 if (s.contains("T")) { 87 s=s.replaceFirst("T",""); 88 s=s.replaceFirst("H",""); 89 s=s.replaceFirst("R",""); 90 s=s.replaceFirst("E",""); 91 s=s.replaceFirst("E",""); 92 analysisedStr += "5"; 93 } 94 if (s.contains("F")) { 95 s=s.replaceFirst("F",""); 96 s=s.replaceFirst("I",""); 97 s=s.replaceFirst("V",""); 98 s=s.replaceFirst("E",""); 99 analysisedStr += "7"; 100 } 101 if (s.contains("S")) { 102 s=s.replaceFirst("S",""); 103 s=s.replaceFirst("E",""); 104 s=s.replaceFirst("V",""); 105 s=s.replaceFirst("E",""); 106 s=s.replaceFirst("N",""); 107 analysisedStr += "9"; 108 } 109 if (!s.contains("T")&&!s.contains("F") 110 &&!s.contains("S")) { 111 return s; 112 } 113 } 114 } 115 // 3、 ONE(O) NINE(I) 116 static public String analysis3(String s) { 117 while(true) { 118 if (s.contains("O")) { 119 s=s.replaceFirst("O",""); 120 s=s.replaceFirst("N",""); 121 s=s.replaceFirst("E",""); 122 analysisedStr += "3"; 123 } 124 if (s.contains("I")) { 125 s=s.replaceFirst("N",""); 126 s=s.replaceFirst("I",""); 127 s=s.replaceFirst("N",""); 128 s=s.replaceFirst("E",""); 129 analysisedStr += "1"; 130 } 131 if (!s.contains("O")&&!s.contains("I")) { 132 return s; 133 } 134 } 135 } 136 static public String analysis(String s) { 137 analysisedStr = ""; 138 s =analysis1(s); 139 s =analysis2(s); 140 s =analysis3(s); 141 return analysisedStr; 142 } 143 public static void main(String[] args) { 144 // 输入 145 Scanner sc = new Scanner(System.in); 146 int n = Integer.parseInt(sc.nextLine()); 147 String[] ns = new String[n]; 148 for (int i = 0; i < n; i++) { 149 ns[i] = sc.nextLine(); 150 } 151 // // 替换 152 for (int i = 0; i < ns.length; i++) { 153 ns[i] = analysis(ns[i]); 154 } 155 //排序 156 for (int i = 0; i < ns.length; i++) { 157 char[] cs = ns[i].toCharArray(); 158 Arrays.sort(cs); 159 ns[i] = new String(cs); 160 } 161 // 输出 162 for (int i = 0; i < ns.length; i++) { 163 System.out.println(ns[i]); 164 } 165 } 166 } 167
原文:https://www.cnblogs.com/the-wang/p/8981622.html