第十四届蓝桥杯三月真题刷题训练——第 16 天 (3.19)

serein_2216 2023-03-19 17:16:13

英文字母

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        int n = scan.nextInt();
        System.out.print((char) (n - 1 + 'A'));
        scan.close();
    }
}

单词分析

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        String word = scan.next();
        int[] arr = new int[26];
        int max = 0;
        for(int i = 0; i < word.length(); i++) {
          int idx = word.charAt(i) - 'a';
          arr[idx]++;
          max = Math.max(max, arr[idx]);
        }
        for(int i = 0; i < 26; i++) {
          if(arr[i] == max) {
            System.out.println((char) (i + 'a'));
            System.out.println(arr[i]);
            break;
          }
        }
        scan.close();
    }
}

 

推导部分和

看题解摸爬滚打的写 

带权并查集:https://www.cnblogs.com/CodingShark/p/17031673.html

import java.util.*;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        int q = scan.nextInt();

        UnionFind uf = new UnionFind(n);

        for(int i = 0; i < m; i++) {
          int l = scan.nextInt() - 1;
          int r = scan.nextInt();
          long s = scan.nextLong();
          uf.union(l, r, s);
        }

        for(int i = 0; i < q; i++) {
          int l = scan.nextInt() - 1;
          int r = scan.nextInt();
          int rootL = uf.find(l);
          int rootR = uf.find(r);
          // 同一集合,就能计算出结果
          if(rootL == rootR) {
            System.out.println(uf.value[l] - uf.value[r]);
          } else {
            System.out.println("UNKNOWN");
          }
        }

        scan.close();
    }
    // 带权并查集
    static class UnionFind {
      int[] root; // 存储每个节点的根节点,即 2-0-1, 1的根节点为2
      long[] value; // 表示i到其根节点的路径长度

      public UnionFind(int n) {
        root = new int[n + 1];
        value = new long[n + 1];
        for(int i = 0; i <= n; i++) {
          root[i] = i;
        }
      }

      public int find(int x) {
        if(x != root[x]) {
          int tmp = root[x];
          // 路径压缩优化,如 2-1-0, 本来0的父节点是1, 经过路径压缩优化后,父节点则变成根节点2
          root[x] = find(root[x]); // 寻找根节点
          value[x] += value[tmp];
        }
        return root[x];
      }

      public void union(int x, int y, long s) {
        int rootX = find(x);
        int rootY = find(y);
        if(rootX != rootY) {
          // 合并l和r的集合,并更新权值
          // 合并规则,将小根节点集合指向大根节点集合        
          int small = rootX > rootY ? rootY : rootX;
          int big = small == rootX ? rootY : rootX;
          root[small] = big;
          // 向量知识
          value[small] = Math.abs(value[y] - value[x] + s);
        }
      }
    }
}

 

...全文
6 回复 打赏 收藏 举报
写回复
回复
切换为时间正序
请发表友善的回复…
发表回复
发帖
高校算法学习社区

4.3w+

社区成员

和众多高校算法内卷分子,一起学习和交流算法那。浓郁的算法交流氛围,拒绝躺平,有效内卷。加入我们,私信我拉你入核心内卷群。
算法数据结构leetcode 个人社区
社区管理员
  • 执 梗
  • Dream-Y.ocean
  • ღCauchyོꦿ࿐
加入社区
帖子事件
创建了帖子
2023-03-19 17:16
社区公告

 刷题!