当前位置:网站首页>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;
}
边栏推荐
- BI-SQL丨WHILE
- 四元数、罗德里格斯公式、欧拉角、旋转矩阵推导和资料
- svm.SVC应用实践1--乳腺癌检测
- esp32经典蓝牙和单片机连接,,,手机蓝牙作为主机
- Can Youxuan database import wrongly be restored?
- The state status is displayed incorrectly after the openGauss switch
- analog IC layout-Design for reliability
- 架构:应用架构的演进以及微服务架构的落地实践
- 790. 数的三次方根
- TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
猜你喜欢
随机推荐
Oracle数据类型介绍
Nacos源码分析专题(一)-环境准备
微服务:微智能在软件系统的简述
周鸿祎称微软抄袭,窃取360安全模式
使用DBeaver进行mysql数据备份与恢复
Can Youxuan database import wrongly be restored?
yaml
Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案
字符串常用方法
Chopper webshell feature analysis
Electronic Manufacturing Warehouse Barcode Management System Solution
Outsourcing worked for three years, it was abolished...
Safety (2)
Use DBeaver for mysql data backup and recovery
Power button 1374. Generate each character string is an odd number
2022 Henan Youth Training League Game (3)
接口测试神器Apifox究竟有多香?
灰度传感器、、、diy原理。。图
IMU预积分的简单理解
第11章_数据库的设计规范