传送门->

又称普及大会。

这题没什么好说的……后缀自动机裸题……并不对劲的人太菜了,之前照着标程逐行比对才过了这道题,前几天刚刚把这题一遍写对……

这题的输出和某两点相同后缀的长度有关,那么把串反过来就和相同前缀的长度有关。建出后缀自动机后,发现点u代表了right[u]个dis[fa[u]+1]~dis[u]相似的子串。此时如何统计答案就很显然了。

想着很简单,写着嘛…其实并不长?

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register LL i=(x);i<=(y);i++)
#define dwn(i,x,y) for(register LL i=(x);i>=(y);i--)
#define re register
#define maxn 600010
#define di ord[i]
#define LL long long
using namespace std;
inline LL read()
{
LL x=0,f=1;
char ch=getchar();
while(isdigit(ch)==0 && ch!='-')ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return x*f;
}
inline void write_(LL x)
{
LL f=0;char ch[20];
if(!x){putchar('0'),putchar(' ');return;}
if(x<0){putchar('-');x=-x;}
while(x)ch[++f]=x%10+'0',x/=10;
while(f)putchar(ch[f--]);
putchar(' ');
}
inline void writen(LL x)
{
LL f=0;char ch[20];
if(!x){puts("0");return;}
if(x<0){putchar('-');x=-x;}
while(x)ch[++f]=x%10+'0',x/=10;
while(f)putchar(ch[f--]);
putchar('\n');
}
LL ch[maxn][30],fa[maxn],r[maxn],dis[maxn],mn[maxn],mns[maxn],mx[maxn],mxs[maxn],lst,cnt;
LL cc[maxn],ord[maxn],rt,n,a[maxn],ans[maxn],num[maxn],inf[4];
char s[maxn];
LL gx(char c){return c-'a';}
void extend(char c)
{
LL np=++cnt,p=lst;dis[np]=dis[lst]+1,lst=np;
for(;p&&!ch[p][gx(c)];p=fa[p])ch[p][gx(c)]=np;
if(p==0)fa[np]=rt;
else
{
LL q=ch[p][gx(c)];
if(dis[q]==dis[p]+1)fa[np]=q;
else
{
LL nq=++cnt;dis[nq]=dis[p]+1;
fa[nq]=fa[q],fa[q]=fa[np]=nq;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
for(;ch[p][gx(c)]==q;p=fa[p])ch[p][gx(c)]=nq;
}
}
}
void upd(LL x,LL y)
{
r[x]+=r[y];
if(mn[x]<mn[y])mns[x]=min(mns[x],mn[y]);
else mns[x]=min(mns[y],mn[x]),mn[x]=mn[y];
if(mx[x]>mx[y])mxs[x]=max(mxs[x],mx[y]);
else mxs[x]=max(mxs[y],mx[x]),mx[x]=mx[y];
}
LL mul(LL x)
{
LL tmx,tmn;
if(mx[x]!=-inf[0]&&mxs[x]!=-inf[0])tmx=mx[x]*mxs[x];
else tmx=-inf[0];
if(mn[x]!=inf[0]&&mns[x]!=inf[0])tmn=mn[x]*mns[x];
else tmn=-inf[0];
return max(tmx,tmn);
}
void qsort()
{
memset(cc,0,sizeof(cc));
rep(i,1,cnt)cc[dis[i]]++;
rep(i,1,n)cc[i]+=cc[i-1];
rep(i,1,cnt)ord[cc[dis[i]]--]=i;
}
int main()
{
n=read();
lst=rt=++cnt;dis[0]=-1;
scanf("%s",s+1);
rep(i,1,n)a[i]=read();
rep(i,1,(n>>1))swap(s[i],s[n-i+1]),swap(a[i],a[n-i+1]);
rep(i,1,n)extend(s[i]);
qsort();
memset(inf,0x7f,sizeof(inf));
rep(i,1,cnt)mx[i]=mxs[i]=ans[i]=-inf[0],mn[i]=mns[i]=inf[0];
LL p=rt;
rep(i,1,n)p=ch[p][gx(s[i])],mx[p]=mn[p]=a[i],r[p]=1;
dwn(i,cnt,1)upd(fa[di],di);
rep(i,1,cnt)
ans[dis[i]]=max(mul(i),ans[dis[i]]),
num[dis[fa[i]]+1]+=r[i]*(r[i]-1)/2,num[dis[i]+1]-=r[i]*(r[i]-1)/2;
dwn(i,n,1)ans[i]=max(ans[i],ans[i+1]);
rep(i,0,n-1)
num[i]+=num[i-1],write_(num[i]),
writen(num[i]==0?0:ans[i]);
return 0;
}
/*
10
ponoiiipoi
2 1 4 7 4 8 3 6 4 7
*/

  

