Educational Codeforces Round 5F. Expensive Strings
题意:给n个串ti,ps,i是s在ti中出现的次数,要求找到s,使得\(\sum_{i=1}^nc_i*p_{s,i}*|s|\)最大
题解:sam裸题,每次插入时相当于在fail链上到1的位置加ci,最后统一乘该节点状态的长度,我居然写了个lct维护!= =还wa了....后来发现打个标记topo一下即可
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000009
#define ld long double
//#define C 0.5772156649
//#define ls l,m,rt<<1
//#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
using namespace std;
const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=600000+10,maxn=1000000+10,inf=0x3f3f3f3f;
char s[N];
vector<char>v[100010];
struct SAM{
int last,cnt;
int ch[N<<1][26],fa[N<<1],l[N<<1];
int a[N<<1],c[N<<1];
ll sz[N<<1];
SAM(){cnt=1;}
void ins(int c,int x){
if(ch[last][c])
{
int p=last,q=ch[last][c];
if(l[q]==l[p]+1)last=q;
else
{
int nq=++cnt;l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof ch[q]);
fa[nq]=fa[q];fa[q]=last=nq;
for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq;
}
sz[last]+=x;
return ;
}
int p=last,np=++cnt;last=np;l[np]=l[p]+1;
for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np;
if(!p)fa[np]=1;
else
{
int q=ch[p][c];
if(l[p]+1==l[q])fa[np]=q;
else
{
int nq=++cnt;l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq;
}
}
sz[np]+=x;
}
void build(int id,int x)
{
last=1;
for(int i=0;i<v[id].size();i++)ins(v[id][i]-'a',x);
}
void topo()
{
for(int i=1;i<=cnt;i++)c[l[i]]++;
for(int i=1;i<=cnt;i++)c[i]+=c[i-1];
for(int i=1;i<=cnt;i++)a[c[l[i]]--]=i;
}
void cal()
{
topo();
for(int i=cnt;i;i--)sz[fa[a[i]]]+=sz[a[i]];
ll ans=0;
for(int i=2;i<=cnt;i++)ans=max(ans,sz[i]*l[i]);
printf("%lld\n",ans);
}
}sam;
int main()
{
int n;scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",s);
for(int j=0;s[j];j++)v[i].pb(s[j]);
}
for(int i=1;i<=n;i++)
{
int x;scanf("%d",&x);
sam.build(i,x);
}
sam.cal();
return 0;
}
/********************
********************/
Educational Codeforces Round 5F. Expensive Strings的更多相关文章
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Educational Codeforces Round 5
616A - Comparing Two Long Integers 20171121 直接暴力莽就好了...没什么好说的 #include<stdlib.h> #include&l ...
- Educational Codeforces Round 17
Educational Codeforces Round 17 A. k-th divisor 水题,把所有因子找出来排序然后找第\(k\)大 view code //#pragma GCC opti ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [Educational Codeforces Round 16]B. Optimal Point on a Line
[Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
随机推荐
- Ajax请求二进制流并在页面展示
后端代码: public void getIntegralQrcode(HttpServletResponse response, String token) throws BizException, ...
- Python记录wsgi
类实现wsgi app from wsgiref.util import setup_testing_defaults from wsgiref.simple_server import make_s ...
- 异步 async & await
1 什么是异步 异步的另外一种含义是计算机多线程的异步处理.与同步处理相对,异步处理不用阻塞当前线程来等待处理完成,而是允许后续操作,直至其它线程将处理完成,并回调通知此线程. 2 异步场景 l 不 ...
- 最短路径 SPFA P3371 【模板】单源最短路径(弱化版)
P3371 [模板]单源最短路径(弱化版) SPFA算法: SPFA 算法是 Bellman-Ford算法 的队列优化算法的别称,通常用于求含负权边的单源最短路径,以及判负权环.SPFA 最坏情况下复 ...
- CustomScrollView
body: CustomScrollView( slivers: [ SliverList( delegate: SliverChildBuilderDelegate( (context, int i ...
- C++ STL标准容器插入删除算法的复杂度
1 vector内部实现: 数组 // 就是没有固定大小的数组,vector直接翻译是向量的意思支持操作:begin(), //取首个元素,返回一个iteratorend(), //取末尾(最后一个元 ...
- 前向传播算法(Forward propagation)与反向传播算法(Back propagation)
虽然学深度学习有一段时间了,但是对于一些算法的具体实现还是模糊不清,用了很久也不是很了解.因此特意先对深度学习中的相关基础概念做一下总结.先看看前向传播算法(Forward propagation)与 ...
- 手机app抓包
简介 爬虫是cs架构中的c端 原理是模拟浏览器向服务器发送请求 如果要爬取手机APP的数据,APP也是服务端与浏览器性质相同 我们只要获取到手机APP给服务器发送数据 并加以分析就能模拟它的请求 从而 ...
- qt部件的可视性
- elasticsearch-mapping字段重要属性
https://blog.csdn.net/gongpulin/article/details/78705205