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序统计出现次数都是套路了吧. 这道题统计子串个 ...
随机推荐
- EventBus简单封装
前言 以前每个页面与每个页面业务逻辑传递让你不知所措,一个又一个接口回调,让你晕头转向,一个又一个参数让你混乱不堪.EventBus一个耦合度低的让你害怕的框架. 什么是EventBus EventB ...
- python(4)之字典
字典的操作方式如下: info={ 'stu1101':"xiaohai", 'stu1102':'liming', 'stu1103':"heima", } ...
- input text 在苹果微信浏览器中有上上阴影
box-shadow:0px 0px 0px rgba(0,0,0,0); -webkit-appearance:none;
- ubuntu下的网速限制软件wondershaper (2011-09-18 00:00:00)转载▼
上网或下载的时候我们常常希望网速快一点,但有时我们也需要限制网速,在ubuntu系统下,可以使用wondershaper,不仅可以限制下载速度还可以限制上传速度. 安装好之后,需要使用终端取得管理员权 ...
- PHP7 ci框架session存文件,登录的时候session不能读取
config.php配置 $config['sess_driver'] = 'files';//以文件存储session $config['sess_cookie_name'] = 'ci_sessi ...
- Java IO流01-总叙
Java IO包体系结构图: 1.流式部分――IO的主体部分: 2.非流式部分——主要包含一些辅助流式部分的类,如:File类.RandomAccessFile类和FileDescriptor等类: ...
- ES6中的Promise.resolve()的作用
var foo = { then: (resolve, reject) => resolve('foo') }; var resolved = Promise.resolve(foo); 相当于 ...
- Android Studio2.1版本后使用虚拟机碰见的问题总结以及其他问题
一.androidstudio的sdk配置问题 如果点击Start a new Android Studio project是没有反应的,并且在Configure下面的SDK Manager是灰色的, ...
- js中有哪几种函数?
匿名函数,回调函数,递归函数,构造函数
- Python之路-python基础一
本章内容: 一.编程语言分类 二.python之变量介绍 三.python交互命令(input,print,getpass) 四.Python之循环控制 ...