据说用后缀数组也能做,不过思维难度略高。

要是觉得对后缀自动机还不够熟悉,可以试一下bzoj3238: [Ahoi2013],也是后缀自动机很好写(才怪)的题。

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register LL i=(x);i<=(y);++i)
#define dwn(i,x,y) for(register LL i=(x);i>=(y);--i)
#define re register
#define maxn 1000010
#define LL long long
using namespace std;
inline LL read()
{
LL x=0,f=1;
char ch=getchar();
while(isdigit(ch)==0 && ch!='-')ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return x*f;
}
inline void write(LL x)
{
LL f=0;char ch[20];
if(!x){puts("0");return;}
if(x<0){putchar('-');x=-x;}
while(x)ch[++f]=x%10+'0',x/=10;
while(f)putchar(ch[f--]);
putchar('\n');
}
LL ch[maxn][30],fa[maxn],dis[maxn],yes[maxn],lst,rt,cntnd;
LL v[maxn],nxt[maxn],fir[maxn],siz[maxn],ans[maxn],cntrd,len;
char s[maxn];
void ade(LL u1,LL v1){v[++cntrd]=v1,nxt[cntrd]=fir[u1],fir[u1]=cntrd;}//u->v
LL gx(char c){return c-'a';}
void extend(char c)
{
LL np=++cntnd,p=lst;dis[np]=dis[p]+1,lst=np;
for(;p&&ch[p][gx(c)]==0;p=fa[p])ch[p][gx(c)]=np;
if(!p)fa[np]=rt;
else
{
LL q=ch[p][gx(c)];
if(dis[q]==dis[p]+1)fa[np]=q;
else
{
LL nq=++cntnd;dis[nq]=dis[p]+1;
fa[nq]=fa[q],fa[np]=fa[q]=nq;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
for(;ch[p][gx(c)]==q;p=fa[p])ch[p][gx(c)]=nq;
}
}
}
void getans(LL u)
{
if(yes[u])siz[u]=1;
LL tmp=0;
for(LL k=fir[u];k!=-1;k=nxt[k])
{
getans(v[k]);
siz[u]+=siz[v[k]];
}
for(LL k=fir[u];k!=-1;k=nxt[k])
tmp+=siz[v[k]]*(siz[u]-siz[v[k]]);
ans[u]=tmp*dis[u]+yes[u]*(siz[u]-1ll)*dis[u];
}
int main()
{
memset(fir,-1,sizeof(fir));
memset(ans,0,sizeof(ans));
lst=rt=++cntnd;dis[0]=-1;
scanf("%s",s+1);
len=strlen(s+1);
rep(i,1,len)swap(s[i],s[len-i+1]);
rep(i,1,len)extend(s[i]);
LL p=rt;
rep(i,1,cntnd)ade(fa[i],i);
rep(i,1,len)p=ch[p][gx(s[i])],yes[p]=1;
getans(rt);
LL ansans=0;
rep(i,1,cntnd)ansans+=ans[i];
write((len+1ll)*len*(len-1ll)/2ll-ansans);
return 0;
}
/*
cacao
*/

 最后祝您身体健康,再见。

