当前位置:网站首页>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;
}
边栏推荐
- Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products
- AI target segmentation capability for fast video cutout without green screen
- 使用docker安装mysql
- 树链剖分-
- Chapter 7 Noise analysis
- 【Unity入门计划】2D Game Kit:初步了解2D游戏组成
- [Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
- 2022年NPDP考完多久出成绩?怎么查询?
- OperatingSystemMXBean获取系统性能指标
- PAT甲级打卡-1001-1004
猜你喜欢

ReentrantLock工作原理

Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles

国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现

How engineers treat open source

Docker-compose安装mysql

svm.SVC应用实践1--乳腺癌检测

因为WiFi原因navicat 无法连接数据库Mysql

【每日一道LeetCode】——1. 两数之和

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

Electronic Manufacturing Warehouse Barcode Management System Solution
随机推荐
cadence landscape bindkey
KICAD 小封装拉线卡顿问题 解决方法
2022年NPDP考完多久出成绩?怎么查询?
29. 删除链表中重复的节点
列表常用方法
pyqt上手体验
永磁同步电机36问(二)——机械量与电物理量如何转化?
MySQL索引优化实战
线程的不同状态
KICAD 拉线宽度无法修改,解决方法
【web】理解 Cookie 和 Session 机制
FOFAHUB usage test
yaml
考完PMP学什么?前方软考等着你~
2022 NPDP take an examination of how the results?How to query?
因为WiFi原因navicat 无法连接数据库Mysql
mockjs生成假数据的基本使用
leetcode / anagram in string - some permutation of s1 string is a substring of s2
递归检查配置项是否更变并替换
第10章_索引优化与查询优化