题解
后缀自动机的每个点都代表了一些子串,我可以在 p a r e n t parent parent树上 d p dp dp,求出每个点上的 e n d p o s endpos endpos集合大小,这样可以得到第一问的答案
另外维护 e n d p o s endpos endpos的最大值、次大值、最小值、次小值,可以得到第二问的答案
代码
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<62)
#define eps 1e-8
#define maxn 300010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
struct SAM
{
int tot, las, fa[maxn<<1], len[maxn<<1], ch[maxn<<1][26];
ll pref[maxn<<1];
void init()
{
int i;
rep(i,tot)cl(ch[i]),fa[i]=len[i]=0;
rep(i,600000)pref[i]=linf;
tot=las=1;
}
void append(int c, int r)
{
int p(las);
len[las=++tot]=len[p]+1;
pref[las]=r;
for(;p and !ch[p][c];p=fa[p])ch[p][c]=las;
if(!p)fa[las]=1;
else
{
int q=ch[p][c];
if(len[q]==len[p]+1)fa[las]=q;
else
{
int qq=++tot;
memcpy(ch[qq],ch[q],sizeof(ch[q]));
fa[qq]=fa[q];
len[qq]=len[p]+1;
fa[q]=fa[las]=qq;
for(;ch[p][c]==q;p=fa[p])ch[p][c]=qq;
}
}
}
int mov(int p, int c){return max(1,ch[p][c]);}
}sam;
struct Graph
{
int etot, head[maxn<<1], to[maxe], next[maxe], w[maxe];
void clear(int N)
{
for(int i=1;i<=N;i++)head[i]=0;
etot=0;
}
void adde(int a, int b, int c=0){to[++etot]=b;w[etot]=c;next[etot]=head[a];head[a]=etot;}
#define forp(pos,G) for(auto p=G.head[pos];p;p=G.next[p])
}G;
ll n, a[maxn], ans1[maxn], ans2[maxn];
typedef pair<pll,pll> P; //( (mn,mn2) , (mx,mx2) )
P merge(P p1, P p2)
{
P p3;
vector<ll> mn({p1.first.first,p1.first.second,p2.first.first,p2.first.second});
sort(mn.begin(),mn.end());
p3.first.first=mn[0], p3.first.second=mn[1];
vector<ll> mx({p1.second.first,p1.second.second,p2.second.first,p2.second.second});
sort(mx.begin(),mx.end());
p3.second.first=mx[3], p3.second.second=mx[2];
return p3;
}
ll getans(P p)
{
ll ans=-linf;
if( abs(p.first.second) < linf) ans=max(ans,p.first.first*p.first.second);
if( abs(p.second.second) < linf) ans=max(ans,p.second.first*p.second.second);
return ans;
}
P pp[maxn<<1];
ll cnt[maxn<<1];
void dfs(ll u)
{
cnt[u] = (sam.pref[u]!=linf);
forp(u,G)
{
auto v=G.to[p];
dfs(v);
pp[u]=merge(pp[u],pp[v]);
cnt[u] += cnt[v];
}
ans1[sam.len[u]] += cnt[u]*(cnt[u]-1)/2;
if(u>1)ans1[sam.len[sam.fa[u]]] -= cnt[u]*(cnt[u]-1)/2;
ans2[sam.len[u]] = max( ans2[sam.len[u]], getans(pp[u]) );
}
char s[maxn];
int main()
{
ll i;
n = read();
scanf("%s",s+1); reverse(s+1,s+n+1);
rep(i,n)a[n-i+1]=read();
sam.init();
rep(i,n)sam.append(s[i]-'a',a[i]);
rep(i,sam.tot)
{
if(i>1)G.adde(sam.fa[i],i);
pp[i] = make_pair( pll(linf,linf) , pll(-linf,-linf) );
if(sam.pref[i]!=linf)pp[i].first.first=pp[i].second.first=sam.pref[i];
}
rep(i,n)ans1[i]=0, ans2[i]=-linf;
dfs(1);
for(i=n-1;~i;i--)
{
ans1[i] += ans1[i+1];
ans2[i] = max(ans2[i],ans2[i+1]);
}
for(i=0;i<n;i++)printf("%lld %lld\n",ans1[i],(ans2[i]==-linf)?0:ans2[i]);
return 0;
}