hsy单词
题意:略
在ac自动机上,一个节点出现的次数等于能通过fail到它的节点的次数之和。而叶节点就等于它被爬过的次数。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#define mkp make_pair
using namespace std;
const double EPS=1e-;
typedef long long lon;
const lon SZ=,SSZ=,APB=,one=,INF=0x7FFFFFFF,mod=;
int n,nex[SZ][APB],cnt,num[SZ];
int fail[SZ],match[SZ],in[SZ],ans[SZ];
char ch[SZ]; void build(int x)
{
int cur=;
for(int i=;ch[i];++i)
{
int c=ch[i]-'a';
if(!nex[cur][c])nex[cur][c]=++cnt;
cur=nex[cur][c];
++num[cur];
//cout<<num[cur]<<endl;
}
match[x]=cur;
} void build_fail()
{
queue<int> q;
q.push();
for(;q.size();)
{
int fr=q.front();
q.pop();
for(int i=;i<APB;++i)
{
if(nex[fr][i])
{
int u=nex[fr][i];
if(fr==)
{
fail[u]=;
}
else
{
int v=fail[fr];
for(;!nex[v][i]&&v;v=fail[v]);
if(nex[v][i])fail[u]=nex[v][i];
else fail[u]=;
}
q.push(u);
}
}
}
} void topo()
{
for(int i=;i<=cnt;++i)
{
++in[fail[i]];
}
stack<int> stk;
for(int i=;i<=cnt;++i)
{
if(!in[i])
{
stk.push(i);
//cout<<"i: "<<i<<endl;
}
ans[i]=num[i];
}
for(;stk.size();)
{
int top=stk.top();
stk.pop();
--in[fail[top]],ans[fail[top]]+=ans[top];
if(!in[fail[top]])
{
stk.push(fail[top]);
}
}
} void init()
{
cin>>n;
for(int i=;i<=n;++i)
{
cin>>ch+;
build(i);
}
build_fail();
topo();
for(int i=;i<=n;++i)
{
cout<<ans[match[i]]<<endl;
}
} void work()
{ } int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
int casenum;
//cin>>casenum;
//cout<<casenum<<endl;
//for(int time=1;time<=casenum;++time)
//for(int time=1;cin>>n>>qnum,n;++time)
{
//cout<<"Case "<<time<<": ";
init();
work();
}
return ;
}
hsy单词的更多相关文章
- java统计字符串单词的个数
在一些项目中可能需要对一段字符串中的单词进行统计,我在这里写了一个简单的demo,有需要的同学可以拿去看一下. 本人没怎么写个播客,如果有啥说的不对的地方,你来打我啊 不说废话了直接贴代码: 实现代码 ...
- JavaScript将字符串中的每一个单词的第一个字母变为大写其余均为小写
要求: 确保字符串的每个单词首字母都大写,其余部分小写. 这里我自己写了两种方法,或者说是一种方法,另一个是该方法的变种. 第一种: function titleCase(str) { var new ...
- BZOJ 3172: [Tjoi2013]单词 [AC自动机 Fail树]
3172: [Tjoi2013]单词 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 3198 Solved: 1532[Submit][Status ...
- [LeetCode] Concatenated Words 连接的单词
Given a list of words (without duplicates), please write a program that returns all concatenated wor ...
- [LeetCode] Word Squares 单词平方
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- [LeetCode] Valid Word Square 验证单词平方
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [LeetCode] Valid Word Abbreviation 验证单词缩写
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- [LeetCode] Maximum Product of Word Lengths 单词长度的最大积
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
随机推荐
- DLLHijack漏洞原理
原理 当一个可执行文件运行时,windows加载器讲课执行模块映射到进程的地址空间中,加载器分析可执行模块的输入表,并设法找出任何需要的DLL,并将它们映射到进程的地址空间中. 由于输入表中只包含DL ...
- 用php实现表格
<?php $contact2 =[ ['江苏'=>['华罗庚','河南','童第周']], ['河南'=>['童第周','华罗庚','河南']], ['河北'=>['刘恒', ...
- dubbo搭建
1.安装java : yum install java 2.下载Tomcat: wget http://mirrors.shu.edu.cn/apache/tomcat/tomcat-9/v9.0.1 ...
- Linq To Object 函数介绍
static void Main(string[] args) { #region Aggregate 把集合中的元素按照表达式依次执行 { IEnumerable<int> list = ...
- 系统学习PHP的一些思路
作为一名新进的PHPer,迫切的想提高自己的编码水平,目前市面上几乎没有高手来给新手指点出一条相对靠谱的途径. 首先,面对PHP众多杂乱无章的各种函数,到底该怎么去理清其各者之间的关系,经过一段时间的 ...
- 缺陷的背后---LIMIT M,N 分页查找
一.问题发现篇 最近组内做了一次典型缺陷分享时,翻阅2018年的缺陷,找到了一个让我觉得“有料”的bug(别的同事测试发现的),先大致简单的描述下这个问题: 需要实现的功能:从一个DB库同步某一段时间 ...
- python垃圾回收机制与小整数池
python垃圾回收机制 当引用计数为0时,python会删除这个值. 引用计数 x = 10 y = x del x print(y) 10 引用计数+1,引用计数+1,引用计数-1,此时引用计数为 ...
- python爬取指定新闻
作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2894 给定一篇新闻的链接newsUrl,获取该新闻的全部信息 标题 ...
- CAS部署在Windows上
我这里有下载好的cas.war和tomcat7,然后我在将cas.war放在tomcat目录下的webapps下,启动tomcat自动解压war包.浏览器输入http://localhost:8080 ...
- bzoj4665 小w的喜糖(dp+容斥)
4665: 小w的喜糖 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 222 Solved: 130[Submit][Status][Discuss ...