当前位置:网站首页>AcWing 1285. Word Problem Solving (AC Automata)
AcWing 1285. Word Problem Solving (AC Automata)
2022-08-02 02:46:00 【QingQingDE23】
AcWing 1285. 单词
用fRecord a prefix and the suffix of the same word the number of occurrences of(Not only is the number of occurrences of words)
#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] ++ ; //The number of records precursors appear
}
id[x] = p; //Each word is assigned a number
}
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]]; //Along the topological sequence upward in reverse chronological order,All progressive values to subsequent precursor node
}
for(int i = 0; i < n; i ++ ){
cout<<f[id[i]]<<endl;
}
return 0;
}
边栏推荐
- 架构:分布式任务调度系统(SIA-Task)简介
- 欧拉公式的证明
- (一)Redis: 基于 Key-Value 的存储系统
- 20. 用两个栈实现队列
- 指针数组和数组指针
- The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
- 面对职场“毕业”,PM&PMO应该如何从容的应对?如何跳槽能够大幅度升职加薪?
- AcWing 1053. 修复DNA 题解(状态机DP、AC自动机)
- 60 Feature Engineering Operations: Using Custom Aggregate Functions【Favorites】
- 53. 最小的k个数
猜你喜欢
随机推荐
1688API
递归检查配置项是否更变并替换
工程师如何对待开源
灰度传感器、、、diy原理。。图
2022-08-01 Install mysql monitoring tool phhMyAdmin
2022牛客多校三_F G
简单的页面跳转活动
KICAD 小封装拉线卡顿问题 解决方法
机器人领域期刊会议汇总
nacos startup error, the database has been configured, stand-alone startup
接口测试神器Apifox究竟有多香?
20. 用两个栈实现队列
Swift运行时(派发机制)
EFCore 反向工程
第11章_数据库的设计规范
EasyGBS平台播放视频时偶尔出现播放失败是什么原因?
Ringtone 1161. Maximum In-Layer Elements and
Can Youxuan database import wrongly be restored?
ros多客户端请求服务
永磁同步电机36问(三)——SVPWM代码实现









