
算法模板
Herod_
这个作者很懒,什么都没留下…
展开
-
unorder_map + pair
传入 pair 的哈希函数struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2>& p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return原创 2021-05-12 17:24:00 · 383 阅读 · 0 评论 -
模板:EXKMP
const int maxn=3e6+9; //字符串长度最大值int next[maxn],ex[maxn]; //ex数组即为extend数组//预处理计算next数组void GETNEXT(char *str){ int i=0,j,po,len=strlen(str); next[0]=len;//初始化next[0] while(str[i]==str[i+1]&&i+1<len)//计算next[1] i++; next原创 2021-01-30 20:24:12 · 153 阅读 · 0 评论 -
模板:Manacher
char Ma[maxn*2];int Mp[maxn*2];void Manacher(char s[],int len){ int l=0; Ma[l++]='$'; Ma[l++]='#'; for(int i=0;i<len;i++){ Ma[l++]=s[i]; Ma[l++]='#'; } Ma[l]=0; int mx=0,id=0; for(int i=0;i<l;i++){原创 2021-01-30 20:22:37 · 135 阅读 · 1 评论 -
模板:模拟退火
double ansx,ansy; //全局最优解的坐标double ans=1e18,t; //全局最优解、温度const double delta=0.993; //降温系数inline double calc_energy(double x,double y) { //计算整个系统的能量 double rt=0; for (re int i=1;i<=n;i++) { double deltax=x-a[i].x,deltay=y-a[i].y;原创 2021-01-19 15:46:49 · 194 阅读 · 0 评论 -
POJ-2069-Super Star(最小球覆盖/模拟退火)
Super StarDuring a voyage of the starship Hakodate-maru (see Problem 1406), researchers found strange synchronized movements of stars. Having heard these observations, Dr. Extreme proposed a theory of “super stars”. Do not take this term as a description原创 2021-01-19 14:38:42 · 449 阅读 · 0 评论 -
POJ-3007-Buried memory(最小圆覆盖/模拟退火)
Buried memoryProblem DescriptionEach person had do something foolish along with his or her growth.But,when he or she did this that time,they could not predict that this thing is a mistake and they will want this thing would rather not happened.The world原创 2021-01-19 14:56:39 · 171 阅读 · 1 评论 -
模板:三分
const double EPS = 1e-9;while(r - l < EPS) { double lmid = l + (r - l) / 3; double rmid = r - (r - l) / 3; lans = f(lmid),rans = f(rmid); // 求凹函数的极小值 if(lans <= rans) r = rmid; else l = lmid; // 求凸函数的极大值 if(lans >=原创 2021-01-18 17:24:11 · 121 阅读 · 0 评论 -
模板:二分图匹配(匈牙利算法的DFS实现)
/* ***************************************************二分图匹配(匈牙利算法的DFS实现)INIT:G[][]两边定点划分的情况CALL:res=Hungary();输出最大匹配数优点:适于稠密图,DFS找增广路快,实现简洁易于理解时间复杂度:O(VE);*************************************************** */const int MAXN = 510;int uN,vN;//u,v数目原创 2021-01-18 15:41:44 · 142 阅读 · 0 评论 -
模板:后缀数组
DA算法#include<bits/stdc++.h>using namespace std;typedef long long ll;const int N=100010;int wa[N],wb[N],wv[N],wss[N],rak[N],height[N],cal[N],n,sa[N];char s[N];int cmp(int *r,int a,int b,int l){return r[a]==r[b]&&r[a+l]==r[b+l];}void d原创 2020-10-27 14:21:42 · 192 阅读 · 0 评论 -
模板:Treap
#include <iostream>#include <cstdio>#include <cstring>#include <stdlib.h>#define INF 0x7fffffffusing namespace std;inline int read(){ int sum=0,f=1;char c=getchar(); while(c>'9'||c<'0'){if(c=='-') f=-1;c=getchar();}原创 2020-10-25 19:16:03 · 138 阅读 · 0 评论 -
模板:Tarjan 求无向图 任意两点路径的必经点
#include<bits/stdc++.h>#define LL long long#define DB double#define RG register#define IL inline#define pb push_backusing namespace std;const int N=2e5+3;int n,m,Q,tot,num,cnt,Time,dfn[N],low[N];int bri[N<<1],vis[N],bel[N],pos[N],dep[N原创 2020-10-24 21:17:13 · 320 阅读 · 0 评论 -
模板:三维最近点对
#include <bits/stdc++.h>using namespace std;#define INF 1e18struct node{ double x,y,z;}a[200050];node s[200050];bool cmp1(node a1,node a2){ if(a1.x != a2.x) return a1.x < a2.x; else return a1.y < a2.y;}bool cmp2(node a1,n原创 2020-10-10 21:32:57 · 744 阅读 · 0 评论 -
模板:SG函数
解题模型:1.把原游戏分解成多个独立的子游戏,则原游戏的SG函数值是它的所有子游戏的SG函数值的异或。即SG(G)=SG(G1)^ SG(G2)^ … ^Sg(Gn)。2.分别考虑每一个子游戏,计算其SG值。SG值的计算方法:(重点) a.可选步数为1~m的连续整数,直接取模即可,SG(x) = x % (m+1)(Bash game)。 b.可选步数为任意步,SG(x) = x(Nim game)。 c.可选步数为一系列不连续的数,用模板计算。结论:1.当SG[x] =原创 2020-09-23 21:00:10 · 367 阅读 · 0 评论 -
HDU-3948-The Number of Palindromes(回文自动机模板题)
The Number of PalindromesProblem DescriptionNow, you are given a string S. We want to know how many distinct substring of S which is palindrome.InputThe first line of the input contains a single integer T(T<=20), which indicates number of test cases原创 2020-09-12 17:14:41 · 176 阅读 · 0 评论 -
模板:回文自动机
const int N = 1e5+10;struct PAM{ /** len[u] : u 节点代表回文串的长度。 fa[u] : u 节点代表回文串的最长回文后缀代表的节点。 tran[u][c] : 转移函数,表示在 u 代表的回文串的两端加上字符 c 之后的回文串。 num[u] : 代表 u 节点代表回文串的回文后缀个数。 L[i] : 代表原字符串以 i 结尾的回文后缀长度。 size[u] : u 点代表的回文串的数量。 **/ int len[N],fa[N],s原创 2020-09-11 21:32:34 · 145 阅读 · 0 评论 -
模板:AC自动机
const int N = 1e6+5;int trie[N][26]; // 字典树int wordcnt[N]; // 次数统计int fail[N]; // 众所周知fail指针int tot = 0; // 节点数量void insertWord(string s){ // 字典树操作,插入字符串 int root = 0; for(int i = 0 ; i < s.size(); i +原创 2020-09-09 21:27:40 · 194 阅读 · 0 评论 -
模板:线段树
typedef long long ll;const int N = 1e5 + 10;#define lson rt << 1 // == rt * 2 左儿子#define rson rt << 1 | 1 // == rt * 2 + 1 右儿子#define int_mid int mid = tree[rt].l + tree[rt].r >> 1 int a[N]; // 初始值 struct node { int l, r; l原创 2020-09-01 13:11:41 · 142 阅读 · 0 评论 -
模板:数论(持续更新)
ps:数大的时候 int全为long long素数筛:const int N = 1e7+15;int prime[N],mark[N],pcnt;// 如果变量名都相同的话,就不用传参了void getPrimes(int prime[],int N,int &pcnt){ memset(mark,0,sizeof(mark)); pcnt = 0; prime[pcnt++] = 2; mark[2] = 1; for(int i = 2;原创 2020-08-27 13:50:58 · 356 阅读 · 0 评论 -
模板:Tarjan
#include<iostream>using namespace std;int DFN[105]; //记录在做dfs时节点的搜索次序int low[105]; //记录节点能够找到的最先访问的祖先的记号int count=1; //标记访问次序,时间戳int stack[105];原创 2020-08-26 22:40:53 · 131 阅读 · 0 评论 -
模板:最大流
const int maxn =1000+10; struct Edge{ int from,to,cap,flow; Edge(){} Edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){}}; struct Dinic{ int n,m,s,t; vector<Edge> edges; vector<int> G[maxn]; int cu.原创 2020-08-25 03:32:52 · 123 阅读 · 0 评论 -
模板:简单计算几何
typedef double db;const db eps = 1e-8;const db inf = 1e20;const db pi = acos(-1.0);int sgn(db x){ if(fabs(x) < eps)return 0; if(x < 0)return -1; else return 1;}struct Point{ db x, y; Point(){} Point(db _x, db _y){ x = _x; y = _y; }原创 2020-08-20 18:39:37 · 149 阅读 · 0 评论 -
模板:KM匹配
const int N = 505;const int INF = 1<<30;int ex_boy[N],ex_girl[N]; // 左右期望值bool vis_girl[N],vis_boy[N]; // 访问标记int love[N][N]; // 期望值矩阵int match[N]; // 右图匹配点int d[N]; // 期望差值...原创 2020-02-22 11:54:09 · 158 阅读 · 0 评论 -
区间DP学习 从入门到模板
核心思想区间DP,顾名思义,就是枚举区间进行DP。用dp[i][j]表示一个区间的最优解,然后从小区间向大区间合并,最终求得整个大区间的解。算法过程就是三层枚举,第一层枚举区间长度,第二次枚举区间起点,第三层枚举区间分割点。这样在枚举第二层的时候,可以保证第三层的区间已经枚举过了。状态转移: dp[i][j] = max(dp[i][j],dp[i][k]+dp[k+1][j]+w[i]...原创 2020-02-02 17:26:33 · 252 阅读 · 0 评论 -
模板:树状数组
int bit[N],n;int lowbit(int x){ return x&(-x);}void add(int i,int x){ while(i<=n) { bit[i]+=x; i+=lowbit(i); }}void sub(int i,int x){ while(i<=n)...原创 2019-12-06 19:06:31 · 123 阅读 · 0 评论 -
模板:大数
string sum(string a,string b){//大数的加法 string result; if(a.size()<b.size()){ string temp(b.size()-a.size(),'0'); a=temp+a; } else{ string temp(a.size()-b.size(),'0'); b=temp+b; } int f...原创 2019-11-26 21:25:39 · 128 阅读 · 0 评论