当前位置:网站首页>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;
}
边栏推荐
- 菜刀webshell特征分析
- leetcode / anagram in string - some permutation of s1 string is a substring of s2
- AI target segmentation capability for fast video cutout without green screen
- 国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
- Chopper webshell feature analysis
- 永磁同步电机36问(三)——SVPWM代码实现
- 项目场景 with ERRTYPE = cudaError CUDA failure 999 unknown error
- JVM调优实战
- Oracle数据类型介绍
- Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
猜你喜欢
feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
GTK RGB图像绘制
BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
局部敏感哈希:如何在常数时间内搜索Embedding最近邻
ros多客户端请求服务
永磁同步电机36问(三)——SVPWM代码实现
项目场景 with ERRTYPE = cudaError CUDA failure 999 unknown error
Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案
随机推荐
十字光标太小怎么调节、CAD梦想画图算量技巧
analog IC layout-Design for reliability
svm.SVC应用实践1--乳腺癌检测
EasyGBS平台播放视频时偶尔出现播放失败是什么原因?
Use DBeaver for mysql data backup and recovery
2022.8.1-----leetcode.1374
EFCore 反向工程
工程师如何对待开源
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
How engineers treat open source
MySQL - CRUD operations
2022-08-01 mysql/stoonedb slow SQL-Q18 analysis
架构:应用架构的演进以及微服务架构的落地实践
isa指针使用详情
OC中成员变量,实例变量和属性之间的区别和联系
架构:分布式任务调度系统(SIA-Task)简介
因为WiFi原因navicat 无法连接数据库Mysql
Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
考完PMP学什么?前方软考等着你~
What to study after the PMP exam?The soft exam ahead is waiting for you~