当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域

接口测试神器Apifox究竟有多香?

Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?

最大层内元素和

GTK RGB图像绘制

罗德里格斯公式(Rodrigues‘ Rotation Formula)推导

第11章_数据库的设计规范

Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided

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

IMU预积分的简单理解
随机推荐
KICAD 拉线宽度无法修改,解决方法
BI - SQL 丨 WHILE
Ringtone 1161. Maximum In-Layer Elements and
1688以图搜货
永磁同步电机36问(二)——机械量与电物理量如何转化?
svm.SVC应用实践1--乳腺癌检测
analog IC layout-Parasitic effects
Flask之路由(app.route)详解
忽晴忽雨
BioVendor Human Club Cellular Protein (CC16) Elisa Kit Research Fields
2022牛客多校四_G M
TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
架构:分布式任务调度系统(SIA-Task)简介
NAS和私有云盘的区别?1篇文章说清楚
线程的不同状态
【Unity入门计划】2D Game Kit:初步了解2D游戏组成
Analysis of the status quo of digital transformation of manufacturing enterprises
详解最强分布式锁工具:Redisson
IMU预积分的简单理解
Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?