当前位置:网站首页>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;
}
边栏推荐
- Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
- NAS和私有云盘的区别?1篇文章说清楚
- 永磁同步电机36问(三)——SVPWM代码实现
- 罗德里格斯公式(Rodrigues‘ Rotation Formula)推导
- GTK RGB图像绘制
- Qt自定义控件和模板分享
- Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
- 1688API
- analog IC layout-Design for reliability
- 1688以图搜货
猜你喜欢

Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol

工程师如何对待开源

启发式合并、DSU on Tree

很有意思的经历,很有意思的项目--文件夹对比工具

字符串常用方法

Unable to log in to the Westward Journey

Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒

Flask之路由(app.route)详解

Outsourcing worked for three years, it was abolished...

国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
随机推荐
JVM调优实战
pyqt上手体验
Electronic Manufacturing Warehouse Barcode Management System Solution
2022 Henan Youth Training League Game (3)
Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
Safety (2)
What to study after the PMP exam?The soft exam ahead is waiting for you~
1688以图搜货
Lombok
2022牛客多校三_F G
aws s3上传文件
AI target segmentation capability for fast video cutout without green screen
FOFAHUB usage test
通用客户端架构
架构:微服务网关(SIA-Gateway)简介
789. 数的范围
Chapter 7 Noise analysis
2022 NPDP take an examination of how the results?How to query?
Chopper webshell feature analysis
微信小程序异步回调函数恶梦和解决办法