#include<bits/stdc++.h>usingnamespace std;constint N =1e6+5;int ch[N][26],nxt[N],cnt[N],idx;voidinsert(char*s){int p =0;for(int i =0;s[i];i++){int j = s[i]-'a';if(!ch[p][j]) ch[p][j]=++idx;
p = ch[p][j];}
cnt[p]++;}voidbuild(){
queue<int> q;for(int i =0;i<26;i++)if(ch[0][i]) q.push(ch[0][i]);while(!q.empty()){int u = q.front();
q.pop();for(int i =0;i<26;i++){int v = ch[u][i];if(v) nxt[v]= ch[nxt[u]][i],q.push(v);else ch[u][i]= ch[nxt[u]][i];}}}intquery(char*s){int ans =0;for(int k =0,i =0;s[k];k++){
i = ch[i][s[k]-'a'];for(int j = i;j &&~cnt[j];j = nxt[j])
ans += cnt[j],cnt[j]=-1;}return ans;}int n;char s[N],t[N];signedmain(){
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
cin>>n;for(int i =1;i<=n;i++){
cin>>s;insert(s);}
cin>>t;build();
cout<<query(t);return0;}
structmatrix{int M[40][40];matrix(){memset(M,0,sizeof(M));}matrix(bool flag){for(int i =0; i <(1<< m); i++) M[i][i]=1;}int*operator[](constint pos){return M[pos];}friend matrix operator*(matrix a, matrix b){
matrix c;for(int i =0; i <(1<< m); i++)for(int j =0; j <(1<< m); j++)for(int K =0; K <(1<< m); K++)
c[i][j]= c[i][j]+ a[i][K]* b[K][j];return c;}};
matrix qpow(matrix a,int k){
matrix res(true);while(k){if(k &1) res =(res * a);
a =(a * a);
k >>=1;}return res;}
Johnson
#include<bits/stdc++.h>usingnamespace std;#defineintlonglong#definelllonglongconstint N =3005;constint INF =1e9;constint M =6005;structEdge{int to,val;Edge(int t,int v):to(t),val(v){}inlinebooloperator<(const Edge &a)const{return a.val < val;}};
vector<Edge> edge[N];int n,m,u,v,w;
ll h[N],dis[N];int Neg[N];bool inq[N],vis[N];inlineboolspfa(){
deque<int> dq;
h[0]=0;
Neg[0]=1;
inq[0]=true;
dq.push_back(0);while(!dq.empty()){int t = dq.front();
dq.pop_front();
inq[t]=false;for(Edge a:edge[t]){if(h[a.to]> h[t]+ a.val){
h[a.to]= h[t]+ a.val;if(!inq[a.to]){
inq[a.to]=true;
dq.push_back(a.to);// ??????SLF
Neg[a.to]++;if(Neg[a.to]== n+1)returntrue;}}}}returnfalse;}inlinevoiddij(int s){For(u,0,n) vis[u]=false,dis[u]= INF;
priority_queue<Edge> q;
q.push(Edge(s,0));
dis[s]=0;while(!q.empty()){
Edge t = q.top();
q.pop();if(vis[t.to])continue;
vis[t.to]=true;for(Edge a:edge[t.to]){if(vis[a.to])continue;if(dis[a.to]> dis[t.to]+ a.val){
dis[a.to]= dis[t.to]+ a.val;
q.push(Edge(a.to,dis[a.to]));}}}}signedmain(){
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
cin>>n>>m;For(u,0,n) h[u]= INF,dis[u]= INF;for(int i =1;i<=m;i++){
cin>>u>>v>>w;
edge[u].push_back(Edge(v,w));}for(int i =1;i<=n;i++) edge[0].push_back(Edge(i,0));if(spfa()) cout<<-1,exit(0);for(int i =1;i<=n;i++){for(Edge &a:edge[i]){// copy ????? important
a.val += h[i]- h[a.to];}}for(int i =1;i<=n;i++){
ll ans =0;dij(i);For(j,1,n){if(dis[j]== INF){
ans += j*INF;}else ans += j*(dis[j]+ h[j]- h[i]);}
cout<<ans<<'\n';}return0;}
克鲁斯卡尔(kru)
#include<bits/stdc++.h>usingnamespace std;#defineFor(i,n)for(registerint i =1;i<=n;i++)constint N =5005;constint M =200005;structEdge{int u,v,w;}edge[M<<1];int s[N<<1],cnt,n,m,ans;inlineboolcmp(const Edge &a,const Edge &b){return a.w < b.w;}inlineintfind(int x){if(x != s[x]) s[x]=find(s[x]);return s[x];}inlinevoidkru(){sort(edge+1,edge+1+m,cmp);int f1,f2;For(i,n) s[i]= i;For(i,m){
f1 =find(edge[i].u);
f2 =find(edge[i].v);if(f1 != f2){
ans += edge[i].w;
s[f1]= f2;
cnt++;if(cnt == n-1)break;}}if(cnt != n-1) cout<<"orz";else cout<<ans;}signedmain(){
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
cin>>n>>m;for(int i =1;i<=m;i++) cin>>edge[i].u>>edge[i].v>>edge[i].w;kru();return0;}
Prim
#include<bits/stdc++.h>usingnamespace std;#defineFor(i,n)for(registerint i =1;i<=n;i++)constint N =5005;constint M =200005;structnode{int to,val;node(int t,int v):to(t),val(v){}inlinebooloperator<(const node &a)const{return a.val < val;}};int n,m,cnt,now =1;bool vis[N];
vector<node> edge[N];inlinevoidprim(){int ans =0;
priority_queue<node> q;
q.push(node(1,0));while(!q.empty()){
node t = q.top();
q.pop();if(vis[t.to])continue;
vis[t.to]=true;
ans += t.val;
cnt++;if(!(cnt^n))break;for(node i:edge[t.to]){if(vis[i.to])continue;
q.push(node(i.to,i.val));}}if(cnt^n) cout<<"orz";else cout<<ans;}signedmain(){
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
cin>>n>>m;int u,v,w;For(i,m){
cin>>u>>v>>w;
edge[u].push_back(node{v,w});
edge[v].push_back(node{u,w});}prim();return0;}
manacher
#include<bits/stdc++.h>usingnamespace std;#defineDec(i,a,n)for(registerint i = a;i>=n;i--)#defineLOG2(x)__lg(x)constint N =2e7+5;char s[N],a[N<<1];int n,dp[N<<1];voidchange(){
n =strlen(s);int k =0;
a[k++]='$';
a[k++]='#';For(i,0,n-1){
a[k++]= s[i];
a[k++]='#';}
a[k++]='&';
n = k;}voidmanacher(){int R =0,C;For(i,1,n-1){if(i < R) dp[i]=min(dp[(C<<1)- i],dp[C]+ C - i);else dp[i]=1;while(a[i + dp[i]]== a[i - dp[i]]) dp[i]++;if(dp[i]+ i > R){
R = dp[i]+ i;
C = i;}}}signedmain(){char cc;int top =0;while(cc =getchar()) s[top++]= cc;change();manacher();int ans =1;For(i,0,n -1) ans =max(ans,dp[i]);
IO<<ans-1;return0;}
失配树
#include<bits/stdc++.h>usingnamespace std;#defineDec(i,a,n)for(registerint i = a;i>=n;i--)#defineLOG2(x)__lg(x)constint N =1e6+5;char s[N];int len,m,fa[N][22],dep[N];voidgetfail(){
fa[0][0]=fa[1][0]=0,dep[0]=0,dep[1]=1;for(int i=2,j=0;i<=len;++i){while(j!=0&&s[j+1]!=s[i]) j=fa[j][0];if(s[j+1]==s[i])++j;
fa[i][0]=j,dep[i]=dep[j]+1;}}intLCA(int&x,int&y){if(dep[x]< dep[y])swap(x,y);Dec(i,21,0)if(dep[fa[x][i]]>= dep[y]) x = fa[x][i];Dec(i,21,0)if(fa[x][i]^ fa[y][i]) x = fa[x][i],y = fa[y][i];return fa[x][0];}signedmain(){
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
cin>>(s +1);
len =strlen(s +1);getfail();
cin>>m;For(i,1,21)For(j,1,len) fa[j][i]= fa[fa[j][i-1]][i-1];while(m--){int p,q;
cin>>p>>q;
cout<<LCA(p,q)<<'\n';}return0;}