3k3 分鐘

模拟散列表 - 哈希表 #include <iostream> #include <cstring> using namespace std; const int N = 1e5 + 3; int h[N]; int e[N], ne[N], idx; void insert(int x){ int k = (x % N + N) % N; e[idx] = x, ne[idx] = h[k], h[k] = idx++; } bool find(int x){
1.6k1 分鐘

连通块中点的数量 - 并查集 #include <iostream> using namespace std; const int N = 1e6 + 10; int p[N], cnt[N]; int n, m; int find(int x){ if(p[x] != x) p[x] = find(p[x]); return p[x]; } int main(){ scanf("%d%d", &n, &m); for(int i = 1; i <
2k2 分鐘

堆排序 # 堆是一棵二叉树 或是一棵 [[完全二叉树]] 这里是小根堆(性质:每一个点都是小于等于左右儿子的) #include <iostream> #include <algorithm> using namespace std; const int N = 1e6 + 10; int n, m; int h[N], cnt; void down(int u){ int t = u; if(u * 2 <= cnt && h[u * 2] < h[t]) t =
1.6k1 分鐘

字符串哈希 - 哈希表 #include <iostream> using namespace std; typedef unsigned long long ULL; const int N = 1e6 + 10, P = 131; int n, m; char str[N]; ULL h[N], p[N]; ULL get(int l, int r){ return h[r] - h[l - 1] * p[r - l + 1]; //返回l到r这个区间内的哈希值 看是否一样 } int ma
2.6k2 分鐘

模拟堆 #include <iostream> #include <algorithm> #include <string.h> using namespace std; const int N = 1e6 + 10; int n, m; int h[N], ph[N], hp[N], cnt; void heap_swap(int a, int b){ swap(ph[hp[a]], ph[hp[b]]); swap(hp[a], hp[b]); swap(h[a], h[b]); } void down(int
1.6k1 分鐘

合并集合 - 并查集 #include <iostream> using namespace std; const int N = 1e6 + 10; int n, m; int p[N]; //并查集里的father数组 存的是每个元素他的父节点是谁 int find(int x){//find x 返回x所在集合的编号 即返回x的祖宗节点 if(p[x] != x) p[x] = find(p[x]); return p[x]; } int main(){ scanf(
1.4k1 分鐘

Trie 字符串统计 #include <iostream> using namespace std; const int N = 1e6 + 10; int son[N][26], cnt[N], idx; char str[N]; void insert(char str[]){ int p = 0; for(int i = 0; str[i]; i++){ int u = str[i] - 'a'; if(!son[p][u]) son[p][u] = ++idx;
9851 分鐘

# 单调栈 #include <iostream> using namespace std; const int N = 1e5 + 10; int n; int stk[N], tt; int main(){ cin >> n; for(int i = 0; i < n; i++){ int x; cin >> x while(tt && stk[tt] >= x) tt--; if(tt) cout << stk[tt] <<
1.5k1 分鐘

KMP 字符串 #include <iostream> using namespace std; const int N = 1e6 + 10; int n, m; char p[N], s[N]; int ne[N]; int main(){ cin >> n >> p + 1 >> m >> s + 1; for(int i = 2, j = 0; i <= n; i++){ while(j && p[i] !&