CodeForces - 616F:Expensive Strings (后缀自动机)
You are given n strings ti. Each string has cost ci.
Let's define the function of string , where ps, i is the number of occurrences of s in ti, |s| is the length of the string s. Find the maximal value of function f(s) over all strings.
Note that the string s is not necessarily some string from t.
The first line contains the only integer n (1 ≤ n ≤ 105) — the number of strings in t.
Each of the next n lines contains contains a non-empty string ti. ti contains only lowercase English letters.
It is guaranteed that the sum of lengths of all strings in t is not greater than 5·105.
The last line contains n integers ci ( - 107 ≤ ci ≤ 107) — the cost of the i-th string.
Output
Print the only integer a — the maximal value of the function f(s) over all strings s. Note one more time that the string s is not necessarily from t.
Examples
2
aa
bb
2 1
4
2
aa
ab
2 1
5
#include<bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(int i=a;i>=b;i--)
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
char c[maxn],s[maxn]; int End[maxn];
struct SAM{
int ch[maxn][],fa[maxn],maxlen[maxn],cnt,last;
int a[maxn],b[maxn],num[maxn]; ll val[maxn],ans=;
void init()
{
cnt=; memset(ch[],,sizeof(ch[]));
}
int add(int x)
{
int np=++cnt,p=last; last=np;
maxlen[np]=maxlen[p]+;memset(ch[np],,sizeof(ch[np]));
while(p&&!ch[p][x]) ch[p][x]=np,p=fa[p];
if(!p) fa[np]=;
else {
int q=ch[p][x];
if(maxlen[q]==maxlen[p]+) fa[np]=q;
else {
int nq=++cnt; maxlen[nq]=maxlen[p]+;
fa[nq]=fa[q]; fa[q]=fa[np]=nq;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
while(p&&ch[p][x]==q) ch[p][x]=nq,p=fa[p];
}
}
}
void sort()
{
rep(i,,cnt) a[i]=;
rep(i,,cnt) a[maxlen[i]]++;
rep(i,,cnt) a[i]+=a[i-];
rep(i,,cnt) b[a[maxlen[i]]--]=i;
}
void solve(int N)
{
sort();
rep(i,,N) scanf("%d",&num[i]);
rep(i,,N){
int now=;
rep(j,End[i-]+,End[i]){
now=ch[now][s[j]-'a'];
val[now]+=num[i];
}
}
rep2(i,cnt,) val[fa[b[i]]]+=val[b[i]];
rep(i,,cnt) ans=max(ans,val[i]*maxlen[i]);
}
}T;
int a[maxn];
int main()
{
int N,L;
scanf("%d",&N);
T.init();
rep(i,,N) {
scanf("%s",c+);
L=strlen(c+); T.last=;
rep(j,,L) T.add(c[j]-'a');
rep(j,,L) s[End[i-]+j]=c[j];
End[i]=End[i-]+L;
}
T.solve(N);
printf("%lld\n",T.ans);
return ;
}
N个串,给个串有一个价值num[i],现在让你找一个串X,它在第i个字符串出现次数为pi,而且∑len(X)*num[i]*p[i]最大。
思路:后缀自动机,对于每个集合,num之和一样,所以肯定选择最长的一个。
CodeForces - 616F:Expensive Strings (后缀自动机)的更多相关文章
- [Educational Round 5][Codeforces 616F. Expensive Strings]
这题调得我心疲力竭...Educational Round 5就过一段时间再发了_(:з」∠)_ 先后找了三份AC代码对拍,结果有两份都会在某些数据上出点问题...这场的数据有点水啊_(:з」∠)_[ ...
- Codeforces 452E Three Strings(后缀自动机)
上学期很认真地学了一些字符串的常用工具,各种 suffix structre,但是其实对后缀自动机这个部分是理解地不太透彻的,以致于看了师兄A这题的代码后,我完全看不懂,于是乎重新看回一些学习后缀自动 ...
- Codeforces 235C Cyclical Quest - 后缀自动机
Some days ago, WJMZBMR learned how to answer the query "how many times does a string x occur in ...
- HDU 6208 The Dominator of Strings 后缀自动机
The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- Codechef2015 May - Chef and Strings (后缀自动机)
用后缀自动机统计出出现1~n次的串的数量f[i] 对于ans[k]=sigma(f[i]*C(i,k)) i>=k ; mo=; ..maxn] of dword; nt:..maxn,'a'. ...
- CF 149E Martian Strings 后缀自动机
这里给出来一个后缀自动机的题解. 考虑对 $s$ 的正串和反串分别建后缀自动机. 对于正串的每个节点维护 $endpos$ 的最小值. 对于反串的每个节点维护 $endpos$ 的最大值. 这两个东西 ...
- Codeforces.700E.Cool Slogans(后缀自动机 线段树合并 DP)
题目链接 \(Description\) 给定一个字符串\(s[1]\).一个字符串序列\(s[\ ]\)满足\(s[i]\)至少在\(s[i-1]\)中出现过两次(\(i\geq 2\)).求最大的 ...
- 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings
E. Little Elephant and Strings time limit per test 3 seconds memory limit per test 256 megabytes inp ...
- codeforces 204E. Little Elephant and Strings(广义后缀自动机,Parent树)
传送门在这里. 大意: 给一堆字符串,询问每个字符串有多少子串在所有字符串中出现K次以上. 解题思路: 这种子串问题一定要见后缀自动机Parent树Dfs序统计出现次数都是套路了吧. 这道题统计子串个 ...
随机推荐
- 自签名证书说明——自签名证书的Issuer和Subject是一样的。不安全的原因是:没有得到专业SSL证书颁发的机构的技术支持?比如使用不安全的1024位非对称密钥对,有效期设置很长等
一般的数字证书产品的主题通常含有如下字段:公用名称 (Common Name) 简称:CN 字段,对于 SSL 证书,一般为网站域名:而对于代码签名证书则为申请单位名称:而对于客户端证书则为证书申请者 ...
- [转]java nio解决半包 粘包问题
java nio解决半包 粘包问题 NIO socket是非阻塞的通讯模式,与IO阻塞式的通讯不同点在于NIO的数据要通过channel放到一个缓存池ByteBuffer中,然后再从这个缓存池中读出数 ...
- python3- __slots__
Python允许在定义class的时候,定义一个特殊的__slots__变量,来限制该class实例能添加的属性: class Student(object): __slots__ = ('name' ...
- Java Date实现加一天,年月日类推往后+1,日期+1,月份+1,年份+1
System.out.println("String类型 "+endDate); //页面传递到后台的时间 为String类型 SimpleDateFormat sdf = new ...
- learning at command AT+CGSN
AT command AT+CGSN [Purpose] Learning how to get mobile module international Mobile Equipment ...
- [转载]Java创建WebService服务及客户端实现
Java创建WebService服务及客户端实现 Java创建WebService服务及客户端实现
- 在docker hub,用github的dockerfile自动生成docker镜像
简介: 我已经深深的爱上了docker技术. 在日常使用中,经常看到docker hub 中有很多autobuild的镜像.基本使用是在github中上传dockerfile,过一会儿,docker ...
- 学习net core的一些疑问?
所有的内容是否一定都要依赖注入? 获取配置文件的方式是否在类库是获取不到环境变量的? 老出现:InvalidOperationException: Unable to resolve service ...
- div 内 图片 垂直居中
vertical-align属性适用于 line-block: <div class="title"> <img src="img_p1_title.p ...
- centos tar 常用
tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用 ...