随笔分类 - 基础算法学习
我倒要看看能拖多久
摘要:蒙德里安的梦想 #include<iostream> #include<cstring> #include<algorithm> #include<vector> using namespace std; typedef long long ll; const int N = 12; const i
阅读全文
摘要:一般ACM或者笔试题的时间限制是1秒或2秒。 在这种情况下,C++代码中的操作次数控制在 107∼108107∼108 为最佳。 下面给出在不同数据范围下,代码的时间复杂度和算法该如何选择: 1. n≤30, 指数级别, dfs+剪枝,状态压缩dp 2. n≤100 => O(n3),floyd,d
阅读全文
摘要:原理 对二分图的一边进行匹配尝试,最后再次遍历统计成功次数 O(nm) //但是实际情况一般少于O(nm) 模板 二分图的最大匹配 #include<iostream> #include<cstring> using namespace std; const int N = 100010; int
阅读全文
摘要:原理 1,2为两种颜色,0为未染色 根据染色是否成功判断是否为二分图 模板 染色法判定二分图 #include<iostream> #include<cstring> #include<algorithm> using namespace std; const int N = 200010; int
阅读全文
摘要:使用 稀疏图求最小生成树边权和 O(mlogm) 模板 Kruskal算法求最小生成树 #include<iostream> #include<algorithm> using namespace std; const int N = 200010; int n,m; int p[N]; //并查集
阅读全文
摘要:用处 求最小生成树的权和时使用 稠密图 O(nm) 模板 Prim算法求最小生成树 #include<iostream> #include<algorithm> #include<cstring> using namespace std; const int N = 510,INF = 0x3f3f
阅读全文
摘要:多源路算法(O(n ^ 3)) Floyd求最短路 #include<iostream> #include<algorithm> using namespace std; const int N = 210,null = 0x3f3f3f3f; int d[N][N]; int n,m; //遍历更
阅读全文
摘要:spfa(O(m) ~ O(nm)) 可以在正权边的时候代替dijkstra算法,如果时间复杂度为O(nm)就不行了 没有负权回路的时候可以代替bellman-ford算法,一般都用spfa spfa是对bellman-ford的bfs优化版本 spfa求最短路 #include<iostream>
阅读全文
摘要:适用范围 1.有负环存在 2.有边数限制 模板 有边数限制的最短路 #include<iostream> #include<algorithm> #include<cstring> using namespace std; const int N = 510,M = 10010; int dist[
阅读全文
摘要:#适用范围 单源正权边最短路 时间复杂度O(n^2) #模板 稠密图(n^2) Dijkstra求最短路 I #include<iostream> #include<algorithm> #include<cstring> using namespace std; const int N = 510
阅读全文
摘要:含义 所有点都是从小编号指向大编号 是一个有序无环图 检查时遍历所有入度0的节点并删除与之相关的连线 模板 有向图的拓扑序列 #include<iostream> #include<cstring> using namespace std; const int N = 100010; int e[N
阅读全文
摘要:模板 //起始位置为u,求v点最短路 //点之间距离为1 int bfs(int u,int v){ int tt = 0,hh = 0; memset(d,-1,sizeof d); d[u] = 0; q[0] = u; while(hh <= tt){ //取出点 int t = q[hh +
阅读全文
摘要:模板 #include<iostream> #include<algorithm> #include<cstring> using namespace std; const int N = 100010,M = N * 2; //h是N个单链表的头节点 //ne是单链表的ne数组 //e是单个节点的
阅读全文
摘要:vector 变长数组 size() 返回元素个数 empty() 检查是否为空 clear() 清空 front()/back() 返回头尾元素 begin()/end() 迭代器 pair<a,b> 一个有a类型元素和b类型元素的结构体 .first 访问第一个元素 .second 访问第二个元
阅读全文
摘要:回文子串的最大长度 技巧 在每两个字符中插入一个新的字符,可以不用判断字符串的奇偶,方便编程。 利用字符串哈希和二分求解 #include<iostream> #include<cstring> #include<algorithm> using namespace std; const int N
阅读全文
摘要:作用 有时候 more niubi than kmp 比较两个区间的字符串是否一致就可以用 原理 将一个字符串看成一个P进制的数 对比时取子串可以直接相减得出 注意点: 1. 任意字符不可以映射成0,否则会出现不同的字符串都映射成0的情况,比如A,AA,AAA皆为0 2. 冲突问题:通过巧妙设置P
阅读全文