1 条题解
-
0
JAVA题解:
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); // 存储不同掩码长度的前缀集合 Set<Long>[] prefixSets = new HashSet[49]; for (int i = 0; i < 49; i++) { prefixSets[i] = new HashSet<>(); } boolean hasZeroMask = false; // 处理VIP MAC地址 for (int i = 0; i < n; i++) { String line = br.readLine().trim(); String[] parts = line.split("/"); String macPart = parts[0]; int maskLen = Integer.parseInt(parts[1]); long macInt = parseMacToLong(macPart); if (maskLen == 0) { hasZeroMask = true; prefixSets[0].add(0L); } else { int shift = 48 - maskLen; long prefix = macInt >> shift; prefixSets[maskLen].add(prefix); } } // 处理查询 int m = Integer.parseInt(br.readLine().trim()); for (int i = 0; i < m; i++) { String query = br.readLine().trim(); long queryInt = parseMacToLong(query); boolean matched = false; if (hasZeroMask) { matched = true; } else { // 遍历所有掩码长度 for (int L = 1; L <= 48; L++) { if (prefixSets[L].isEmpty()) { continue; } int shift = 48 - L; long queryPrefix = queryInt >> shift; if (prefixSets[L].contains(queryPrefix)) { matched = true; break; } } } System.out.println(matched ? "YES" : "NO"); } } /** * 将MAC地址字符串转换为长整型 */ private static long parseMacToLong(String macStr) { String[] parts = macStr.split("-"); long macLong = 0; for (String part : parts) { macLong = (macLong << 8) | Integer.parseInt(part, 16); } return macLong; } /** * 将长整型转换为MAC地址字符串 */ private static String longToMacStr(long macLong) { StringBuilder sb = new StringBuilder(); for (int i = 5; i >= 0; i--) { int part = (int)((macLong >> (i * 8)) & 0xFF); sb.append(String.format("%02x", part)); if (i > 0) { sb.append("-"); } } return sb.toString(); } /** * 判断MAC地址是否匹配指定前缀和掩码长度 */ private static boolean isMacMatch(long macLong, long prefixLong, int maskLen) { if (maskLen == 0) { return true; } int shift = 48 - maskLen; long macPrefix = macLong >> shift; return macPrefix == prefixLong; } /** * 生成随机MAC地址 */ private static String generateRandomMac() { Random random = new Random(); long macLong = random.nextLong() & ((1L << 48) - 1); // 确保只有48位 return longToMacStr(macLong); } /** * 解决MAC地址匹配问题 */ public static List<String> solve(List<MacMask> vipMacs, List<String> queries) { // 存储不同掩码长度的前缀集合 Set<Long>[] prefixSets = new HashSet[49]; for (int i = 0; i < 49; i++) { prefixSets[i] = new HashSet<>(); } boolean hasZeroMask = false; // 处理VIP MAC地址 for (MacMask macMask : vipMacs) { long macLong = parseMacToLong(macMask.mac); int maskLen = macMask.maskLen; if (maskLen == 0) { hasZeroMask = true; prefixSets[0].add(0L); } else { int shift = 48 - maskLen; long prefix = macLong >> shift; prefixSets[maskLen].add(prefix); } } // 处理查询 List<String> results = new ArrayList<>(); for (String query : queries) { long queryLong = parseMacToLong(query); boolean matched = false; if (hasZeroMask) { matched = true; } else { // 遍历所有掩码长度 for (int L = 1; L <= 48; L++) { if (prefixSets[L].isEmpty()) { continue; } int shift = 48 - L; long queryPrefix = queryLong >> shift; if (prefixSets[L].contains(queryPrefix)) { matched = true; break; } } } results.add(matched ? "YES" : "NO"); } return results; } /** * MAC地址和掩码长度的数据结构 */ static class MacMask { String mac; int maskLen; public MacMask(String mac, int maskLen) { this.mac = mac; this.maskLen = maskLen; } } }
- 1
信息
- ID
- 27
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 7
- 标签
- 递交数
- 3
- 已通过
- 1
- 上传者