题意:略

在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单词的更多相关文章

  1. java统计字符串单词的个数

    在一些项目中可能需要对一段字符串中的单词进行统计,我在这里写了一个简单的demo,有需要的同学可以拿去看一下. 本人没怎么写个播客,如果有啥说的不对的地方,你来打我啊 不说废话了直接贴代码: 实现代码 ...

  2. JavaScript将字符串中的每一个单词的第一个字母变为大写其余均为小写

    要求: 确保字符串的每个单词首字母都大写,其余部分小写. 这里我自己写了两种方法,或者说是一种方法,另一个是该方法的变种. 第一种: function titleCase(str) { var new ...

  3. BZOJ 3172: [Tjoi2013]单词 [AC自动机 Fail树]

    3172: [Tjoi2013]单词 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 3198  Solved: 1532[Submit][Status ...

  4. [LeetCode] Concatenated Words 连接的单词

    Given a list of words (without duplicates), please write a program that returns all concatenated wor ...

  5. [LeetCode] Word Squares 单词平方

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  6. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  7. [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  8. [LeetCode] Valid Word Abbreviation 验证单词缩写

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  9. [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 ...

随机推荐

  1. HTML5的优点与缺点?

    优点:a.网络标准统一.HTML5本身是由W3C推荐出来的. b.多设备.跨平台 c.即时更新,提高可用性和改进用户的友好体验: d.有几个新的标签,这将有助于开发人员定义重要的内容: e.可以给站点 ...

  2. CF 219D 树形DP

    CF 219D [题目链接]CF 219D [题目类型]树形DP &题意: 给一个n节点的有向无环图,要找一个这样的点:该点到其它n-1要逆转的道路最少,(边<u,v>,如果v要到 ...

  3. #WEB安全基础 : HTTP协议 | 0x9 GET和POST请求以及请求URI的方式

    请求URI的方式 1.URI为完整的请求URI GET http://hackr.jp/index.htm HTTP/1.1 2.在首部字段Host中写明域名或IP地址 GET/index.htm H ...

  4. 从拥抱开源到回馈开源,灵雀云助力CNCF中国区培训业务

    6月27日,全球首屈一指的开源盛会 2018 LinuxCon + ContainerCon + CloudOpen China (LC3)在中国北京国家会议中心落下帷幕.二度落地中国的LC3大会热度 ...

  5. sql server导出数据,远程连接失败,需要设置权限

    在sql  server management中右键当前连接——>方面 在 服务器配置中 将  RemoteAccessEnabled.RemoteDacEnabled设置为TRUE 安全性—— ...

  6. Xamarin.Forms + Prism,整理页面导航跳转流程

    3个Page,Page1 -> Page2 -> Page3 -> Page2 -> Page1. PageViewModel实现接口:INavigatingAware, IN ...

  7. SQL Server 复制表结构以及数据,去除表中重复字段

    --复制另一个数据库中的某张表的结构及数据--select * from Test.dbo.TestTable(查询表中所有数据) --into [表名] 插入当前数据库新表,如果没有该表就创建 se ...

  8. 针对多条件查询,应对 url 无用 null 值现象处理

    多条件查询 应对 url 无用 null 值现象 处理例如:http://xxoo.b2b.com/orders?city_id=5&repertory_id=7&area_id=39 ...

  9. SQL 聚集函数(聚组函数)的使用 注意事项

    原文地址 http://hi.baidu.com/sdunus/blog/item/333c1a29305041fe99250aad.html 在介绍GROUP BY 和 HAVING 子句前,我们必 ...

  10. Ubuntu 无界面使用selenium chrome + headless

    1. 安装 selenium : sudo pip install selenium 2. 安装 chromdriver: 进入 淘宝镜像源 下载 chromdriver, 可以查看 notes.tx ...