loading

CF616F Expensive Strings

隔壁 CF316G3 远比这题难吧,为什么那题 2400 这题 2700?

题目描述

\(n\) 个字符串 \(t_i\),第 \(i\) 个字符串的价值为 \(c_i\)。现在想求一个字符串函数 \(f(S)=\sum_{i=1}^n |S|\cdot c_i\cdot p_{S,t_i}\),其中 \(p_{S,T}\) 表示字符串 \(S\)\(T\) 中的出现次数。求 \(f(S)\) 的最大值,\(S\) 可以为空。

\(n\le 10^5,\sum |t_i|\le 5\times10^5,|c_i|\le 10^7\)

分析

\(n\) 个串用奇怪字符拼起来做后缀数组。那么一个子串在某个字符串中出现就相当于它是某个后缀的前缀。由于奇怪字符的存在,可以保证一个子串不会从一个字符串匹配到另一个字符串上。

对于每个属于字符串 \(t_i\) 的后缀的开头,赋上权值 \(c_i\),表示若该后缀和某子串匹配会对该子串有 \(c_i\) 的权值。那么 \(f(S)\) 就相当于 \(S\) 能匹配上的后缀的权值和乘上 \(|S|\)

由于 \(height\) 数组的性质,一个子串只会在 \(sa\) 数组上某个连续的区间出现,该串长度即为区间 \(height\) 最小值,权值就相当于区间和,做一遍前缀和即可。此时即便该子串出现区间的权值和是负数(子串长度永远非负),我们也可以通过取空串使得 \(ans=0\),因此可以保证子串取可能最大值一定不劣。

那么做法就显然了,考虑每个 \(height_i\) 的贡献区间的左右端点是左右第一个 \(height_j<height_i\)\(j\) 的前/后一位,单调栈做即可。

特判取这 \(n\) 个整串的情况,其中特判某个整串是另一个整串的子串的情况。我们只需要取出以该整串为开头的后缀(设该位置为 \(k\)),看看 \(height_k,height_{k+1}\) 的值是否都小于 \(|t_i|\) 即可。

点击查看代码
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define FlushIn fread(Fread::ibuf,1,1<<21,stdin)
#define FlushOut fwrite(Fwrite::obuf,1,Fwrite::S-Fwrite::obuf,stdout)
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P__ puts("")
#define PU puts("--------------------")
#define popc __builtin_popcount
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define lowb lower_bound
#define uppb upper_bound
#define rep(a,b,c) for(int a=(b);a<=(c);a++)
#define per(a,b,c) for(int a=(b);a>=(c);a--)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=d)
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=d)
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x)
//#define double long double
#define int long long
//#define int __int128
using namespace std;
typedef long long i64;
bool greating(int x,int y){return x>y;}
bool greatingll(long long x,long long y){return x>y;}
bool smallingll(long long x,long long y){return x<y;}
namespace Fread {
	const int SIZE=1<<21;
	char ibuf[SIZE],*S,*T;
	inline char getc(){if(S==T){T=(S=ibuf)+fread(ibuf,1,SIZE,stdin);if(S==T)return '\n';}return *S++;}
}
namespace Fwrite{
	const int SIZE=1<<21;
	char obuf[SIZE],*S=obuf,*T=obuf+SIZE;
	inline void flush(){fwrite(obuf,1,S-obuf,stdout);S=obuf;}
	inline void putc(char c){*S++=c;if(S==T)flush();}
	struct NTR{~NTR(){flush();}}ztr;
}
/*#ifdef ONLINE_JUDGE
#define getchar Fread::getc
#define putchar Fwrite::putc
#endif*/
inline int rd(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}return x*f;
}
inline void write(int x,char ch='\0'){
	if(x<0){x=-x;putchar('-');}
	int y=0;char z[40];
	while(x||!y){z[y++]=x%10+48;x/=10;}
	while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
bool Mbg;
const int maxn=6e5+5,maxm=1e5+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int k,a[maxn],c[maxm],s[maxn];
string t[maxm];
int n,str[maxn];
int sa[maxn],rk[maxn],hi[maxn];
int id[maxn],nrk[maxn],prk[maxn<<1],cnt[maxn];
bool qwq(int x,int y,int z){
	return prk[x]==prk[y]&&prk[x+z]==prk[y+z];
}
void SA(){
	int m=k+127;
	rep(i,1,n)cnt[rk[i]=str[i]]++;
	rep(i,2,m)cnt[i]+=cnt[i-1];
	per(i,n,1)sa[cnt[rk[i]]--]=i;
	for(int j=1,p;;j<<=1,m=p){
		p=0;rep(i,n-j+1,n)id[++p]=i;
		rep(i,1,n)if(sa[i]>j)id[++p]=sa[i]-j;
		rep(i,1,m)cnt[i]=0;
		rep(i,1,n)cnt[nrk[i]=rk[id[i]]]++;
		rep(i,2,m)cnt[i]+=cnt[i-1];
		per(i,n,1)sa[cnt[nrk[i]]--]=id[i];
		rep(i,1,n)prk[i]=rk[i];
		p=0;rep(i,1,n)rk[sa[i]]=qwq(sa[i],sa[i-1],j)?p:++p;
		if(p==n)break;
	}
	int h=0;
	rep(i,1,n){
		if(h)--h;
		while(str[i+h]==str[sa[rk[i]-1]+h])++h;
		hi[rk[i]]=h;
	}
}
int sta[maxn],tp,lft[maxn],rht[maxn];
void Init(){
	rep(i,1,n)lft[i]=0,rht[i]=n+1;
	tp=0;rep(i,1,n){
		while(tp&&hi[i]<hi[sta[tp]])rht[sta[tp]]=i,--tp;
		sta[++tp]=i;
	}
	tp=0;per(i,n,1){
		while(tp&&hi[i]<hi[sta[tp]])lft[sta[tp]]=i,--tp;
		sta[++tp]=i;
	}
}
int st[maxn];
void solve_the_problem(){
	cin>>k;
	rep(i,1,k)cin>>t[i];
	rep(i,1,k)cin>>c[i];
	rep(i,1,k){
		int lsbl=t[i].size();st[i]=n+1;
		rep(j,0,lsbl-1)str[++n]=t[i][j],a[n]=c[i];
		str[++n]='z'+i,a[n]=-inf;
	}
	SA();
	rep(i,1,n)s[i]=s[i-1]+a[sa[i]];
	Init();
	int ans=0;
	rep(i,1,k)if(hi[rk[st[i]]]<(int)t[i].size()&&hi[rk[st[i]]+1]<(int)t[i].size()){
		ans=max(ans,c[i]*(int)t[i].size());
	}
	rep(i,2,n)if(hi[i]){
		int l=lft[i]+1,r=rht[i]-1;
		ans=max(ans,(s[r]-s[l-2])*hi[i]);
	}
	cout<<ans;
}
bool Med;
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
//	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	IOS;int _=1;while(_--)solve_the_problem();
}
/*
2
aa
aaa
2 -3
output:0
*/

posted @ 2024-02-06 16:54  dcytrl  阅读(22)  评论(0)    收藏  举报