标签:pam clu space lse http last def code max
#include <bits/stdc++.h>
#define Sigma 30
#define MAXN 500010
#define int long long
using namespace std ;
int n, m, ans1, ans2 ; char s[MAXN], t[MAXN] ;
struct PAM{
int rt0, rt1, last, sz, f[MAXN], ch[MAXN][Sigma], fail[MAXN], len[MAXN] ;
void Init(){
sz = -1, rt0 = ++ sz, rt1 = ++ sz ;
fail[rt0] = fail[rt1] = rt1, len[rt0] = 0, len[rt1] = -1, last = rt0 ;
}
PAM(){Init();}
void Insert(int x, int p, char *s){
int u = last ;
while (s[p] != s[p - len[u] - 1]) u = fail[u] ;
if (!ch[u][x]){
int newq = ++ sz, fa = fail[u] ;
while (s[p] != s[p - len[fa] - 1]) fa = fail[fa] ;
fail[newq] = ch[fa][x], ch[u][x] = newq, len[newq] = len[u] + 2 ;
}
last = ch[u][x], f[last] ++ ;
}
void Solve() {
for(int i=sz;i;--i) f[fail[i]] += f[i];
}
}p,q;
void dfs(int x,int y) {
if(x+y>2) {
if(p.len[x]>ans1) {
ans1=p.len[x];
ans2=1;
}
else if(p.len[x]==ans1) {
ans2++;
}
}
for(int i=1;i<=26;i++)
if(p.ch[x][i] && q.ch[y][i]) dfs(p.ch[x][i],q.ch[y][i]);
}
signed main() {
ios::sync_with_stdio(false);
cin>>n>>m>>s+1>>t+1;
for(int i=1;i<=n;i++) p.Insert(s[i]-'a'+1,i,s);
for(int i=1;i<=m;i++) q.Insert(t[i]-'a'+1,i,t);
p.Solve(); q.Solve();
dfs(1,1);dfs(0,0);
cout<<ans1<<" "<<ans2<<endl;
}
标签:pam clu space lse http last def code max
原文地址:https://www.cnblogs.com/mollnn/p/12252847.html