当前位置:网站首页>AcWing 1285. 单词 题解(AC自动机)
AcWing 1285. 单词 题解(AC自动机)
2022-08-02 02:37:00 【QingQingDE23】
AcWing 1285. 单词
用f记录一个与这个后缀相同的单词前缀出现的次数(不仅仅是单词出现的次数)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int tr[N][26], f[N], q[N];
int n;
string str;
int ids;
int id[N];
int ne[N];
void insert(int x){
int p = 0;
for(int i = 0; str[i]; i ++ ){
int t = str[i] - 'a';
if(!tr[p][t]) tr[p][t] = ++ ids;
p = tr[p][t];
f[p] ++ ; //记录前驱出现的次数
}
id[x] = p; //每个单词被赋予一个编号
}
void build(){
int hh = 0, tt = -1;
for(int i = 0; i < 26; i ++ ){
if(tr[0][i]) q[ ++ tt] = tr[0][i];
}
while(hh <= tt){
int t = q[hh ++ ];
for(int i = 0; i < 26; i ++ ){
int &p = tr[t][i];
if(!p) p = tr[ne[t]][i];
else{
ne[p] = tr[ne[t]][i];
q[ ++ tt] = p;
}
}
}
}
int main()
{
cin>>n;
for(int i = 0; i < n; i ++ ){
cin>>str;
insert(i);
}
build();
for(int i = ids - 1; i >= 0; i -- ){
f[ne[q[i]]] += f[q[i]]; //沿拓扑序倒序向上,将所有前驱节点的值累加到后继节点上
}
for(int i = 0; i < n; i ++ ){
cout<<f[id[i]]<<endl;
}
return 0;
}
边栏推荐
猜你喜欢

Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案

feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r

BioVendor Human Club Cellular Protein (CC16) Elisa Kit Research Fields

Ringtone 1161. Maximum In-Layer Elements and

永磁同步电机36问(二)——机械量与电物理量如何转化?

数值积分方法:欧拉积分、中点积分和龙格-库塔法积分

openGauss切换后state状态显示不对

指针数组和数组指针

因为WiFi原因navicat 无法连接数据库Mysql

忽晴忽雨
随机推荐
很有意思的经历,很有意思的项目--文件夹对比工具
面对职场“毕业”,PM&PMO应该如何从容的应对?如何跳槽能够大幅度升职加薪?
51. 数字排列
Oracle数据类型介绍
The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string
数仓:数仓从ETL到ELT架构的转化以及俩者的区别
AWR分析报告问题求助:SQL如何可以从哪几个方面优化?
2022-08-01 Install mysql monitoring tool phhMyAdmin
AI target segmentation capability for fast video cutout without green screen
项目场景 with ERRTYPE = cudaError CUDA failure 999 unknown error
IMU预积分的简单理解
工程师如何对待开源
CASE2023
KICAD 拉线宽度无法修改,解决方法
四元数、罗德里格斯公式、欧拉角、旋转矩阵推导和资料
2022.8.1-----leetcode.1374
OperatingSystemMXBean获取系统性能指标
优炫数据库导库导错了能恢复吗?
Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
2022 NPDP take an examination of how the results?How to query?