2018 Arab Collegiate Programming Contest (ACPC 2018) E - Exciting Menus AC自动机
建个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 long using namespace std; const int N = 1e5 + ;
const int M = 1e6 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, a[N];
string s[N]; struct Ac {
int ch[N][], val[N], f[N], mx[N], depth[N], deg[N], tot, sz;
inline int newNode() {
tot++; f[tot] = ; val[tot] = ; mx[tot] = , deg[tot] = ;
memset(ch[tot], , sizeof(ch[tot]));
return tot;
}
void init(int _sz) {sz = _sz; tot = -; newNode();}
inline int idx(int c) {return c - 'a';}
void addStr(string& s, int *a) {
int u = ;
for(int i = ; 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 + ;
mx[u] = max(mx[u], a[i]);
}
}
void build() {
queue<int> que;
for(int c = ; c < sz; c++) {
int v = ch[][c];
if(!v) ch[][c] = ;
else f[v] = , que.push(v);
}
while(!que.empty()) {
int u = que.front(); que.pop();
for(int c = ; 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();
cin >> n;
for(int i = ; i <= n; i++) cin >> s[i];
for(int i = ; i <= n; i++) {
for(int j = ; j < s[i].size(); j++) cin >> a[j];
addStr(s[i], a);
}
build();
queue<int> que;
for(int i = ; 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 = ;
for(int i = ; 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 ;
} /*
*/
2018 Arab Collegiate Programming Contest (ACPC 2018) E - Exciting Menus AC自动机的更多相关文章
- 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 使得 ...
- 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 的连续子区间. ...
- 2018 German Collegiate Programming Contest (GCPC 18)
2018 German Collegiate Programming Contest (GCPC 18) Attack on Alpha-Zet 建树,求lca 代码: #include <al ...
- (寒假GYM开黑)2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)
layout: post title: 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018) author: &qu ...
- (寒假GYM开黑)2018 German Collegiate Programming Contest (GCPC 18)
layout: post title: 2018 German Collegiate Programming Contest (GCPC 18) author: "luowentaoaa&q ...
- 2018 China Collegiate Programming Contest Final (CCPC-Final 2018)-K - Mr. Panda and Kakin-中国剩余定理+同余定理
2018 China Collegiate Programming Contest Final (CCPC-Final 2018)-K - Mr. Panda and Kakin-中国剩余定理+同余定 ...
- 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)- D. Delivery Delays -二分+最短路+枚举
2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)- D. Delivery Delays -二分+最短路+枚举 ...
- 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)-E. Explosion Exploit-概率+状压dp
2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)-E. Explosion Exploit-概率+状压dp [P ...
- 2019年湖南多校第一场||2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)
第一场多校就打的这么惨,只能说自己太菜了,还需继续努力啊- 题目链接: GYM链接:https://codeforces.com/gym/101933 CSU链接:http://acm.csu.edu ...
随机推荐
- bzoj1038
这是一道非常有意思的题目 Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一 ...
- HGOI20180813 (NOIP2018 提高组 Day2 模拟试题)
省常中省选提高Day2 继续 第一题就考了贪心,正解95pts的贪心策略第一印象是想到的,但是被自己否定掉了qwq,然后打了 不是正解的贪心,样例5没过(可怜)思路如下:先找出每个门对应可以通过的人数 ...
- Appium+python自动化获取toast消息(windows版)的方法
原来用的Appium1.5.3GUI版本,那为什么升级呢? 为了兼容最新版本的iOS10和Android7 Xcode8升级后,将不支持使用UIAutomation,而是改为使用XCUITest了,并 ...
- 解析word公式的解决方案(office插入和wps插入不同的解决方案)
这几天在公司的项目有个需求就是数学公式的导入,而对于word来说,插入的公式xml格式,需要转换为mathML,借用插件MathJax来进行展示,而对于wps插入的公式来说,获取到的是一个wmf图片, ...
- 前端学习 -- Css -- 定义列表
定义列表用来对一些词汇或内容进行定义 使用dl来创建一个定义列表 dl中有两个子标签 dt : 被定义的内容 dd : 对定义内容的描述 同样dl和ul和ol之间都可以互相嵌套 <!DOCTYP ...
- Redis与memecache的区别
转载连接: https://www.biaodianfu.com/redis-vs-memcached.html Redis的作者Salvatore Sanfilippo曾经对这两种基于内存的数据存储 ...
- vue element-ui 实现点击查看审核记录
<el-dialog title="审核信息" :visible.sync="seeVisible" width="30%" :bef ...
- bzoj千题计划216:bzoj1499: [NOI2005]瑰丽华尔兹
http://www.lydsy.com/JudgeOnline/problem.php?id=1499 预处理从每个位置向每个方向最多能走几步 dp[k][i][j] 第k个时间段后,钢琴到位置(i ...
- Webx示例-PetStore分析1
1. 下载源码 2. 启动容器,加载组件--WebxContextLoaderListener WebxContextLoaderListener继承自org.springframework.web. ...
- unp学习笔记——Chapter1
1.发现网络拓扑的几个重要的命令 (1).netstat -i 提供网络接口的信息.我们还指定-n 标志以输出数值地址,而不是试图把它们反向解析成名字.netstat -r 展示路由表. dzhwen ...