本文最后更新于2021年11月3日,已超过 1 个月没更新!

第二讲 数据结构

表达式求值

import java.util.*;

public class Main {
    static Deque<Integer> num = new LinkedList<>();
    static Deque<Character> op = new LinkedList<>();
    static HashMap<Character, Integer> pr = new HashMap<Character, Integer>(){{
        put('+', 1);
        put('-', 1);
        put('*', 2);
        put('/', 2);
    }};

    public static void eval() {
        int b = num.pop();
        int a = num.pop();
        char c = op.pop();
        int x = 0;
        if (c == '+') x = a + b;
        else if (c == '-') x = a - b;
        else if (c == '*') x = a * b;
        else x = a / b;
        num.push(x);
    }

    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int len = s.length();
        for (int i = 0; i < len; ++ i) {
            char c = s.charAt(i);
            if (Character.isDigit(c)) {
                int x = 0, j = i;
                while (j < len && Character.isDigit(s.charAt(j))) {
                    x = x * 10 + s.charAt(j) - '0';
                    ++ j;
                }
                i = j - 1;
                num.push(x);
            }
            else if (c == '(') op.push(c);
            else if (c == ')') {
                while(op.peek() != '(') eval();
                op.pop();
            }
            else {
                while (!op.isEmpty() && op.peek() != '(' && pr.get(op.peek()) >= pr.get(c)) eval();
                op.push(c);
            }
        }
        while (!op.isEmpty()) eval();
        System.out.println(num.peek());
    }
}

单调栈

方法一

import java.util.*;

public class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] nums = new int[n];
        int[] ans = new int[n];
        for (int i = 0; i < n; ++ i) {
            nums[i] = sc.nextInt();
            ans[i] = -1;
        }
        Deque<Integer> stack = new LinkedList<>();
        for (int i = n - 1; i >= 0; -- i) {
            while (!stack.isEmpty() && nums[i] < nums[stack.peek()]) {
                ans[stack.pop()] = nums[i];
            }
            stack.push(i);
        }
        for (int i = 0; i < n; ++ i) {
            System.out.print(ans[i] + " ");
        }
    }
}

方法二

import java.util.*;

public class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] nums = new int[n];
        int[] ans = new int[n];
        for (int i = 0; i < n; ++ i) {
            nums[i] = sc.nextInt();
            ans[i] = -1;
        }
        Deque<Integer> stack = new LinkedList<>();
        for (int i = 0; i < n; ++ i) {
            while (!stack.isEmpty() && nums[i] <= stack.peek()) stack.pop();
            if (!stack.isEmpty()) ans[i] = stack.peek();
            stack.push(nums[i]);
        }
        for (int i = 0; i < n; ++ i) {
            System.out.print(ans[i] + " ");
        }
    }
}

单调队列

滑动窗口

import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), k = sc.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; ++ i) nums[i] = sc.nextInt();

        int[] ans = new int[n - k + 1];
        Deque<Integer> dq = new LinkedList<>();
        // 形成窗口前
        for (int i = 0; i < k; ++ i) {
            while (!dq.isEmpty() && dq.peekLast() < nums[i])
                dq.pollLast();
            dq.offerLast(nums[i]);
        }
        ans[0] = dq.peekFirst();

        // 形成窗口后
        for (int i = k; i < n; ++ i) {
            if (!dq.isEmpty() && nums[i - k] == dq.peekFirst()) {
                dq.pollFirst();
            }
            while (!dq.isEmpty() && dq.peekLast() < nums[i])
                dq.pollLast();
            dq.offerLast(nums[i]);
            ans[i - k + 1] = dq.peekFirst();
        }

        for (int a : ans) System.out.print(a + " ");
    }
}

KMP

KMP字符串

import java.util.*;
import java.io.*;

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int n = Integer.parseInt(br.readLine());
        char[] p = br.readLine().toCharArray();
        int m = Integer.parseInt(br.readLine());
        char[] s = br.readLine().toCharArray();

        // 求next数组
        int[] next = new int[n];
        for (int i = 1, j = 0; i < n; ++ i) {
            while (j > 0 && p[i] != p[j]) {
                j = next[j - 1];
            }
            if (p[i] == p[j]) ++ j;
            next[i] = j;
        }

        int ans = -1;
        // 匹配
        for (int i = 0, j = 0; i < m; ++ i) {
            while (j > 0 && s[i] != p[j]) {
                j = next[j - 1];
            }
            if (s[i] == p[j]) ++ j;
            if (j == n) {
                bw.write(i - j + 1 + " ");
                j = next[j - 1];
            }
        }
        br.close();
        bw.close();
    }
}

Trie

Trie字符串统计

