当前位置:网站首页>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;
}
边栏推荐
- 项目场景 with ERRTYPE = cudaError CUDA failure 999 unknown error
- 很有意思的经历,很有意思的项目--文件夹对比工具
- NAS和私有云盘的区别?1篇文章说清楚
- AWR analysis report questions for help: How can SQL be optimized from what aspects?
- 2022牛客多校四_G M
- Unable to log in to the Westward Journey
- Nanoprobes纳米探针丨Nanogold偶联物的特点和应用
- KICAD 小封装拉线卡顿问题 解决方法
- 最大层内元素和
- esp32经典蓝牙和单片机连接,,,手机蓝牙作为主机
猜你喜欢
国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
Ringtone 1161. Maximum In-Layer Elements and
ApiFox 基本使用教程(浅尝辄止,非广)
Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案
Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
Nanoprobes纳米探针丨Nanogold偶联物的特点和应用
详解最强分布式锁工具:Redisson
FOFAHUB usage test
CASE2023
随机推荐
How engineers treat open source
IMU预积分的简单理解
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
机器人领域期刊会议汇总
FOFAHUB usage test
【web】理解 Cookie 和 Session 机制
Safety (1)
【每日一道LeetCode】——1. 两数之和
AcWing 1053. 修复DNA 题解(状态机DP、AC自动机)
永磁同步电机36问(二)——机械量与电物理量如何转化?
Nanoprobes纳米探针丨Nanogold偶联物的特点和应用
CASE2023
国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
The state status is displayed incorrectly after the openGauss switch
Analysis of the status quo of digital transformation of manufacturing enterprises
leetcode / anagram in string - some permutation of s1 string is a substring of s2
2022.8.1-----leetcode.1374
面对职场“毕业”,PM&PMO应该如何从容的应对?如何跳槽能够大幅度升职加薪?
字典常用方法
数仓:数仓从ETL到ELT架构的转化以及俩者的区别