知识合集(板子)
本博客主要收集一些常用的板子的写法和一些做题技巧及方法(不定期更新)
LIS
https://www.cnblogs.com/jd1412/p/14084132.html
tarjin
https://www.luogu.com.cn/paste/teipaej1
最短路径树
https://www.cnblogs.com/jd1412/p/14084313.html
并查集
https://www.cnblogs.com/jd1412/p/14084793.html
最小生成树
https://www.cnblogs.com/jd1412/p/14084660.html
LCA
https://www.cnblogs.com/jd1412/p/14084867.html
拓扑排序
inline void tp()
{
for(int i=1;i<=sum;i++)
{
if(!ru[i]) q.push(i);
}
while(q.size())
{
ll u=q.front();
q.pop();
sh[++cnt]=u;
for(int i=0;i<e1[u].size();i++)
{
ll v=e1[u][i];
ru[v]--;
if(!ru[v]) q.push(v);
}
}
}
康托展开
https://www.cnblogs.com/jd1412/p/14085026.html
树的重心
https://www.cnblogs.com/jd1412/p/14066753.html
树的直径
https://www.cnblogs.com/jd1412/p/14066826.html
Dijkstra
https://www.cnblogs.com/jd1412/p/13282446.html
SPFA
https://www.cnblogs.com/jd1412/p/14066912.html
//朴素版本
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<vector>
#include<queue>
#define ll long long
const ll maxn=1e4+10;
ll n,m,k;
ll dis[maxn][3];
struct node
{
ll u,v,w;
} s[maxn];
inline void bellman_ford(ll x)
{
memset(dis,0x3f,sizeof(dis));
dis[x][0]=dis[x][1]=0;
ll t=0;
for(int i=1;i<=n-1;i++)
{
t^=1;
for(int j=1;j<=m;j++)
{
ll u=s[j].u,v=s[j].v,w=s[j].w;
if(dis[v][t^1]>dis[u][t]+w)
{
dis[v][t^1]=dis[u][t]+w;
}
}
}
if(dis[n][t^1]>=0x3f3f3f3f3f3f3f3f/2)
{
printf("impossible\n");
}
else
{
printf("%lld\n",dis[n][t^1]);
}
return ;
}
int main(void)
{
scanf("%lld %lld %lld",&n,&m,&k);
for(int i=1;i<=m;i++)
{
scanf("%lld %lld %lld",&s[i].u,&s[i].v,&s[i].w);
}
bellman_ford(1);
return 0;
}
Floyd
for(int i=1;i<=n;i++) f[i][i]=0;
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
f[i][j]=std::max(f[i][j],f[i][k]+f[k][j]);
}
}
}
传递闭包
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
dp[i][j]|=dp[i][k]&dp[k][j];
}
}
}
线段树
https://www.cnblogs.com/jd1412/p/13246053.html
树状数组
https://www.cnblogs.com/jd1412/p/14087315.html
线性筛
int v[maxn],prime[maxn];
void primes(int n)
{
memset(v,0,sizeof(v));//最小质因子
m=0;//质数个数
for(int i=2;i<=n;i++)
{
if(!v[i])//未被标记,i为质数
{
v[i]=i;
prime[++m]=i;
}
for(int j=1;j<=m;j++)
{
if(prime[j]>v[i]||prime[j]>n/i) break;//i有更小的质因子,或者超出n的范围
v[i*prime[j]]=prime[j];//prime[j]为合数 i*prime[j] 的最小质因子
}
}
}
矩阵加速
https://www.cnblogs.com/jd1412/p/12724329.html
exgcd
https://www.cnblogs.com/jd1412/p/12709042.html
crt & excrt
除法分块
https://www.cnblogs.com/jd1412/p/13282403.html
ST表
https://www.cnblogs.com/jd1412/p/13246122.html
快速幂
https://www.cnblogs.com/jd1412/p/12456522.html
离散化
void hash()
{
scanf("%lld%lld",&n,&q);
for(int i=1;i<=n;i++) scanf("%lld",a+i);
memcpy(b+1,a+1,n*sizeof(long long));
sort(b+1,b+1+n);
lont long *bg=b+1;
long long *en=unique(b+1,b+1+n);
m=en-bg;
for(int i=1;i<=n;i++)
{
a[i]=lower_bound(bg,en,a[i])-b;
}
}
字符串hash
#include<iostream>
#include<cstdio>
#include<cstring>
#include<math.h>
#include<algorithm>
using namespace std;
typedef unsigned long long ll;
const ll maxn=1e4+10;
ll n,m,base=131;
ll a[maxn];
int prime=233333,sum=1;
char s[maxn];
inline ll hashe(char s[])
{
int len=strlen(s);
ll ans=0;
for(int i=0;i<len;i++)
{
ans=(ans*base+(ll)s[i]);
}
return ans;
}
int main(void)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",s);
a[i]=hashe(s);
}
sort(a+1,a+n+1);
for(int i=1;i<n;i++)
{
if(a[i]!=a[i+1])
{
sum++;
}
}
printf("%lld",sum);
}
KMP
#include<iostream>
#include<cstdio>
#include<cstring>
#include<math.h>
#include<algorithm>
#define ll long long
using namespace std;
const ll maxn=1e6+10;
ll n,m;
ll kmp[maxn];
char a[maxn],b[maxn];
int main(void)
{
ll j=0;
scanf("%s%s",a+1,b+1);
ll lena=strlen(a+1);
ll lenb=strlen(b+1);
for(int i=2;i<=lenb;i++)
{
while(j&&b[i]!=b[j+1])
{
j=kmp[j];
}
if(b[j+1]==b[i]) j++;
kmp[i]=j;
}
j=0;
for(int i=1;i<=lena;i++)
{
while(j>0&&b[j+1]!=a[i])
{
j=kmp[j];
}
if(b[j+1]==a[i])
{
j++;
}
if(j==lenb)
{
printf("%lld\n",i-lenb+1);
j=kmp[j];
}
}
for(int i=1;i<=lenb;i++)
{
printf("%lld ",kmp[i]);
}
return 0;
}