如有特殊情况有标注 qwq。
Kmp
void kmp(string a, int bor[]){
int n = a.size();
vector <char> s(n + 1);
for (int i = 1; i <= n; i ++ ) s[i] = a[i - 1];
bor[1] = 0;
for (int i = 2; i <= n; i ++ ){
int p = bor[i - 1];
while (p && s[p + 1] != s[i]) p = bor[p];
if (s[p + 1] == s[i]) bor[i] = p + 1;
}
}Trie
struct Trie{
int tr[N * 26][26], cnt[N * 26], tot = 1;
// sth.
void insert(string s, int qwq){
int p = 1; cnt[p] ++;
for (auto i : s){
int x = i - 'a';
if (!tr[p][x]) tr[p][x] = ++ tot;
p = tr[p][x], cnt[p] ++ ;
}
// sth.
}
// sth.
} tr;
manacher
void manacher(string str, int ans[]){
int n = str.size();
vector <char> t(n + 1), s(2 * n + 5);
for (int i = 1; i <= n; i ++ ) t[i] = str[i - 1];
s[1] = '|';
for (int i = 1; i <= n; i ++ ) s[2 * i] = t[i], s[2 * i + 1] = '|';
n = 2 * n + 1, s[0] = '<', s[n + 1] = '>';
int l = 0, r = 0;
for (int i = 1; i <= n; i ++ ){
if (i > r) ans[i] = 1;
else ans[i] = (min(ans[l + r - i], r - i));
while (s[i - ans[i]] == s[i + ans[i]]) ans[i] ++;
if (i + ans[i] > r) l = i - ans[i], r = i + ans[i];
}
}ACAM
struct ACAM{
int tot = 1, tr[M][26], nxt[M];
// sth
void insert(string s){
int p = 1;
for (auto i : s){
int x = i - 'a';
if (!tr[p][x]) tr[p][x] = ++ tot;
p = tr[p][x];
}
// sth.
}
void build_ac(){
queue <int> q;
for (int c = 0; c < 26; c ++ ){
int p = tr[1][c];
if (p) nxt[p] = 1, q.push(p);
else tr[1][c] = 1;
}
while (q.size()){
int u = q.front(); q.pop();
// sth
for (int c = 0; c < 26; c ++ ){
if (!tr[u][c]) tr[u][c] = tr[nxt[u]][c];
else nxt[tr[u][c]] = tr[nxt[u]][c], q.push(tr[u][c]);
}
}
}
} ac;SAM
struct SAM{
int tot = 1, lst = 1;
struct node{
int len, fa, ch[26];
} tr[N << 1];
int siz[N << 1];
vector <int> g[N << 1];
void extend(int c){
int p = lst, np = lst = ++ tot;
siz[np] = 1, tr[np].len = tr[p].len + 1;
while (p && !tr[p].ch[c]) tr[p].ch[c] = np, p = tr[p].fa;
if (!p) tr[np].fa = 1;
else{
int q = tr[p].ch[c];
if (tr[q].len == tr[p].len + 1) tr[np].fa = q;
else{
int nq = ++ tot;
tr[nq] = tr[q], tr[nq].len = tr[p].len + 1;
tr[np].fa = tr[q].fa = nq;
while (p && tr[p].ch[c] == q) tr[p].ch[c] = nq, p = tr[p].fa;
}
}
}
// sth.
} sam;exgcd
int exgcd(int a, int b, int &x, int &y){
if (!b){
x = 1, y = 0;
return a;
}
int t = exgcd(b, a % b, y, x);
y -= (a / b) * x;
return t;
}dinic(bfs)
bool bfs(){
memset(dep, 0, sizeof dep);
memcpy(cur, head, sizeof cur);
queue <int> q; q.push(S); dep[S] = 1;
while (q.size()){
int u = q.front(); q.pop();
for (int i = head[u]; ~i; i = e[i].nxt){
int v = e[i].to;
if (!dep[v] && e[i].w){
dep[v] = dep[u] + 1;
if (v == T) return true;
q.push(v);
}
}
}
return false;
}
int dfs(int u, int flow){
if (u == T || !flow) return flow;
int rest = flow;
for (int &i = cur[u]; ~i; i = e[i].nxt){
int v = e[i].to;
if (dep[v] == dep[u] + 1 && e[i].w){
int k = dfs(v, min(rest, e[i].w));
e[i].w -= k, e[i ^ 1].w += k, rest -= k;
if (!rest) break;
}
}
return flow - rest;
}
int dinic(){
int res = 0;
while (bfs()) res += dfs(S, inf);
return res;
}新的线性基(区间线性基,离线)
struct linear{
int W, pos[65];
ll num[65];
linear(){ W = 60; for (int i = 0; i <= W; i ++ ) num[i] = pos[i] = 0; }
linear(int w){ W = w; for (int i = 0; i <= W; i ++ ) num[i] = pos[i] = 0; }
void clear(){ for (int i = 0; i <= W; i ++ ) num[i] = pos[i] = 0; }
bool insert(ll x, int p){
for (int i = W; i >= 0 && x; i -- )
if ((x >> i) & 1ll){
if (num[i]){
if (pos[i] < p) swap(pos[i], p), swap(x, num[i]);
x ^= num[i];
}
else{ num[i] = x, pos[i] = p, x = 0; return true; }
}
return false;
}
ll query_mx(ll x, int le){
ll ans = x;
for (int i = W; i >= 0; i -- ) if (pos[i] >= le) ans = max(ans, ans ^ num[i]);
return ans;
}
ll query_mn(ll x, int le){
ll ans = x;
for (int i = W; i >= 0; i -- ) if (pos[i] >= le) ans = min(ans, ans ^ num[i]);
return ans;
}
};线性基
struct linear{
ll W, num[65];
linear(){ W = 60; for (int i = 0; i <= W; i ++ ) num[i] = 0; }
linear(int w){ W = w; for (int i = 0; i <= W; i ++ ) num[i] = 0; }
void clear(){ for (int i = 0; i <= W; i ++ ) num[i] = 0; }
bool insert(ll x){
for (int i = W; i >= 0 && x; i -- )
if ((x >> i) & 1ll){
if (num[i]) x ^= num[i];
else{ num[i] = x, x = 0; return true; }
}
return false;
}
ll query_mx(int x){
ll ans = x;
for (int i = W; i >= 0; i -- ) ans = max(ans, ans ^ num[i]);
return ans;
}
ll query_mn(int x){
ll ans = x;
for (int i = W; i >= 0; i -- ) ans = min(ans, ans ^ num[i]);
return ans;
}
};
linear merge(linear a, linear b){
linear ans = a;
for (int i = b.W; i >= 0; i -- ) if (b.num[i]) ans.insert(b.num[i]);
return ans;
}淀粉质板子(这一版很慢,要更新,但是咕了)
bool flg[N];
int wei, wsiz, /* sth. */;
int res, /* sth. */;
int fsiz(int f, int u){
int siz = 1;
for (auto [v, w] : g[u]){
if (v == f) continue;
if (flg[v]) continue;
siz += fsiz(u, v);
}
return siz;
}
int fwei(int f, int u, int sum){
int siz = 1, mx = 0;
for (auto [v, w] : g[u]){
if (v == f) continue;
if (flg[v]) continue;
int sz = fwei(u, v, sum);
mx = max(mx, sz), siz += sz;
}
mx = max(mx, sum - siz);
if (mx < wsiz) wei = u, wsiz = mx;
return siz;
}
void judge( /* sth. */ ){
/* sth. */
for (auto [v, w] : g[u]){
if (v == f) continue;
if (flg[v]) continue;
judge( /* sth. */ );
}
}
void calc(int now){
wei = now, wsiz = fsiz(0, now);
fwei(0, wei, wsiz), flg[wei] = 1;
/* sth. */
for (auto [v, w] : g[wei]){
if (flg[v]) continue;
/* sth. */
}
/* sth. */
for (auto [v, w] : g[wei]) if (!flg[v]) calc(v);
}Spfa 模版
bool spfa(int cnt, int st){
vector <bool> inq(cnt + 1, false); inq[st] = true;
vector <int> tot(cnt + 1, 0);
for (int i = 1; i <= cnt; i ++ ) dis[i] = inf;
queue <int> q; q.push(st); dis[st] = 0;
while (q.size()){
int u = q.front(); inq[u] = false; q.pop();
tot[u] ++; if (tot[u] > cnt) return false;
for (auto [v, w] : g[u]) if (dis[v] > dis[u] + w){
dis[v] = dis[u] + w;
if (!inq[v]) q.push(v), inq[v] = true;
}
} return true;
}Floyd 模版
void floyd(){
for (int k = 1; k <= n; k ++ )
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ )
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}矩阵快速幂mod
struct mat{
int n, m;
int a[M][M];
mat(){ n = 0, m = 0; }
mat(int _n, int _m){
n = _n, m = _m;
for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= m; j ++ ) a[i][j] = 0;
}
};
mat mul(mat a, mat b){
mat res(a.n, b.m);
for (int i = 1; i <= a.n; i ++ )
for (int j = 1; j <= b.m; j ++ )
for (int k = 1; k <= a.m; k ++ )
res.a[i][j] = (res.a[i][j] + 1ll * a.a[i][k] * b.a[k][j] % mod) % mod;
return res;
}
mat qpow(mat a, int b){
mat res(a.n, a.m);
for (int i = 1; i <= a.n; i ++ ) res.a[i][i] = 1;
while (b){
if (b & 1) res = mul(res, a);
a = mul(a, a), b >>= 1;
}
return res;
}
mat qpow(mat t, mat a, int b){
mat res = t;
while (b){
if (b & 1) res = mul(res, a);
a = mul(a, a), b >>= 1;
}
return res;
}矩阵快速幂
struct mat{
int n, m;
ll a[M][M];
mat(){ n = 0, m = 0; }
mat(int _n, int _m){
n = _n, m = _m;
for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= m; j ++ ) a[i][j] = INF;
}
};
mat mul(mat a, mat b){
mat res(a.n, b.m);
for (int i = 1; i <= a.n; i ++ )
for (int j = 1; j <= b.m; j ++ )
for (int k = 1; k <= a.m; k ++ )
res.a[i][j] = min(res.a[i][j], a.a[i][k] + b.a[k][j]);
return res;
}
mat qpow(mat a, int b){
mat res(a.n, a.m);
while (b){
if (b & 1) res = mul(res, a);
a = mul(a, a), b >>= 1;
}
return res;
}
mat qpow(mat t, mat a, int b){
mat res = t;
while (b){
if (b & 1) res = mul(res, a);
a = mul(a, a), b >>= 1;
}
return res;
}