import java.util.*;
import java.io.*;

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int n = Integer.parseInt(br.readLine());

        Trie trie = new Trie();
        while (n -- > 0) {
            String[] strs = br.readLine().split(" ");
            if ("I".equals(strs[0])) {
                trie.insert(strs[1]);
            } else {
                bw.write(trie.getNum(strs[1])+ "\n");
            }
        }
        br.close();
        bw.close();
    }
}

class Trie {
    TrieNode root;

    public Trie () {
        root = new TrieNode();
    }

    public void insert (String s) {
        TrieNode cur = root;
        for (int i = 0; i < s.length(); ++ i) {
            int c = s.charAt(i) - 'a';
            if (cur.sub[c] == null)
                cur.sub[c] = new TrieNode();
            cur = cur.sub[c];
        }
        cur.isEnd = true;
        cur.num ++;
    }

    public int getNum(String s) {
        TrieNode cur = root;
        int ans = 0;
        for (int i = 0; i < s.length(); ++ i) {
            int c = s.charAt(i) - 'a';
            if (cur.sub[c] == null) return 0;
            cur = cur.sub[c];
        }
        return cur.isEnd ? cur.num : 0;
    }

    class TrieNode {
        TrieNode[] sub = new TrieNode[26];
        int num = 0;
        boolean isEnd = false; // 重要
    }
}

最大异或对

import java.util.*;
import java.io.*;

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int n = Integer.parseInt(br.readLine());
        String[] nums = br.readLine().split(" ");

        Trie trie = new Trie();
        int ans = 0;
        for (int i = 0; i < n; ++ i) {
            int num = Integer.parseInt(nums[i]);
            trie.insert(num);
            ans = Math.max(ans, trie.getXorMax(num));
        }

        bw.write(ans+ "\n");
        br.close();
        bw.close();
    }
}

class Trie {
    TrieNode root;

    public Trie () {
        root = new TrieNode();
    }

    public void insert (int x) {
        TrieNode cur = root;
        for (int i = 30; i >= 0; -- i) {
            int tmp = (x >> i) & 1;
            if (cur.sub[tmp] == null) {
                cur.sub[tmp] = new TrieNode();
            }
            cur = cur.sub[tmp];
        }
    }

    public int getXorMax(int x) {
        TrieNode cur = root;
        int ans = 0;
        for (int i = 30; i >= 0; -- i) {
            int tmp = (x >> i) & 1;
            if (cur.sub[tmp ^ 1] != null) {
                ans = (ans << 1) + 1;
                cur = cur.sub[tmp ^ 1];
            } else {
                ans = ans << 1;
                cur = cur.sub[tmp];
            }
        }
        return ans;
    }

    class TrieNode {
        TrieNode[] sub = new TrieNode[2];
    }
}

并查集

  • 处理问题:
    • 将两个集合合并
    • 询问两个元素是否在一个集合中
  • 基本原理:
    • 每个集合用一棵树表示。
    • 树根编号就是整个集合的编号。
    • 每个节点储存它的父节点,p[x]表示x的父节点
  • 问题:
    1. 如何判断树根: if (p[x] = x) x = p[x];
    2. 如何求x的集合编号:while (p[x] != x) x = p[x];
    3. 如何合并两个集合:p[x]是x的集合编号,p[y]是y的集合编号。p[x] = y

合并集合

import java.util.*;
import java.io.*;

public class Main {
    static int N = 100010;
    static int[] p = new int[N];
    public static void main (String[] args) throws IOException{

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        String[] s1 = br.readLine().split(" ");
        int n = Integer.parseInt(s1[0]);
        int m = Integer.parseInt(s1[1]);
        while (n-- > 0) p[n] = n;
        while (m-- > 0) {
            String[] s = br.readLine().split(" ");
            int a = Integer.parseInt(s[1]), b = Integer.parseInt(s[2]);
            if ("M".equals(s[0])) {
                p[find(a)] = find(b);
            } else {
                if (find(a) == find(b)) {
                    bw.write("Yes\n");
                } else {
                    bw.write("No\n");
                }
            }
        }
        br.close();
        bw.close();
    }

    private static int find(int x) {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }
}

连通块中点的数量

import java.util.*;
import java.io.*;

