当前位置: 首页 > news >正文

题解:GDCPC 2022 C 魔法师

题意

给定 \(n\) 个大小写字母串 \(s_i\)。每次操作可以选择两个没有选过的串 \(s_i,s_j(i\neq j)\),产生 \(\min(|\operatorname{lcp}(s_i,s_j)|,|\operatorname{lcs}(s_i,s_j))|^2\) 的贡献。求能产生的最大贡献。\(1\leq n\leq 2\times 10^5\)\(1\leq \sum|s_i|\leq 3\times 10^5\)

题解

考虑如果产生的贡献为 \(|\operatorname{lcp}(s_i,s_j)|^2\) 怎么做。贪心,从大到小枚举 \(len\),那么对于所有 LCP 能取到 \(len\) 的字符串对,一定会在当前长度匹配掉。放到 Trie 上,插入一个串时,对于每个经过的节点 \(p\),令 \(cnt_p\gets cnt_p+1\)。按深度从大到小遍历每个点 \(p\),其对答案贡献为 \(\left\lfloor\dfrac{cnt_p}{2}\right\rfloor\times dep_p^2\),然后把 \(p\) 到根的链上的节点的 \(cnt\) 都减去 \(2\left\lfloor\dfrac{cnt_p}{2}\right\rfloor\)。跳过 \(cnt\leq 1\) 的点,暴力更新 \(cnt\) 复杂度就是总体 \(\mathcal{O}(\sum|s_i|)\) 的了。

回到原题,考虑能否重构字符串使得贡献变为 LCP。可以发现,只需要在奇数位置插入原串,偶数位置插入原串的反串,这样贡献就是 \(\left\lfloor\dfrac{|\operatorname{lcp}(s_i,s_j)|}{2}\right\rfloor^2\) 了,套用上述做法即可。时间复杂度为 \(\mathcal{O}(|\Sigma|\times \sum|s_i|)\)

代码
#include <bits/stdc++.h>using namespace std;#define lowbit(x) ((x) & -(x))
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
const int N = 2e5 + 6, L = 6e5 + 5, C = 53;template<typename T> inline void chk_min(T &x, T y) { x = min(x, y); }
template<typename T> inline void chk_max(T &x, T y) { x = max(x, y); }int n;
int f(char ch) { return ch >= 'a' && ch <= 'z' ? ch - 'a' + 1 : ch - 'A' + 27; } 
struct Trie {int tot, mxd, son[L][C], cnt[L], dep[L], fa[L];vector<int> vec[L];void ins(const string &s) {int p = 0;for (char ch : s) {ch = f(ch);if (!son[p][ch]) chk_max(mxd, dep[son[p][ch] = ++tot] = dep[p] + 1), fa[tot] = p;++cnt[p = son[p][ch]];}}ll solve() {ll res = 0;for (int i = 1; i <= tot; ++i) vec[dep[i]].push_back(i);for (int d = mxd; d; --d) for (int x : vec[d]) if (cnt[x] > 1) {res += (ll)(d >> 1) * (d >> 1) * (cnt[x] >> 1);int c = cnt[x] - (cnt[x] & 1);for (int p = x; p; p = fa[p]) cnt[p] -= c;}return res;}
} T;int main() {ios::sync_with_stdio(0), cin.tie(0);cin >> n;for (int i = 1; i <= n; ++i) {string s, t; cin >> s;for (int i = 0; i < s.size(); ++i) t += s[i], t += s[s.size() - 1 - i];T.ins(t);}cout << T.solve();return 0;
}
http://www.jsqmd.com/news/46395/

相关文章:

  • 2025 最新推荐窗帘十大品牌权威排行榜,定制 / 智能 / 遮光等全品类精选 国际协会测评认证优质品牌合集
  • 2025年11月岩板品牌选购指南:基于市场数据的品牌排名与性能对比
  • 2025 最新卫浴一线品牌推荐排行榜:权威揭晓领军品牌与新锐黑马,装修选购全攻略
  • 2025年11月岩板品牌推荐榜单与选择指南:五大品牌综合对比分析
  • cad图纸怎么转换成pdf?这4个工具亲测好用!
  • k8s 调试
  • 11.20 OP222操纵杆气缸报警
  • Linux部署Minio
  • 面向对象的设计第一阶段设计总结分析
  • C语言中的strcat的模拟实现
  • 2025年比较好的真石漆岗亭厂家推荐及选择参考
  • 《数字破局》第三章需求迷雾
  • 利用配置错误的postMessage()函数实现DOM型XSS攻击
  • 《数字破局》 第二章:规划与选人
  • 2025年北京除甲醛服务机构权威评测:氧道净醛水漆/甲醛净化/新房装修除甲醛服务机构解析
  • 2025年口碑好的矿用气动遥控平板车杭州别墅大宅装修
  • 2025 年试验箱生产厂家全景推荐!六大实力厂商覆盖全品类需求,品质与服务双保障
  • 2025年靠谱的纸箱珍珠棉用户好评厂家排行
  • 2025年质量好的矿用防爆柴油机搬运车行业内口碑厂家排行榜
  • if __name__ == __main__作用
  • 2025B2B外贸独立站优化服务商有哪些-外贸服务商测评推荐
  • 2025年质量好的自动伸缩门厂家推荐及选择参考
  • 全新AI增强Demo发布:DHTMLX Gantt与Diagram如何通过LLM更智能地构建项目与组织结构
  • DELL服务器设置来电自动启动
  • 电梯调度
  • 锚点定位
  • 2025医用隔离电源哪家好?深度测评
  • 2025年靠谱的飞手接单专业推荐榜单
  • 2025年口碑好的大连装修设计用户口碑最佳排行
  • 聚焦医疗基建:2025年中心供氧工程推荐深度解析