洛谷 P2178 [NOI2015]品酒大会 后缀自动机

版权声明:2333 https://blog.csdn.net/liangzihao1/article/details/82117446

题目描述

一年一度的“幻影阁夏日品酒大会”隆重开幕了。大会包含品尝和趣味挑战 两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个奖项,吸引了众多品酒师参加。

在大会的晚餐上,调酒师 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 。

输入输出样例

输入样例#1:
10
ponoiiipoi
2 1 4 7 4 8 3 6 4 7
输出样例#1:
45 56
10 56
3 32
0 0
0 0
0 0
0 0
0 0
0 0
0 0
输入样例#2:
12
abaabaabaaba
1 -2 3 -4 5 -6 7 -8 9 -10 11 -12
输出样例#2:
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 。

分析:
其实是求有多少对后缀,满足他们的 l c p 大于等于 i ,并输出他们权值积的最大值。
根据一般套路,把串取反然后搞后缀自动机。然后相当于求两个前缀的 l c s ,也就是 f a i l 树上的 l c a 。我们可以先求出 l c p = i 的有多少对,然后跑后缀和。对于第二问,我们可以同样可以跑出 l c p = i 的最大积,但是这样可能有点麻烦,可以跑出 l c p >= i 的最大积,这样答案不会变,而且这个最大积就是子树合并,维护最大值和次大值,最小值和次小值。
然后感觉对sam有更深的认识。
r i g h t 集大小显然就是后缀为当前串的前缀个数,显然可以通过 f a i l 转移,如果当前串就是一个前缀,那么就要加 1

代码:

// luogu-judger-enable-o2
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define LL long long 

const int maxn=6e5+7;
const int inf=2e9;

using namespace std;

int a[maxn],size[maxn],sum[maxn],top[maxn],b[maxn];
char s[maxn];
int n,cnt;
LL ans1[maxn],ans2[maxn];

struct rec{
    int min1,min2,max1,max2;
}f[maxn];

struct node{
    int len,fail;
    int son[26];
}t[maxn];

void build_sam()
{
    cnt=1;
    int now=1,p,q,clone;
    for (int i=0;i<n;i++)
    {
        int c=s[i]-'a';
        p=now;
        now=++cnt;
        t[now].len=t[p].len+1;
        size[now]=sum[now]=1;
        f[now]=(rec){a[i+1],inf,a[i+1],-inf};
        while (p&&(!t[p].son[c])) t[p].son[c]=now,p=t[p].fail;
        if (!p) t[now].fail=1;
        else
        {
            q=t[p].son[c];
            if (t[p].len+1==t[q].len) t[now].fail=q;
            else
            {
                clone=++cnt;
                t[clone]=t[q];
                t[clone].len=t[p].len+1;
                f[clone]=(rec){inf,inf,-inf,-inf};
                t[now].fail=t[q].fail=clone;
                while (p&&(t[p].son[c]==q)) t[p].son[c]=clone,p=t[p].fail;
            }
        }
    }
}

rec calc(rec x,rec y)
{
    rec z;
    if (x.max1>y.max1)
    {
        z.max1=x.max1;
        z.max2=max(x.max2,y.max1);
    }
    else
    {
        z.max1=y.max1;
        z.max2=max(x.max1,y.max2);
    }
    if (x.min1<y.min1)
    {
        z.min1=x.min1;
        z.min2=min(x.min2,y.min1);
    }
    else
    {
        z.min1=y.min1;
        z.min2=min(x.min1,y.min2);
    }
    return z;
}

void prework()
{   
    for (int i=1;i<=cnt;i++) b[t[i].len]++;
    for (int i=1;i<=n;i++) b[i]+=b[i-1];
    for (int i=1;i<=cnt;i++) top[b[t[i].len]--]=i;
    for (int i=cnt;i>0;i--) size[t[top[i]].fail]+=size[top[i]];
    for (int i=cnt;i>0;i--)
    {
        int len=t[t[i].fail].len;
        ans1[len]+=(LL)sum[t[i].fail]*(LL)size[i];
        sum[t[i].fail]+=size[i];
    }
    for (int i=cnt;i>0;i--) f[t[top[i]].fail]=calc(f[top[i]],f[t[top[i]].fail]);    
    for (int i=cnt;i>0;i--)
    {
        int len=t[i].len;
        if (size[i]>1)ans2[len]=max(ans2[len],max((LL)f[i].max1*(LL)f[i].max2,(LL)f[i].min1*(LL)f[i].min2));
    }   
    for (int i=n-1;i>=0;i--)
    {
        ans1[i]+=ans1[i+1];
        ans2[i]=max(ans2[i],ans2[i+1]);
    }
}

int main()
{
    scanf("%d",&n); 
    scanf("%s",s);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    reverse(s,s+n);
    reverse(a+1,a+n+1);
    build_sam();
    for (int i=1;i<=n+1;i++) ans2[i]=-1e18;
    prework();
    for (int i=0;i<n;i++)
    {
        if (ans2[i]==-1e18) ans2[i]=0;
        printf("%lld %lld\n",ans1[i],ans2[i]);
    }
}

猜你喜欢

转载自blog.csdn.net/liangzihao1/article/details/82117446