这题调得我心疲力竭...Educational Round 5就过一段时间再发了_(:з」∠)_

先后找了三份AC代码对拍,结果有两份都会在某些数据上出点问题...这场的数据有点水啊_(:з」∠)_【然而卡掉本弱还是轻轻松松的】

题目链接:616F - Expensive Strings

题目大意:给出\(n\)个字符串\(t_i\)以及\(n\)个数\(c_i\),定义\(p_{s,i}\)为字符串\(s\)在\(t_i\)中出现的次数,\(f(s)=\sum_{i=1}^{n}c_i\cdot p_{s,i}\cdot |s|\),求\(f(s)\)的最大值

题解:考虑将\(n\)个字符串用互不相同的字符串连接起来,并求出新串的后缀数组。对于一段连续且合法(对任意i,有sa[i]对应的字符不为连接符)的区间\([l,r]\),其对应的答案就为\(min\left \{ height_i \right \}\cdot \sum c_j\),\(j\)为sa[i]所属原字符串的编号,对应所取的字符串就是这连续几个后缀的最长公共子串。因此若考虑暴力枚举所有的合法区间,会有下面的代码

for(int l=;l<=len_sum;l++)if(s[sa[l]-]>N)
for(int r=l;r<=len_sum;r++)if(s[sa[r]-]<N)break;else
{
LL k=;
int mi=N;
for(int i=l+;i<=r;i++)mi=min(mi,height[i]);
if(mi==)break;
if(mi==N)
{
mi=;
for(int i=sa[l]-;s[i]>N;i++)
mi++;
}
for(int i=l;i<=r;i++)k+=1ll*mi*c[belong[sa[i]-]];
ans=max(ans,k);
}

暴力

   但是这样是显然会TLE的,所以需要进一步优化

   考虑每一个height[i]的影响范围,即在该范围内,所有包含i的区间都以height[i]为最小值,此时原式的式子就为\(height_i\cdot \sum_{i=l}^{r}c_j\),这里\(l\),\(r\)表示的就是height[i]的影响范围,\(j\)依然为sa[i]所属原字符串的编号。预处理每个height[i]的影响范围以及\(c_j\)的前缀和就好了

