美团9.3笔试字母树Java

public class CharacterTree{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] num = new int[n - 1];
        int[] ans = new int[n];
        ArrayList<Integer>[] nm = new ArrayList[n];
        for(int i = 0; i < n ;i++){
            nm[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i < n - 1; i++) {
            num[i] = scanner.nextInt();
            nm[num[i] - 1].add(Integer.valueOf(i + 1));
        }
        scanner.nextLine();
        String string = scanner.nextLine();
        for (int i = 0; i < n; i++) {
            Queue<Integer> queue = new LinkedList<>();
            queue.add(i);
            Set<Character> set = new HashSet<>();
            int count = 0;
            while (!queue.isEmpty()){
                int top = queue.peek();
                queue.poll();
                if(!set.contains(string.charAt(top))){
                    count++;
                    set.add(string.charAt(top));
                }
                for (Integer j : nm[top]){
                    queue.add(j);
                }
            }
            ans[i] = count;
        }
        for (int i = 0; i < n; i++) {
            System.out.println(ans[i]);
        }
    }
}
#美团笔试#
全部评论
大佬,你这题全a了ma
点赞 回复 分享
发布于 2022-09-05 16:43 上海
hi~同学,秋招遇“寒气”,牛客送温暖啦!23届秋招笔面经有奖征集中,参与就得牛客会员7天免费体验,最高赢300元京东卡!戳我去看>>>https://www.nowcoder.com/link/zhengjipinglun
点赞 回复 分享
发布于 2022-09-05 12:38 北京
{"pureText":"","imgs":[{"alt":"discuss_166****548117.jpeg","height":1623,"localSrc":"content://media/external/images/media/138082","src":"https://uploadfiles.nowcoder.com/message_images/20220905/165664256_1662345547451/discuss_1662345548117.jpeg","width":960}]}
点赞 回复 分享
发布于 2022-09-05 10:39 北京

相关推荐

不愿透露姓名的神秘牛友
07-08 12:10
点赞 评论 收藏
分享
这算盘打的
程序员小白条:都这样的,都是潜规则,你自己说可以实习一年就行了,实习可以随便跑路的
点赞 评论 收藏
分享
人力小鱼姐:实习经历没有什么含金量,咖啡店员迎宾这种就别写了,其他两段包装一下 想找人力相关的话,总结一下个人优势,结合校园经历里有相关性的部分,加一段自我评价
点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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