1. 数组模拟链表
// 用数组模拟单链表
import java.util.*;
public class Main {
// head表示头节点下标
// e[i]表示节点i的值
// ne[i]表示节点i的next指针是多少
// idx 存储当前已经用到了哪个点
static final int N = 100010;
static int head, idx;
static int[] e = new int[N], ne = new int[N];
// 初始化
static void init() {
head = -1;
idx = 0;
}
// 将x插入头节点
static void add_to_head(int x) {
e[idx] = x;
ne[idx] = head;
head = idx;
idx++;
}
// 将x插入到下标是k的点后面
static void add(int k, int x) {
e[idx] = x;
ne[idx] = ne[k];
ne[k] = idx;
idx++;
}
// 将下标为k的点后面的点删掉
static void remove(int k) {
ne[k] = ne[ne[k]];
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int m = Integer.parseInt(in.nextLine());
init();
while (m -- > 0) {
String[] s = in.nextLine().split(" ");
if (s[0].equals("H")) {
int x = Integer.parseInt(s[1]);
add_to_head(x);
} else if (s[0].equals("I")) {
int k = Integer.parseInt(s[1]);
int x = Integer.parseInt(s[2]);
add(k - 1, x);
} else {
int k = Integer.parseInt(s[1]);
if (k == 0) {
head = ne[head];
}
else remove(k - 1);
}
}
for (int i = head; i!= -1; i = ne[i]) {
System.out.print(e[i] + " ");
}
}
}
// 用数组模拟双链表
static final int N = 100010;
static int idx;
static int[] e = new int[N], l = new int[N], r = new int[N];
static void init() {
// 0表示左端点, 1表示右端点
r[0] = 1, l[0] = 0;
idx = 2;
}
// 在下标是k的点的右边,插入x
static void add(int k, int x) {
e[idx] = x;
r[idx] = r[k];
l[idx] = k;
l[r[k]] = idx;
r[k] = idx;
}
// 删除第k个点
void remove(int k) {
r[l[k]] = r[k];
l[r[k]] = l[k];
}
2. 数组模拟栈 队列
// 数组模拟栈
int[] stk = new int[N];
int tt = 0;
stk[tt++] = x; // 插入
tt--; // 删除
if (tt > 0) not empty // 判断是否为空
stk[tt] // 栈顶元素
// 用数组模拟队列
int[] q = new int[N];
int hh = 0, tt = -1; // 队头, 队尾
q[++tt] = x; // 插入
hh++; // 删除
if (hh <= tt) not empty
q[hh] // 队头元素
q[tt] // 队尾元素
3. 单调栈
一般问题:数组中每个数,找到其左边(右边)离它最近的小于(大于)它的数
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] stk = new int[100010]; // 单调增栈
int tt = -1;
while (n -- > 0) {
int x = in.nextInt();
while (tt >= 0 && stk[tt] >= x) tt--;
if (tt >= 0) System.out.print(stk[tt] + " ");
else System.out.print("-1 ");
stk[++tt] = x;
}
}
}
4. 单调队列
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), k = in.nextInt();
int[] q = new int[1000010];
int[] a = new int[1000010];
for (int i = 0; i < n; i++) a[i] = in.nextInt();
// 单调递增队列
int hh = 0, tt = -1;
for (int i = 0; i < n; i++) {
if (hh <= tt && i - k + 1 > q[hh]) hh++;
while (hh <= tt && a[q[tt]] >= a[i]) tt--;
q[++tt] = i;
if(i >= k - 1) System.out.print(a[q[hh]] + " ");
}
System.out.println();
// 单调递减队列
hh = 0;
tt = -1;
for (int i = 0; i < n; i++) {
if (hh <= tt && i - k + 1 > q[hh]) hh++;
while (hh <= tt && a[q[tt]] <= a[i]) tt--;
q[++tt] = i;
if(i >= k - 1) System.out.print(a[q[hh]] + " ");
}
}
}
5. KMP算法
6. Trie树
高效地存储和查找字符串
import java.util.*;
public class Main {
static int N = 100010, idx = 0; // 字符串最大总长度100000, idx表示当前用到的节点
// 下标是0的点,既是根节点,又是空节点
static int[][] son = new int[N][26]; // 小写字母,每个节点最多26条边
static int[] cnt = new int[N]; // cnt[i]以第i个节点结尾的字符串 的数目
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
while (n -- > 0) {
String[] str = in.nextLine().split(" ");
if (str[0].equals("I")) {
insert(str[1].toCharArray());
} else {
System.out.println(query(str[1].toCharArray()));
}
}
}
public static void insert(char[] str) {
int p = 0;
for (int i = 0; i < str.length; i++) {
int u = str[i] - 'a'; // 'a' - 'z' 映射到 0 - 25
if (son[p][u] == 0) son[p][u] = ++idx; // 不存在字符为u的节点,则创建(第idx个节点)
p = son[p][u];
}
cnt[p] ++;
}
public static int query(char[] str) {
int p = 0;
for (int i = 0; i < str.length; i++) {
int u = str[i] - 'a';
if (son[p][u] == 0) return 0;
p = son[p][u];
}
return cnt[p];
}
}
7. 并查集
import java.util.*;
public class Main {
static final int N = 100010;
static int[] p = new int[N];
// 返回x的祖宗节点 + 路径压缩
public static int find(int x) {
if (p[x] != x) p[x] = find(p[x]); // 如果x不是祖宗节点,则令x的父节点为祖宗节点
return p[x];
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] a = in.nextLine().split(" ");
int n = Integer.parseInt(a[0]);
int m = Integer.parseInt(a[1]);
// 节点[1, n]
for (int i = 1; i <= n; i++) p[i] = i;
while (m -- > 0) {
a = in.nextLine().split(" ");
int x = Integer.parseInt(a[1]);
int y = Integer.parseInt(a[2]);
if (a[0].equals("M")) {
p[find(x)] = find(y); // 将x所在集合 和 y所在集合 合并
} else {
if (find(x) == find(y)) System.out.println("Yes");
else System.out.println("No");
}
}
}
}
import java.util.*;
public class Main {
static final int N = 100010;
static int[] p = new int[N];
static int[] size = new int[N]; // 存储了集合大小,只有根节点有效,求节点所在集合大小需先求根节点,size[root]
// 返回x的祖宗节点 + 路径压缩
public static int find(int x) {
if (p[x] != x) p[x] = find(p[x]); // 如果x不是祖宗节点,则令x的父节点为祖宗节点
return p[x];
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] a = in.nextLine().split(" ");
int n = Integer.parseInt(a[0]);
int m = Integer.parseInt(a[1]);
// 节点[1, n]
for (int i = 1; i <= n; i++) {
p[i] = i;
size[i] = 1;
}
while (m -- > 0) {
a = in.nextLine().split(" ");
if (a[0].equals("C")) {
int x = Integer.parseInt(a[1]);
int y = Integer.parseInt(a[2]);
if (find(x) == find(y)) continue; // x y已在一个集合
size[find(y)] += size[find(x)]; // 合并后y的根节点 作为合并集合的根节点,其size存储了集合中所有节点的size
p[find(x)] = find(y); // 将x所在集合 和 y所在集合 合并
} else if (a[0].equals("Q1")){
int x = Integer.parseInt(a[1]);
int y = Integer.parseInt(a[2]);
if (find(x) == find(y)) System.out.println("Yes");
else System.out.println("No");
} else {
int x = Integer.parseInt(a[1]);
System.out.println(size[find(x)]);
}
}
}
}
8. 堆
删除或修改任意元素时,只会执行down(k) 或者 up(k) (当新数大于原数,执行down,否则执行up)
import java.util.*;
public class Main {
static final int N = 100010;
static int[] h = new int[N]; // 堆 [1, size]
static int size; // 堆大小
public static void down(int u) {
int t = u; // 求解三个节点中最小下标
if (u * 2 <= size && h[u * 2] < h[t]) t = u * 2;
if (u * 2 + 1 <= size && h[u * 2 + 1] < h[t]) t = u * 2 + 1;
if (u != t) { // 如果u不是最小节点,则交换值 并继续down
int tmp = h[u];
h[u] = h[t];
h[t] = tmp;
down(t);
}
}
public static void up (int u) {
// 如果当前节点大于父节点,则循环交换两值
while (u / 2 >= 1 && h[u / 2] > h[u]) {
int tmp = h[u];
h[u] = h[u / 2];
h[u / 2] = tmp;
u /= 2;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), m = in.nextInt();
size = n;
for (int i = 1; i <= n; i++) h[i] = in.nextInt();
for (int i = n / 2; i >= 1; i--) down(i); // 从最后第二层开始,将每层元素down,复杂度O(n) 即从下层往上,慢慢变成堆
//如果用插入元素建堆,则复杂度O(nlogn)
while (m -- > 0) {
System.out.print(h[1] + " "); // 输出堆最小值
// 删除h[1], 即把最后一个元素赋值给h[1],并执行down操作
h[1] = h[size];
size -- ;
down(1);
}
}
}
import java.util.*;
public class Main {
static final int N = 100010;
static int[] h = new int[N]; // 堆 [1, size]
static int size = 0; // 堆大小
static int[] ph = new int[N]; // ph[k] = i 第k个插入的数 对应的 堆中坐标i
static int[] hp = new int[N]; // hp[i] = i 堆中坐标i 对应的 k
// 模拟c++中swap函数
public static void swap(int [] nums, int a, int b) {
int tmp = nums[a];
nums[a] = nums[b];
nums[b] = tmp;
}
// 堆中元素交换时,维护映射关系hp和ph
public static void heap_swap(int a, int b) {
swap(ph, hp[a], hp[b]);
swap(hp, a, b);
swap(h, a, b);
}
public static void down(int u) {
int t = u; // 求解三个节点中最小下标
if (u * 2 <= size && h[u * 2] < h[t]) t = u * 2;
if (u * 2 + 1 <= size && h[u * 2 + 1] < h[t]) t = u * 2 + 1;
if (u != t) { // 如果u不是最小节点,则交换值 并继续down
heap_swap(u, t);
down(t);
}
}
public static void up (int u) {
// 如果当前节点大于父节点,则循环交换两值
while (u / 2 >= 1 && h[u / 2] > h[u]) {
heap_swap(u, u / 2);
u /= 2;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
int m = 0; // 当前 第m个插入的数
while (n -- > 0) {
String[] str = in.nextLine().split(" ");
if (str[0].equals("I")) { // 插入第m个数
size ++ ;
m ++;
// 第m个数 先放到 最后的位置size
ph[m] = size;
hp[size] = m;
h[size] = Integer.parseInt(str[1]);
up(size);
} else if (str[0].equals("PM")) { // 输出最小值
System.out.println(h[1]);
} else if (str[0].equals("DM")) { // 删除最小值
heap_swap(1, size);
size --;
down(1);
} else if (str[0].equals("D")) { // 删除第k个插入的数
int k = Integer.parseInt(str[1]);
k = ph[k]; // 第k个数 对应的 堆中的坐标
heap_swap(k, size);
size --;
up(k);
down(k);
} else if (str[0].equals("C")) { // 修改第k个数
int k = Integer.parseInt(str[1]);
int x = Integer.parseInt(str[2]);
k = ph[k]; // 第k个数 对应的 堆中的坐标
h[k] = x;
up(k);
down(k);
}
}
}
}
9. Hash表
// 拉链法
import java.util.*;
public class Main {
static final int N = 100003; // N一般取质数比较好(可取大于数据范围的第一个质数)
static int[] h = new int[N]; // 存储头节点位置, -1为空节点 (每个哈希值对应一个链表)
static int[] e = new int[N]; // 链表值
static int[] ne = new int[N]; // 链表指针
static int idx = 0; // 当前可用链表节点
public static void insert(int x) {
int k = (x % N + N) % N; // x % n可能为负数,+ N保证为正,最后再取模
// 将新节点插入头部
e[idx] = x;
ne[idx] = h[k];
h[k] = idx ++;
}
// 查找x是否在哈希表中
public static boolean query (int x) {
int k = (x % N + N) % N;
for (int i = h[k]; i != -1; i = ne[i]) {
if (e[i] == x) return true;
}
return false;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
for (int i = 0; i < h.length; i++) h[i] = -1; // 初始化哈希表中所有指针为空
while (n -- > 0) {
String[] str = in.nextLine().split(" ");
int x = Integer.parseInt(str[1]);;
if (str[0].equals("I")) {
insert(x);
} else if (str[0].equals("Q")) {
if (query(x)) {
System.out.println("Yes");
} else System.out.println("No");
}
}
}
}
// 开放寻址法
import java.util.*;
public class Main {
// 一般取 2-3倍 的数据范围
static final int N = 200003; // N一般取质数比较好(可取大于数据范围的第一个质数)
static final int Null = 0x3f3f3f3f; // 0x3f3f3f3f > 10^9
static int[] h = new int[N];
// 寻找x所在下标,若不存在,则返回应该存放的位置
public static int find (int x) {
int k = (x % N + N) % N;
// 若h[k]已有数,则顺序向后寻找
while (h[k] != Null && h[k] != x) {
k++;
if (k == N) k = 0;
}
return k;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
for (int i = 0; i < h.length; i++) h[i] = Null; // 初始化哈希表中所有指针为空
while (n -- > 0) {
String[] str = in.nextLine().split(" ");
int x = Integer.parseInt(str[1]);
int k = find(x);
if (str[0].equals("I")) {
h[k] = x;
} else if (str[0].equals("Q")) {
if (h[k] != Null) {
System.out.println("Yes");
} else System.out.println("No");
}
}
}
}
// 字符串哈希
import java.util.*;
public class Main {
static final long MOD = Long.MAX_VALUE; // c++中用unsigned long long,可不用取模,溢出即自动取模
static final int N = 100010, P = 131; // 经验值P: 131 1331
static long[] h = new long[N]; // hash值
static long[] p = new long[N]; // p[i] = P ^ i
// 字符串[l, r]的哈希值
public static long get(int l, int r) {
return h[r] - h[l - 1] * p[r - l + 1];
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] arr = in.nextLine().split(" ");
int n = Integer.parseInt(arr[0]);
int m = Integer.parseInt(arr[1]);
char[] str = in.nextLine().toCharArray();
p[0] = 1;
for (int i = 1; i <= n; i++) {
h[i] = (h[i - 1] * P + str[i - 1]) % MOD; // h[i]为字符串前缀[1, i]的哈希值
p[i] = p[i - 1] * P;
}
while (m -- > 0) {
arr = in.nextLine().split(" ");
int l1 = Integer.parseInt(arr[0]);
int r1 = Integer.parseInt(arr[1]);
int l2 = Integer.parseInt(arr[2]);
int r2 = Integer.parseInt(arr[3]);
if (get(l1, r1) == get(l2, r2)) { // 若两个子串的hash值相同,则为相同子串
System.out.println("Yes");
} else System.out.println("No");
}
}
}