AcWing
  • 首页
  • 课程
  • 题库
  • 更多
    • 竞赛
    • 题解
    • 分享
    • 问答
    • 应用
    • 校园
  • 关闭
    历史记录
    清除记录
    猜你想搜
    AcWing热点
  • App
  • 登录/注册

2025.5.8 某为机试题 文件目录

作者: 作者的头像   小彭要奋斗 ,  2025-05-08 23:50:08 · 广东 ,  所有人可见 ,  阅读 3


0


import java.util.*;

class Main(){

    public static List<String> split(String str, String c){
        return Arrays.asList(str.split(c));
    }

    public static int dfs(String id, Map<String, List<String>> childMap, Map<String, Integer> directorySize){
        int res = directorySize.getOrDefault(id, 0);
        List<String> childs = childMap.getOrDefault(id, new ArrayList<>());
        for(String s : childs){
            res += dfs(s, childMap, directorySize);
        }

        return res;
    }

    public static void main(String[] argv){
        Scanner in = new Scanner(System.in);
        int m = in.nextInt();
        int n = in.nextInt();
        in.nextLine();

        Map<String, List<String>> childMap = new HashMap<>();
        Map<String, Integer> directorySize = new HashMap<>();

        for(int i = 0;i < m;i++){
            String[] param = in.nextLine().split(" ");
            String id = param[0];
            int size = Integer.parseInt(param[1]);
            directorySize.put(id, size);

            if(param[2].length(0) == 2) continue;

            String childstr = param[2].substring(1, param[2].length() - 1);
            List<String> childList = childstr.isEmpty() ? new ArrayList<>() : param[2].split(",");
            childMap.put(id, childList);
        }
        Sytem.out.println(dfs(String.value(n), childMap, directorySize));
    }
}

0 评论

App 内打开
你确定删除吗?
1024
x

© 2018-2025 AcWing 版权所有  |  京ICP备2021015969号-2
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息