1 条题解
-
0
java题解:
import java.io.*; import java.util.*; public class Main { private static String[] tokens; private static int pos; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input = br.readLine(); tokens = input.split(" "); pos = 0; Node root = parseNode(); Map<String, Integer> requiredKeywords = root.calc(); // 按字母顺序排序关键字 List<String> sortedKeys = new ArrayList<>(requiredKeywords.keySet()); Collections.sort(sortedKeys); // 构建输出 StringBuilder keywordOutput = new StringBuilder(); StringBuilder countOutput = new StringBuilder(); for (String key : sortedKeys) { int count = requiredKeywords.get(key); keywordOutput.append(key).append(" "); countOutput.append(count).append(" "); } // 去除末尾空格 if (keywordOutput.length() > 0) { keywordOutput.setLength(keywordOutput.length() - 1); } if (countOutput.length() > 0) { countOutput.setLength(countOutput.length() - 1); } System.out.println(keywordOutput.toString()); System.out.println(countOutput.toString()); } // 基本节点接口 private interface Node { Map<String, Integer> calc(); } // 关键字节点 private static class TokenNode implements Node { private final String token; public TokenNode(String token) { this.token = token; } @Override public Map<String, Integer> calc() { Map<String, Integer> result = new HashMap<>(); result.put(token, 1); return result; } } // 序列节点(包含多个子节点) private static class SequenceNode implements Node { private final List<Node> children = new ArrayList<>(); @Override public Map<String, Integer> calc() { Map<String, Integer> result = new HashMap<>(); for (Node child : children) { Map<String, Integer> childResult = child.calc(); for (Map.Entry<String, Integer> entry : childResult.entrySet()) { String key = entry.getKey(); int value = entry.getValue(); result.put(key, result.getOrDefault(key, 0) + value); } } return result; } } // 分支节点(包含多个选项) private static class BranchNode implements Node { private final boolean required; private final List<Node> options = new ArrayList<>(); public BranchNode(boolean required) { this.required = required; } @Override public Map<String, Integer> calc() { List<Map<String, Integer>> maps = new ArrayList<>(); for (Node option : options) { maps.add(option.calc()); } // 如果是可选分支,添加一个空选项 if (!required) { maps.add(new HashMap<>()); } // 找出所有关键字 Set<String> allKeys = new HashSet<>(); for (Map<String, Integer> map : maps) { allKeys.addAll(map.keySet()); } Map<String, Integer> result = new HashMap<>(); for (String key : allKeys) { // 取所有选项中关键字出现次数的最小值 int min = Integer.MAX_VALUE; for (Map<String, Integer> map : maps) { min = Math.min(min, map.getOrDefault(key, 0)); } if (min > 0) { result.put(key, min); } } return result; } } // 解析分支节点 private static Node parseBranch() { boolean required = tokens[pos].equals("{"); pos++; BranchNode branchNode = new BranchNode(required); String closeBracket = required ? "}" : "]"; while (!tokens[pos].equals(closeBracket)) { SequenceNode seqNode = new SequenceNode(); while (!tokens[pos].equals("|") && !tokens[pos].equals(closeBracket)) { if (tokens[pos].equals("{") || tokens[pos].equals("[")) { seqNode.children.add(parseBranch()); } else { seqNode.children.add(new TokenNode(tokens[pos])); pos++; } } branchNode.options.add(seqNode); if (tokens[pos].equals("|")) { pos++; } } pos++; // 跳过闭合括号 return branchNode; } // 解析顶层节点 private static Node parseNode() { SequenceNode seqNode = new SequenceNode(); while (pos < tokens.length) { if (tokens[pos].equals("{") || tokens[pos].equals("[")) { seqNode.children.add(parseBranch()); } else if (tokens[pos].equals("}") || tokens[pos].equals("|") || tokens[pos].equals("]")) { break; } else { seqNode.children.add(new TokenNode(tokens[pos])); pos++; } } return seqNode; } }
- 1
信息
- ID
- 14
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 6
- 标签
- 递交数
- 2
- 已通过
- 1
- 上传者