bzoj4199 [Noi2015]品酒大会

Description


一年一度的“幻影阁夏日品酒大会”隆重开幕了。大会包含品尝和趣味挑战 两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个奖项,吸引了众多品酒师参加。
在大会的晚餐上,调酒师 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 相似”的酒调兑可以得到的美味度的最大值。

Solution


感觉后缀数组的方法比较好理解,但是我不会啊

可以发现实际上要求所有相等的字串的某些神奇值,把字符串倒过来插进sam里面建出parent树,维护一下最大、最小、次大、次小即可
注意空间的问题和开LL

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define drp(i,st,ed) for (int i=st;i>=ed;--i)
#define fill(x,t) memset(x,t,sizeof(x))
#define copy(x,t) memcpy(x,t,sizeof(x))

typedef long long LL;
const LL INF=1152921504606846976LL;
const int N=300005;

struct edge {int x,y,next;} e[N*4];

int rec[N*4][26],fa[N*4],len[N*4],tot=1,last=1;
int a[N],size[N*4];
int ls[N*4],edCnt;
LL ans1[N],ans2[N],mx[N*4],mn[N*4];

char str[N];

int read() {
    int x=0,v=1; char ch=getchar();
    for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
    for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
    return x*v;
}

void add_edge(int x,int y) {
    e[++edCnt]=(edge) {x,y,ls[x]}; ls[x]=edCnt;
}

void extend(int ch,int w) {
    int p,q,np,nq;
    p=last; last=np=++tot;
    size[np]=1; len[np]=len[p]+1;
    mx[np]=mn[np]=w;
    for (;p&&!rec[p][ch];p=fa[p]) rec[p][ch]=np;
    if (!p) fa[np]=1;
    else {
        q=rec[p][ch];
        if (len[p]+1==len[q]) fa[np]=q;
        else {
            nq=++tot; len[nq]=len[p]+1;
            copy(rec[nq],rec[q]);
            fa[nq]=fa[q];
            fa[np]=fa[q]=nq;
            for (;p&&rec[p][ch]==q;p=fa[p]) rec[p][ch]=nq;
        }
    }
}

void pre_work() {
    rep(i,2,tot) add_edge(fa[i],i);
}

void dfs(int x) {
    if (!mx[x]&&!mn[x]) mx[x]=-INF,mn[x]=INF;
    for (int i=ls[x];i;i=e[i].next) {
        dfs(e[i].y);
        if (mx[x]!=-INF&&mn[x]!=INF&&mx[e[i].y]!=-INF&&mn[e[i].y]!=INF) {
            ans2[len[x]]=std:: max(ans2[len[x]],mx[x]*mx[e[i].y]);
            ans2[len[x]]=std:: max(ans2[len[x]],mn[x]*mn[e[i].y]);
        }
        ans1[len[x]]+=(LL)size[x]*size[e[i].y];
        mn[x]=std:: min(mn[x],mn[e[i].y]);
        mx[x]=std:: max(mx[x],mx[e[i].y]);
        size[x]+=size[e[i].y];
    }
}

int main(void) {
    int n=read();
    rep(i,0,n) ans2[i]=-INF;
    scanf("%s",str+1);
    rep(i,1,n) a[i]=read();
    drp(i,n,1) extend(str[i]-'a',a[i]);
    pre_work(); dfs(1);
    drp(i,n-1,0) ans2[i]=std:: max(ans2[i+1],ans2[i]);
    drp(i,n-1,0) ans1[i]+=ans1[i+1];
    rep(i,0,n-1) if (!ans1[i]) puts("0 0");
    else printf("%lld %lld\n", ans1[i],ans2[i]);
    return 0;
}

来自 https://www.luogu.org/problemnew/show/P2178

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值