public class Main {
    static int[] p;
    public static void main (String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s1 = br.readLine().split(" ");
        int n = Integer.parseInt(s1[0]), m = Integer.parseInt(s1[1]);
        p = new int[n + 1];
        int[] size = new int[n + 1];
        for (int i = 1; i <= n; ++ i) {
            p[i] = i;
            size[i] = 1;
        }
        while (m-- > 0) {
            String[] s = br.readLine().split(" ");
            int a = Integer.parseInt(s[1]);
            if("C".equals(s[0])) {
                int b = Integer.parseInt(s[2]);
                int x = find(a), y = find(b);
                // 如果父节点不在一个集合中,进行合并
                if (x != y) {
                    p[x] = y;
                    size[y] += size[x];
                }
            } else if ("Q1".equals(s[0])) {
                int b = Integer.parseInt(s[2]);
                if (find(a) == find(b)) bw.write("Yes\n");
                else bw.write("No\n");
            } else {
                bw.write(size[find(a)] + "\n");
            }
        }
        bw.close();
        br.close();
    }

    private static int find (int x) {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }
}

食物链

import java.util.*;
import java.io.*;

public class Main {
    static int[] p;
    static int[] d; // 到根节点的距离
    public static void main (String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s1 = br.readLine().split(" ");
        int n = Integer.parseInt(s1[0]);
        int k = Integer.parseInt(s1[1]);
        p = new int[n + 1];
        d = new int[n + 1];
        for (int i = 1; i <= n; ++ i) p[i] = i;
        int ans = 0;
        for (int i = 0; i < k; ++ i) {
            String[] s = br.readLine().split(" ");
            int x = Integer.parseInt(s[1]), y = Integer.parseInt(s[2]);
            if (x <= n && y <= n) {
                int px = find(x), py = find(y);
                if ("1".equals(s[0])) {
                    if (px == py) {
                        if ((d[x] - d[y]) % 3 != 0) ++ans;
                    } else {
                        p[px] = py;
                        d[px] = d[y] - d[x];
                    }
                } else {
                    if (px == py) {
                        if ((d[x] - d[y] - 1) % 3 != 0) ++ ans;
                    } else {
                        p[px] = py;
                        d[px] = d[y] - d[x] + 1;
                    }
                }
            } else {
                ++ ans;
            }
        }
        bw.write(ans + "\n");
        bw.close();
        br.close();
    }

    private static int find (int x) {
        if (p[x] != x) {
            int t = find(p[x]);
            d[x] += d[p[x]];
            p[x] = t;
        }
        return p[x];
    }
}

哈希表

  • 存储结构
    • 开放地址法
    • 拉链法

模拟散列表

import java.util.*;


public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = Integer.parseInt(sc.nextLine());
        MyHashSet set = new MyHashSet();
        while (t -- > 0) {
            String[] q = sc.nextLine().split(" ");
            if (q[0].equals("I")) set.add(Integer.parseInt(q[1]));
            else {
                if (set.contains(Integer.parseInt(q[1])))
                    System.out.println("Yes");
                else System.out.println("No");
            }

        }
    }
}

class MyHashSet{
    private static final int BASE = 769;
    private LinkedList[] data;

    public MyHashSet() {
        data = new LinkedList[BASE];
        for (int i = 0; i < BASE; ++ i) {
            data[i] = new LinkedList<Integer>();
        }
    }

    public void add(int key) {
        int h = hash(key);
        if (!data[h].contains(key))
            data[h].add(key);
    }

    public boolean contains(int key) {
        int h = hash(key);
        return data[h].contains(key);
    }

    private int hash(int key) {
        key = key > 0 ? key : -key;
        return key % BASE;
    }
}

字符串哈希

import java.util.*;
import java.io.*;

// p = 131 or 13331 再试其他值
// Q = 2 ^ 64
// 99.99%都不会出现冲突
public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] vals = br.readLine().split(" ");
        int n = Integer.parseInt(vals[0]), m = Integer.parseInt(vals[1]);
        String s = br.readLine();
        hash(s);
        while (m -- > 0) {
            vals = br.readLine().split("\\s+");
            int l1 = Integer.parseInt(vals[0]);
            int r1 = Integer.parseInt(vals[1]);
            int l2 = Integer.parseInt(vals[2]);
            int r2 = Integer.parseInt(vals[3]);
            if (get(l1, r1) == get(l2, r2)) bw.write("Yes\n");
            else bw.write("No\n");
        }
        br.close();
        bw.close();
    }

    static long[] h, p; // p[i]存储base的i次方
    static int base = 131;
    static long mod = Long.MAX_VALUE;

    private static void hash(String s) {
        int n = s.length();
        p = new long[n + 1];
        h = new long[n + 1];
        p[0] = 1;
        for (int i = 1; i <= n; ++ i) {
            p[i] = p[i - 1] * base;
            h[i] = (h[i - 1] * base + (s.charAt(i - 1) - 'a' + 1)) % mod;
        }
    }

    private static long get(int l, int r) {
        return h[r] - h[l - 1] * p[r - l + 1]; 
    }
}

扩展题型: 最长公共子路径


Try and fail, but don't fail to try.