CF1902E. Collapsing Strings-LCP、字典树
link:https://codeforces.com/contest/1902/problem/E
题意:You are given
- if
is empty, ; - if is empty, ; - if the last letter of
is equal to the first letter of , then , where is the substring of from the -th letter to the -th one; - otherwise,
, i. e. the concatenation of two strings.
Calculate .
因此 $$ans=\sum_{i=1}n\sum_{j=1}n |s_i|+|s_j|-2 LCP(s_j,s_i')=2n\cdot\sum_{i=1}n|s_i|-2\sum_{i=1}n\sum_{j=1}^n LCP(s_i',s_j)$$
对于后者,枚举每个
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) using namespace std; typedef long long ll; const int N=1e6+5; const int C=26; int n,tr[N][C+1],tag[N],cnt; string s[N]; void insert(const string &s){ int len=s.length(),k=0; for(int i=0;i<len;i++){ int ch=s[i]-'a'; if(!tr[k][ch])tr[k][ch]=++cnt; k=tr[k][ch]; tag[k]++; } } ll work(const string &s){ int len=s.length(),k=0; ll ret=0; for(int i=len-1;i>=0;i--){ int ch=s[i]-'a'; k=tr[k][ch]; if(!k)break; ret+=tag[k]; } return ret; } int main(){ fastio; cin>>n; ll ans=0; rep(i,1,n){ cin>>s[i]; insert(s[i]); ans+=(ll)2*n*s[i].length(); } rep(i,1,n)ans-=2*work(s[i]); cout<<ans; return 0; }
分类:
CodeForces
, 字典树
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具