#include<bits/stdc++.h>
using namespace std;
#define N 600001
#define LL long long
int wa[N+],wb[N+],wv[N+],Ws[N+];
int cmp(int *r,int a,int b,int l){return r[a]==r[b]&&r[a+l]==r[b+l];}
void da(const int r[],int sa[],int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=; i<m; i++) Ws[i]=;
for(i=; i<n; i++) Ws[x[i]=r[i]]++;
for(i=; i<m; i++) Ws[i]+=Ws[i-];
for(i=n-; i>=; i--) sa[--Ws[x[i]]]=i;
for(j=,p=; p<n; j*=,m=p)
{
for(p=,i=n-j; i<n; i++) y[p++]=i;
for(i=; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=; i<n; i++) wv[i]=x[y[i]];
for(i=; i<m; i++) Ws[i]=;
for(i=; i<n; i++) Ws[wv[i]]++;
for(i=; i<m; i++) Ws[i]+=Ws[i-];
for(i=n-; i>=; i--) sa[--Ws[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=; i<n; i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
return;
}
int sa[N],Rank[N],height[N];
void calheight(const int *r,int *sa,int n)
{
int i,j,k=;
for(i=; i<=n; i++) Rank[sa[i]]=i;
for(i=; i<n; height[Rank[i++]]=k)
for(k?k--:,j=sa[Rank[i]-]; r[i+k]==r[j+k]; k++);
for(int i=n;i>=;--i) ++sa[i],Rank[i]=Rank[i-];
}
char str[N];
LL ans,sum[N];
int n,c[N],s[N],leng[N],belong[N],_l[N],_r[N],len_sum;
void rua(int L,int R)
{
LL res=,x,le;
for(int i=L;i<=R;i++)
{
x=belong[sa[i]-];
le=leng[x]-sa[i]+;
if((i==R || height[i+]<le) && (i==L || height[i]<le))
res=max(res,1ll*le*c[x]);
}
for(int i=L;i<=R;i++)
res=max(res,1ll*height[i]*(sum[min(R,_r[i])]-sum[max(L,_l[i]-)-]));
ans=max(ans,res);
}
int main()
{
scanf("%d",&n);
scanf("%s",str);
int len=strlen(str);
for(int i=;i<len;i++)
belong[len_sum]=,s[len_sum++]=str[i]+N;
leng[]=len_sum;
for(int i=;i<=n;i++)
{
s[len_sum++]=i;
scanf("%s",str);
len=strlen(str);
for(int j=;j<len;j++)
belong[len_sum]=i,s[len_sum++]=str[j]+N;
leng[i]=len_sum;
}
da(s,sa,len_sum+,N+);
calheight(s,sa,len_sum);
for(int i=;i<=n;i++)
scanf("%d",&c[i]);
for(int i=;i<=len_sum;i++)
sum[i]=sum[i-]+c[belong[sa[i]-]];
_l[]=,_r[len_sum]=len_sum;
for(int i=;i<=len_sum;i++)
{
int _=i;
while(_> && height[i]<=height[_-])
_=_l[_-];
_l[i]=_;
}
for(int i=len_sum-;i>=;i--)
{
int _=i;
while(_<len_sum && height[i]<=height[_+])
_=_r[_+];
_r[i]=_;
}
for(int l=;l<=len_sum;l++)if(s[sa[l]-]>N)
{
int r=l;
while(r<=len_sum && s[sa[r]-]>N)r++;
rua(l,r-);
l=r;
}
printf("%I64d\n",ans);
}

[Educational Round 5][Codeforces 616F. Expensive Strings]的更多相关文章

  1. [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]

    这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...

  2. [Educational Round 3][Codeforces 609F. Frogs and mosquitoes]

    这题拖了快一周_(:з」∠)_就把这货单独拿出来溜溜吧~ 本文归属:Educational Codeforces Round 3 题目链接:609F - Frogs and mosquitoes 题目 ...

  3. [Codeforces Round #438][Codeforces 868D. Huge Strings]

    题目链接:868D - Huge Strings 题目大意:有\(n\)个字符串,\(m\)次操作,每次操作把两个字符串拼在一起,并询问这个新串的价值.定义一个新串的价值\(k\)为:最大的\(k\) ...

  4. [Educational Round 17][Codeforces 762F. Tree nesting]

    题目连接:678F - Lena and Queries 题目大意:给出两个树\(S,T\),问\(S\)中有多少连通子图与\(T\)同构.\(|S|\leq 1000,|T|\leq 12\) 题解 ...

  5. [Educational Round 13][Codeforces 678F. Lena and Queries]

    题目连接:678F - Lena and Queries 题目大意:要求对一个点集实现二维点对的插入,删除,以及询问\(q\):求\(max(x\cdot q+y)\) 题解:对每个点集内的点\(P( ...

  6. [Educational Round 10][Codeforces 652F. Ants on a Circle]

    题目连接:652F - Ants on a Circle 题目大意:\(n\)个蚂蚁在一个大小为\(m\)的圆上,每个蚂蚁有他的初始位置及初始面向,每个单位时间蚂蚁会朝着当前面向移动一个单位长度,在遇 ...

  7. [Educational Round 59][Codeforces 1107G. Vasya and Maximum Profit]

    咸鱼了好久...出来冒个泡_(:з」∠)_ 题目连接:1107G - Vasya and Maximum Profit 题目大意:给出\(n,a\)以及长度为\(n\)的数组\(c_i\)和长度为\( ...

  8. Codeforces Educational Round 33 题解

    题目链接   Codeforces Educational Round 33 Problem A 按照题目模拟,中间发现不对就直接输出NO. #include <bits/stdc++.h> ...

  9. Codeforces Educational Round 92 赛后解题报告(A-G)

    Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...

随机推荐

  1. 如何在IntelliJ IDEA中使用.ignore插件忽略不必要提交的文件

    如何在IntelliJ IDEA中使用.ignore插件忽略不必要提交的文件 最近初学Git,而且在使用的IDE是IntelliJ IDEA,发现IDEA在提交项目到本地仓库的时候,会把.idea文件 ...

  2. [物理学与PDEs]第1章习题13 静磁场的矢势在媒质交界面上的条件

    试讨论对静磁场的矢势, 如何决定其在媒质交界面上的条件. 解答: 由 $\rot{\bf A}={\bf B}$ 知 $$\bex \oint_l {\bf A}\cdot\rd {\bf l} =\ ...

  3. Unity3D 热更新方案总结

    如何评价腾讯在Unity下的xLua(开源)热更方案? Unity 游戏用XLua的HotFix实现热更原理揭秘 腾讯开源手游热更新方案,Unity3D下的Lua编程 [Unity]基于IL代码注入的 ...

  4. C++ 实现简单命令行学生管理系统

    C++ 实现简单命令行学生管理系统 预览: 编译环境是macOS.system("clear") 在windows下请换成 system("cls") #inc ...

  5. IDEA 中使用MyBatis-generator 自动生成MyBatis代码

    0.在Intellij IDEA创建maven项目 1. 在maven项目的pom.xml 添加mybatis-generator-maven-plugin 插件 <build> < ...

  6. JS基础之传参(值传递、对象传递)

    一.概念 我们需了解什么是按值传递(call by value),什么是按引用传递(call by reference).在计算机科学里,这个部分叫求值策略(Evaluation Strategy). ...

  7. 1、Flutter_初体验_创建第一个应用_AndroidStudio_windows

    1.前言 至于 Flutter 是啥,我就不在这啰嗦了,下面以 windows 为例,展示一下从安装 Flutter 到运行 APP 整个过程: 2.安装 Flutter 2.1.中文介绍文档:htt ...

  8. yum安装软件报错Segmentation fault处理

    yum安装软件报错Segmentation fault处理 在使用yum 更新软件时提示:Segmentation fault 中文错误提示: 段错误 [root@CMS-BAK:/usr/local ...

  9. 【原创】大数据基础之Marathon(1)简介、安装、使用

    marathon 1.6.322 官方:https://mesosphere.github.io/marathon/ 一 简介 Marathon is a production-grade conta ...

  10. js 校验 btc eth 地址

    NPM 安装 npm install wallet-address-validator Browser <script src="wallet-address-validator.mi ...