题目描述
一年一度的“幻影阁夏日品酒大会”隆重开幕了。大会包含品尝和趣味挑战 两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个奖项,吸引了众多品酒师参加。
在大会的晚餐上,调酒师 Rainbow 调制了 n 杯鸡尾酒。这 n 杯鸡尾酒排成一行,其中第 n 杯酒 (1 ≤ i ≤ n) 被贴上了一个标签si,每个标签都是 26 个小写 英文字母之一。设 str(l, r)表示第 l 杯酒到第 r 杯酒的 r − l + 1 个标签顺次连接构成的字符串。若 str(p, po) = str(q, qo),其中 1 ≤ p ≤ po ≤ n, 1 ≤ q ≤ qo ≤ n, p ≠ q, po − p + 1 = qo − q + 1 = r ,则称第 p 杯酒与第 q 杯酒是“ r 相似” 的。当然两杯“ r 相似”(r > 1)的酒同时也是“ 1 相似”、“ 2 相似”、……、“ (r − 1) 相似”的。特别地,对于任意的 1 ≤ p , q ≤ n , p ≠ q ,第 p 杯酒和第 q 杯酒都 是“ 0 相似”的。
在品尝环节上,品酒师 Freda 轻松地评定了每一杯酒的美味度,凭借其专业的水准和经验成功夺取了“首席品酒家”的称号,其中第 i 杯酒 (1 ≤ i ≤ n) 的 美味度为 ai 。现在 Rainbow 公布了挑战环节的问题:本次大会调制的鸡尾酒有一个特点,如果把第 p 杯酒与第 q 杯酒调兑在一起,将得到一杯美味度为 ap*aq 的 酒。现在请各位品酒师分别对于 r = 0,1,2, ⋯ , n − 1 ,统计出有多少种方法可以 选出 2 杯“ r 相似”的酒,并回答选择 2 杯“ r 相似”的酒调兑可以得到的美味度的最大值。
输入输出格式
输入格式:
第 1 行包含 1 个正整数 n ,表示鸡尾酒的杯数。
第 2 行包含一个长度为 n 的字符串 S,其中第 i 个字符表示第 i 杯酒的标签。
第 3 行包含 n 个整数,相邻整数之间用单个空格隔开,其中第 i 个整数表示第 i 杯酒的美味度 ai 。
输出格式:
包括 n 行。第 i 行输出 2 个整数,中间用单个空格隔开。第 1 个整 数表示选出两杯“ (i − 1) 相似”的酒的方案数,第 2 个整数表示选出两杯 “ (i − 1) 相似”的酒调兑可以得到的最大美味度。若不存在两杯“ (i − 1) 相似” 的酒,这两个数均为 0 。
输入输出样例
10 ponoiiipoi 2 1 4 7 4 8 3 6 4 7
45 56 10 56 3 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0
12 abaabaabaaba 1 -2 3 -4 5 -6 7 -8 9 -10 11 -12
66 120 34 120 15 55 12 40 9 27 7 16 5 7 3 -4 2 -4 1 -4 0 0 0 0
说明
【样例说明 1】
用二元组 (p, q) 表示第 p 杯酒与第 q 杯酒。
0 相似:所有 45 对二元组都是 0 相似的,美味度最大的是 8 × 7 = 56 。
1 相似: (1,8) (2,4) (2,9) (4,9) (5,6) (5,7) (5,10) (6,7) (6,10) (7,10) ,最大的 8 × 7 = 56 。
2 相似: (1,8) (4,9) (5,6) ,最大的 4 × 8 = 32 。
没有 3,4,5, ⋯ ,9 相似的两杯酒,故均输出 0 。
【时限1s,内存512M】
【分析】
口胡几句...首先我们只用考虑i相似的情况,i-1,i-2...相似什么的倒着累加就好了。
那么对于两个位置i,j,它们的相似度就是suffix(i),suffix(j)的lcp长度。
这时候我们想起了后缀树一个神奇的性质:两后缀的lcp长度=两后缀对应节点的LCA的step值。
那么搞一个反串,弄一下SAM,parent树就是原串后缀树了。
每个np点打上标记,在parent树上维护mx,mn和size,统计答案。问题解决。注意初始化。
为啥维护min? 负负得正
【代码】
#include<iostream>
#include<cstring>
#include<cstdio>
#define inf 1e9+7
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=600005;
char s[mxn];
int p,np,q,nq,tot,cnt,root;
ll num[mxn],mx[mxn],mn[mxn],ans[mxn];
struct edge {int to,next;} f[mxn];
int step[mxn],pre[mxn],son[mxn][28],size[mxn],head[mxn],a[mxn];
inline ll abs(ll x) {if(x<0) return -x;return x;}
inline void add(int u,int v)
{
f[++cnt].to=v,f[cnt].next=head[u],head[u]=cnt;
}
inline void sam(int w,int c)
{
p=np;
step[np=(++tot)]=step[p]+1;
mx[np]=mn[np]=a[w],size[np]=1;
while(p && !son[p][c])
son[p][c]=np,p=pre[p];
if(!p)
{
pre[np]=1;return;
}
q=son[p][c];
if(step[q]==step[p]+1) pre[np]=q;
else
{
step[nq=(++tot)]=step[p]+1;
memcpy(son[nq],son[q],sizeof son[q]);
pre[nq]=pre[q];
pre[np]=pre[q]=nq;
while(p && son[p][c]==q)
son[p][c]=nq,p=pre[p];
}
}
inline void dfs(int u)
{
for(int i=head[u];i;i=f[i].next)
{
int v=f[i].to;
dfs(v);
num[step[u]]+=(ll)size[u]*size[v];
if(abs(mx[u])<inf && abs(mx[v]<inf))
ans[step[u]]=max(ans[step[u]],mx[u]*mx[v]);
if(abs(mn[u])<inf && abs(mx[v]<inf))
ans[step[u]]=max(ans[step[u]],mn[u]*mn[v]);
size[u]+=size[v];
mx[u]=max(mx[u],mx[v]);
mn[u]=min(mn[u],mn[v]);
}
}
int main()
{
int i,j,len;
memset(ans,-0x3f,sizeof ans);
memset(mx,-0x3f,sizeof mx);
memset(mn,0x3f,sizeof mn);
np=tot=root=1;
scanf("%d",&len);
scanf("%s",s+1);
fo(i,1,len) scanf("%d",&a[i]);
fo(i,1,len)
sam(len-i+1,s[len-i+1]-'a'+1);
fo(i,2,tot) add(pre[i],i);
dfs(1);
for(i=len-1;i>=0;i--) num[i]+=num[i+1];
for(i=len-1;i>=0;i--) ans[i]=max(ans[i],ans[i+1]);
for(i=len-1;i>=0;i--) if(num[i]==0) ans[i]=0;
fo(i,0,len-1) printf("%lld %lld\n",num[i],ans[i]);
return 0;
}