华为OD机试真题 - 快速人名查找

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String[] namea = sc.nextLine().split(",");
        String abbr = sc.nextLine();

        System.out.println(getNames(namea, abbr));
    }

    public static List getNames(String[] nameall, String word) {
        List<String> ans = new ArrayList<>();
        for (int i = 0; i < nameall.length; i++) {
            if (isSaftty(nameall[i], word))
                ans.add(nameall[i]);
        }
        return ans;
    }

    public static boolean isSaftty(String name, String word) {
        String[] names = name.split( &quot; &quot;);
        int index = 0, right = 0, i = 1;
        while (right + i < word.length()) {
            if (dfs(names, word, index, right, right + i))
                return true;
            i++;
        }
        return false;
    }

    public static boolean dfs(String[] names, String word, int index,int left,int right) {
        if (index > names.length - 1 &amp;&amp; left > word.length())
            return true;
        if (index > names.length - 1 || right > word.length())
            return false;
        String s = word.substring(left, right);
        if (names[index].startsWith(s)) {
            if (index == names.length - 1 &amp;&amp; right == word.length())
                return true;
            int i = 1;
            while (right + i <= word.length()) {
                if (dfs(names, word, index + 1, right, right + i))
                    return true;
                i++;
            }
            return false;
        } else
            return false;
    }
全部评论

相关推荐

宇算唯航:目测实缴资本不超100W的小公司
点赞 评论 收藏
分享
06-07 17:17
嘉兴学院 教师
心爱的idea:你孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务