E - Exciting Menus

建个AC自动机求个fail指针就好啦。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long longusing namespace std;const int N = 1e5 + 7;
const int M = 1e6 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);int n, a[N];
string s[N];struct Ac {int ch[N][26], val[N], f[N], mx[N], depth[N], deg[N], tot, sz;inline int newNode() {tot++; f[tot] = 0; val[tot] = 0; mx[tot] = 0, deg[tot] = 0;memset(ch[tot], 0, sizeof(ch[tot]));return tot;}void init(int _sz) {sz = _sz; tot = -1; newNode();}inline int idx(int c) {return c - 'a';}void addStr(string& s, int *a) {int u = 0;for(int i = 0; i < s.size(); i++) {int c = idx(s[i]);if(!ch[u][c]) ch[u][c] = newNode();u = ch[u][c];val[u]++;depth[u] = i + 1;mx[u] = max(mx[u], a[i]);}}void build() {queue<int> que;for(int c = 0; c < sz; c++) {int v = ch[0][c];if(!v) ch[0][c] = 0;else f[v] = 0, que.push(v);}while(!que.empty()) {int u = que.front(); que.pop();for(int c = 0; c < sz; c++) {int v = ch[u][c];if(!v) ch[u][c] = ch[f[u]][c];else f[v] = ch[f[u]][c], deg[f[u]]++, que.push(v);}}}void solve() {init(26);cin >> n;for(int i = 1; i <= n; i++) cin >> s[i];for(int i = 1; i <= n; i++) {for(int j = 0; j < s[i].size(); j++) cin >> a[j];addStr(s[i], a);}build();queue<int> que;for(int i = 1; i <= tot; i++) if(!deg[i]) que.push(i);while(!que.empty()) {int u = que.front(); que.pop();mx[f[u]] = max(mx[f[u]], mx[u]);deg[f[u]]--;if(!deg[f[u]]) que.push(f[u]);}LL ans = 0;for(int i = 1; i <= tot; i++)ans = max(ans, 1ll*val[i]*mx[i]*depth[i]);cout << ans << "\n";}
} ac;int main() {freopen("exciting.in", "r", stdin);int T; scanf("%d", &T);while(T--) {ac.solve();}return 0;
}/*
*/

转载于:https://www.cnblogs.com/CJLHY/p/10271522.html

2018 Arab Collegiate Programming Contest (ACPC 2018) E - Exciting Menus AC自动机相关推荐

  1. 2018 Arab Collegiate Programming Contest (ACPC 2018) L.Looking for Taste(按位或)

    题目 n个数选k个,使它们的或最大 n<=1e5 k>=20 ai<=1e6 题解 从高向低位或, 每个数至少产生一位的贡献,所以最多20个数 贪心地使或的数最大即可 由于或没有副作 ...

  2. 2018 Arab Collegiate Programming Contest (ACPC 2018) G. Greatest Chicken Dish (线段树+GCD)

    题目链接:https://codeforces.com/gym/101991/problem/G 题意:给出 n 个数,q 次询问区间[ li,ri ]之间有多少个 GCD = di 的连续子区间. ...

  3. 2018 Arab Collegiate Programming Contest (ACPC 2018) H - Hawawshi Decryption 数学 + BSGS

    H - Hawawshi Decryption 对于一个给定的生成数列 R[ 0 ] 已知, (R[ i - 1 ] * a + b) % p = R[ i ] (p 是 质数), 求最小的 x 使得 ...

  4. 2018 German Collegiate Programming Contest (GCPC 18)

    2018 German Collegiate Programming Contest (GCPC 18) Attack on Alpha-Zet 建树,求lca 代码: #include <al ...

  5. 2021 HZNU Winter Training Day 17 (2018 German Collegiate Programming Contest (GCPC 18))

    2021 HZNU Winter Training Day 17 (2018 German Collegiate Programming Contest (GCPC 18)) 题目 A B C D E ...

  6. 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018) - 4.28

    赛后补了几道 赛中我就写了两个... A - Altruistic AmphibiansGym - 101933A 看了眼榜没几个人做.就没看. 最后发现就是一个DP(但是我觉得复杂度有点迷) 题意: ...

  7. The 2018 JUST Collegiate Programming Contest H题 Cube

    小学数学题 #include<cstdio> #include<cmath> using namespace std; unsigned long long area, ans ...

  8. 2018 China Collegiate Programming Contest - Jilin Site F - The Hermit HDU - 6560 思维

    链接Problem - 6560 题意 有n个站点每个站点可以发送完美信号 关于完美信号的定义 有i j k三个站点 分别保证 i<j<k dis(i,j)> dis(j,k) 并且 ...

  9. 2018 German Collegiate Programming Contest (GCPC 18) M - Mountaineers(启发式合并)

    题目链接:https://codeforces.com/gym/102021/attachments 题意:现在有个地图由n∗mn*mn∗m个方格组成,每个方格上有个数字,代表在这个点的海拔高度,现在 ...

最新文章

  1. 所有字符不含换行 正则表达式_网络爬虫 | 正则表达式
  2. sql Escape用法
  3. C++友元函数和友元类(一)
  4. 并查集 路径压缩(具体解释)
  5. “社区之星”(社区核心贡献者)成长故事征集
  6. JS --- this(4)
  7. 计算机毕业设计中基于python的快递查询系统
  8. NYOJ259 - 茵茵的第一课
  9. 关于DBC文件的创建(DBC文件系列其一)
  10. 中国大陆IP过滤器-Java实现
  11. Minidump方式保留程序崩溃现场
  12. 使用Visual studio查看exe或DLL文件的依赖项
  13. 运行VS2008提示找不到一个或多个组件,请重新安装该应用程序错误的解决方法V
  14. opencv之伪彩色处理
  15. Android Activity生命周期以及LoCat的使用
  16. win7系统一键还原教程
  17. 四、模拟英语四六级答题卡识别阅卷评分
  18. 中年男人失业,滴滴,外卖,保安三选一,怎么选?
  19. Spring 概述及优点
  20. QQ邮箱里面添加学校的edu邮箱

热门文章

  1. 混凝土试块送检要求与时间
  2. 淘宝网nbsp;E客服帐号nbsp;登录阿里旺旺时nbsp;…
  3. 要闻 | 人大金仓重磅亮相2022南京软博会
  4. Android Library的创建及使用详解
  5. whistle-安卓手机配置代理
  6. 淘宝链接转换成淘宝客链接--PHP
  7. 刷手机流量,反正浪费就完事了
  8. 各地政府开放平台_8个视频让您对开放政府感到兴奋
  9. select()函数的作用
  10. 粒子群算法改进——压缩因子法