华为OD机试真题 - 中文分词模拟器
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] str = new String[2];
int i = 0;
while (in.hasNextLine()) {
str[i] = in.nextLine();
i++;
}
String[] litter = str[1].split(",");
String[] words = str[0].split(",");
System.out.println(getResult(litter, words));
}
public static String getResult(String[] litter, String[] words) {
LinkedHashMap hashMap = new LinkedHashMap();
// 如果里面放入map 可以模拟输入法
// LinkedHashMap hashMap = new LinkedHashMap();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < litter.length; i++) {
if (hashMap.get(litter[i].charAt(0)) != null) {
hashMap.get(litter[i].charAt(0)).add(litter[i]);
} else {
ArrayList arrayList = new ArrayList();
arrayList.add(litter[i]);
hashMap.put(litter[i].charAt(0),arrayList);
}
}
for (int i = 0; i < words.length; i++) {
String temp = null;
ArrayList path = new ArrayList<>();
for (int j = 0; j < words[i].length();) {
List list = hashMap.get(words[i].charAt(j));
if (list == null) {
if (temp != null) {
System.out.println(stringBuilder.length() - temp.length() + " " + stringBuilder.length());
stringBuilder.delete(stringBuilder.length() - temp.length() - 1, stringBuilder.length());
j -= temp.length();
continue;
}
stringBuilder.append(words[i].charAt(j));
}
else {
boolean has = false;
for (int k = 0; k < list.size(); k++) {
String str = (String) list.get(k);
if (words[i].length() - j >= str.length()) {
temp = words[i].substring(j, str.length() + j);
if (temp.contains(str) && !path.contains(str)) {
j += str.length();
path.add(str);
stringBuilder.append(str + ",");
has = true;
break;
}
}
}
if (!has) {
if (temp != null) {
System.out.println(stringBuilder.length() - temp.length() + " " + stringBuilder.length());
stringBuilder.delete(stringBuilder.length() - temp.length() - 1, stringBuilder.length());
j -= temp.length();
}
}
}
}
}
stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
return stringBuilder.toString();
}
Scanner in = new Scanner(System.in);
String[] str = new String[2];
int i = 0;
while (in.hasNextLine()) {
str[i] = in.nextLine();
i++;
}
String[] litter = str[1].split(",");
String[] words = str[0].split(",");
System.out.println(getResult(litter, words));
}
public static String getResult(String[] litter, String[] words) {
LinkedHashMap
// 如果里面放入map 可以模拟输入法
// LinkedHashMap
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < litter.length; i++) {
if (hashMap.get(litter[i].charAt(0)) != null) {
hashMap.get(litter[i].charAt(0)).add(litter[i]);
} else {
ArrayList arrayList = new ArrayList();
arrayList.add(litter[i]);
hashMap.put(litter[i].charAt(0),arrayList);
}
}
for (int i = 0; i < words.length; i++) {
String temp = null;
ArrayList path = new ArrayList<>();
for (int j = 0; j < words[i].length();) {
List list = hashMap.get(words[i].charAt(j));
if (list == null) {
if (temp != null) {
System.out.println(stringBuilder.length() - temp.length() + " " + stringBuilder.length());
stringBuilder.delete(stringBuilder.length() - temp.length() - 1, stringBuilder.length());
j -= temp.length();
continue;
}
stringBuilder.append(words[i].charAt(j));
}
else {
boolean has = false;
for (int k = 0; k < list.size(); k++) {
String str = (String) list.get(k);
if (words[i].length() - j >= str.length()) {
temp = words[i].substring(j, str.length() + j);
if (temp.contains(str) && !path.contains(str)) {
j += str.length();
path.add(str);
stringBuilder.append(str + ",");
has = true;
break;
}
}
}
if (!has) {
if (temp != null) {
System.out.println(stringBuilder.length() - temp.length() + " " + stringBuilder.length());
stringBuilder.delete(stringBuilder.length() - temp.length() - 1, stringBuilder.length());
j -= temp.length();
}
}
}
}
}
stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
return stringBuilder.toString();
}
全部评论
相关推荐