美文网首页
2022-08-10

2022-08-10

作者: 小金hhh | 来源:发表于2022-08-09 20:44 被阅读0次

    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Scanner;
    import java.util.Set;
    public class Solution {
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    try {
    while (scanner.hasNext()){
    int opsNum = Integer.parseInt(scanner.nextLine());
    HashMap<String, HashSet<String>> guToName = new HashMap<>();
    HashMap<String,HashSet<String>> nameToGu = new HashMap<>();
    for (int i=0;i<opsNum;i++){
    String str = scanner.nextLine();
    // 注册
    if (str.charAt(0) == '1'){
    String[] arrStrs = str.split(" ");
    String[] gStr = scanner.nextLine().split(" ");
    for (String s : gStr){
    if (!nameToGu.containsKey(arrStrs[1])){
    nameToGu.put(arrStrs[1],new HashSet<>());
    }
    nameToGu.get(arrStrs[1]).add(s);
    if (!guToName.containsKey(s)){
    guToName.put(s, new HashSet<>());
    }
    guToName.get(s).add(arrStrs[1]);
    }
    }
    // 查询
    if (str.charAt(0) == '2'){
    String name = str.split(" ")[1];
    if (!nameToGu.containsKey(name)){
    System.out.println("error");
    }else {
    int ans = 0;
    boolean theOne = false;
    Set<String> gupiao1 = nameToGu.get(name);
    for (String s : gupiao1){
    Set<String> name1 = guToName.get(s);
    for (String n : name1){
    if (!n.equals(name)){
    Set<String> gu = nameToGu.get(n);
    for (String g : gu){
    if (!gupiao1.contains(g)){
    ans++;
    }
    }
    }
    }
    }
    System.out.println(ans);
    }
    }
    }
    }
    }catch (Exception e){
    e.printStackTrace();
    }
    }
    }

    相关文章

      网友评论

          本文标题:2022-08-10

          本文链接:https://www.haomeiwen.com/subject/sziawrtx.html