华为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();
    }
全部评论

相关推荐

09-29 17:59
已编辑
哈尔滨工程大学 C++
宁德新能源 视觉软件开发 税前28
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
09-29 19:22
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务