好像CSDN目前不支持代码收缩的功能,页面有拖沓,可以根据目录来寻找位置。或者之后我会分个不同板块
目录
一、考点
1、排序:堆排序、插入排序
2、图论:拓扑图、最短路径(基本dijkstra)、遍历(深搜广搜)
3、树:前中后层遍历、前中求后/后中求前(前后求中【不唯一】)、完全二叉、平衡二叉(AVL)、二叉查找(BST)
4、其他:并查集、快乐模拟、哈希、背包、最大不下降(LIS)、最长公共(LCS)、分块思想、最大连续和、最长回文、最近公共祖先(LCA)
二、代码小知识点
三、算法模板
1、堆排序
void downAd(int low, int high) {
int i = low, j = i * 2;
while (j <= high) {
if (j + 1 <= high && temp[j + 1] > temp[j])
j = j + 1;
if (temp[j] > temp[i]) {
swap(temp[j], temp[i]);
i = j;
j = i * 2;
}
else break;
}
}
void heapSort() {
bool flag = false;
for (int i = n / 2; i >= 1; i--)downAd(i,n);
for (int i = n; i > 1; i--) {
swap(temp[i], temp[1]);
downAd(1, i - 1);
}
}
2、树的排序
//前后序获得中序
void getIn(int prel, int prer, int postl, int postr) {
if (prel == prer) {
in.push_back(pre[prel]);
return;
}
if (pre[prel] == post[postr]) {
int i = prel + 1;
while(i <= prer && pre[i] != post[postr-1])i++;
getIn(prel + 1, i - 1, postl, postl + (i - prel - 1) - 1);
in.push_back(post[postr]);
getIn(i, prer, postl + (i - prel - 1), postr - 1);
}
}
3、LCA
(但是目前没用过,求LCA的题目都有更简便的做法)
#include<bits/stdc++.h>
using namespace std;
const int maxn = 500005;
struct edge{
int next, to;
}e[maxn << 1];
int head[maxn], cnt;
int n, m, s, a, b, xx, yy;
int depth[maxn], fa[maxn][22];
void add(int u, int v)
{
e[++cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt;
}
void dfs(int now, int fath)
{
depth[now] = depth[fath] + 1;
fa[now][0] = fath;
for(int i = 0; i <= 18; i++)
fa[now][i+1] = fa[fa[now][i]][i];
for(int i = head[now]; i != 0; i = e[i].next)
{
if(e[i].to != fath)
dfs(e[i].to, now);
}
}
int lca(int x, int y)
{
if(depth[x] < depth[y]) swap(x, y);
for(int i = 18; i >= 0; i--)
{
if(depth[fa[x][i]] >= depth[y]) x = fa[x][i];
if(x == y) return x;
}
for(int i = 18; i >= 0; i--)
{
if(fa[x][i] != fa[y][i])
{
x = fa[x][i];
y = fa[y][i];
}
}
return fa[x][0];
}
int main()
{
scanf("%d%d%d", &n, &m, &s);
for(int i = 1; i < n; i++)
{
scanf("%d%d", &xx, &yy);
add(xx, yy); add(yy, xx);
}
dfs(s, 0);
for(int i = 1; i <= m; i++)
{
scanf("%d%d", &a, &b);
printf("%d\n", lca(a, b));
}
return 0;
}
4、并查集
四、例题
1、dijkstra+DFS
1018 Public Bike Management (30 分)
2、AVL
1123 Is It a Complete AVL Tree (30分)(层次遍历/BFS)
3、LCS、LIS(DP)
1045 Favorite Color Stripe (30分)
题解:【PTA-A】1045 Favorite Color Stripe (30分)(LIS与LCS)_Netceor的博客-CSDN博客
4、背包问题
5、堆排序
1098 Insertion or Heap Sort (25分)
6、前后建立中序
1119 Pre- and Post-order Traversals (30分)
7、快乐模拟(快乐模拟的题目其实挺好理解,就是要细心耐心)
8、zigzag层序遍历
1127 ZigZagging on a Tree (30分)
9、LCA