并不对劲的bzoj4199: [Noi2015]品酒大会的更多相关文章

  1. [UOJ#131][BZOJ4199][NOI2015]品酒大会 后缀数组 + 并查集

    [UOJ#131][BZOJ4199][NOI2015]品酒大会 试题描述 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个 ...

  2. [UOJ#131][BZOJ4199][NOI2015]品酒大会

    [UOJ#131][BZOJ4199][NOI2015]品酒大会 试题描述 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个 ...

  3. [bzoj4199][Noi2015]品酒大会_后缀自动机_后缀树_树形dp

    品酒大会 bzoj-4199 Noi-2015 题目大意:给定一个字符串,如果其两个子串的前$r$个字符相等,那么称这两个子串的开头两个位置$r$相似.如果两个位置勾兑在一起那么美味度为两个位置的乘积 ...

  4. [BZOJ4199][NOI2015]品酒大会

    #131. [NOI2015]品酒大会 统计 描述 提交 自定义测试 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个奖项, ...

  5. bzoj4199: [Noi2015]品酒大会(后缀数组)

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

  6. BZOJ4199 [Noi2015]品酒大会 【后缀数组 + 单调栈 + ST表】

    题目 一年一度的"幻影阁夏日品酒大会"隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发"首席品 酒家"和"首席猎手"两个奖项,吸 ...

  7. 2019.02.28 bzoj4199: [Noi2015]品酒大会(sam+线段树)

    传送门 题意:给一个串,每个位置有一个权值,当S[s...s+len−1]=S[t...t+len−1]&&S[s...s+len]̸=S[t..t+len]S[s...s+len-1 ...

  8. bzoj千题计划257:bzoj4199: [Noi2015]品酒大会

    http://www.lydsy.com/JudgeOnline/problem.php?id=4199 求出后缀数组的height 从大到小枚举,合并 维护组内 元素个数,最大.次大.最小.次小 # ...

  9. [BZOJ4199][Noi2015]品酒大会 树形DP+后缀自动机

    由于要找后缀的前缀,所以先用反串建立SAM. link边组成了后缀树. 两个子串的最长公共前缀是LCA的step 树形dp即可. #include<iostream> #include&l ...

随机推荐

  1. Leetcode 300.最长上升子序列

    最长上升子序列 给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的 ...

  2. Linux 修改主机名

    1 vi /etc/sysconfig/network 2 vi /etc/hosts 3 hostname xxx 4 Done! 退出重连后生效

  3. [POJ1984]Navigation Nightmare

    [POJ1984]Navigation Nightmare 试题描述 Farmer John's pastoral neighborhood has N farms (2 <= N <= ...

  4. 【带权并查集】HDU 3047 Zjnu Stadium

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 [题意] http://blog.csdn.net/hj1107402232/article/detail ...

  5. IIS文件存在但报404问题解决

    遇到一个奇怪的问题,在IIS7.5中,一些样式和JS文件存在,但访问就是报404. 根据网上搜索到的解决方法,发现解决不了,不同同样的问题引起的. 网上解决: 1.没有配置合适的MIME信息,通过添加 ...

  6. Java调用K3Cloud的密码加密算法实现登录密码检验

    背景: 最近要开始做K3Cloud移动,BOS平台的移动单据收费,就想单独做移动模块,搭建环境:后台SSH2,前端Android.在手机端登录时通过Ajax方式传递用户名和密码到后台校验,后台在去K3 ...

  7. 如何在Eclipse中生成Native类对应的JNI的.h文件

    1 致谢 感谢super_level网友 他的博客写的很清楚 给了我很多帮助 链接如下:http://blog.csdn.net/super_level/article/details/2124353 ...

  8. HDU 4622 (后缀自动机)

    HDU 4622 Reincarnation Problem : 给一个串S(n <= 2000), 有Q个询问(q <= 10000),每次询问一个区间内本质不同的串的个数. Solut ...

  9. windows 下安装Apache httpd 只需三步

    1.下载 Apache 官网地址:http://httpd.apache.org/docs/current/platform/windows.html#down 找到这个, 看到这几个选项: Apac ...

  10. msp430入门编程35

    msp430中C语言的可移植--规划软件层次