题意:略

在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. WebApi接口访问异常问题。尝试创建“testController”类型的控制器时出错。请确保控制器具有无参数公共构造函数

    本来运行的好好的webAPI 接口突然报了个 :“尝试创建“testController”类型的控制器时出错.请确保控制器具有无参数公共构造函数” 错误.耗了半宿最终解决了, 原因: api控制器中引 ...

  2. “行业客户云原生最佳实践日” 亮相KubeCon上海

    2018年11月13日至15日,由CNCF主办的KubeCon + CloudNativeCon将首次登陆中国上海,这是全球范围内规模最大的Kubernetes和云原生技术盛会. 唯一聚焦客户实践的分 ...

  3. [python 练习] 计算个税

    题目:利用python计算个税 说明:python有序字典的使用 代码: # -*- coding: utf-8 -*- from collections import OrderedDict # 税 ...

  4. cnpm安装失败

    解决办法: 先:npm config set registry "http://registry.npmjs.org/" 再:npm install -g cnpm

  5. 微信不支持App下载的解决方案 微信跳转打开外部浏览器下载(苹果跳转商店下载)

    在微信中,打开app下载链接,或者使用微信扫一扫app下载二维码,都是无法下载app的. 因为腾讯为了自身利益,屏蔽了其他app直接在微信中下载.下面给分享下,找到的2种有效的解决方案. 方案:点击链 ...

  6. python 七段管模块

    python 七段管模块 def drawGap(): #绘制数码管间隔 turtle.penup() turtle.fd(5) def drawLine(draw): #绘制单段数码管 drawGa ...

  7. Winform 图片预览列表+分页显示

    针对图片列表展示信息,一开始没有做过相关类似的功能,大多都是以表格行显示为主,所以刚开始实现这个功能的时候是懵逼的.无从下口.在网上搜索一时半会也没找到合适的解决方案.大致就是类似于下图这样,每条数据 ...

  8. 问题 1690: 算法4-7:KMP算法中的模式串移动数组

    题目链接:https://www.dotcpp.com/oj/problem1690.html 题目描述 字符串的子串定位称为模式匹配,模式匹配可以有多种方法.简单的算法可以使用两重嵌套循环,时间复杂 ...

  9. ss命令详解

    ss是Socket Statistics的缩写.顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容.ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信息 ...

  10. 什么是Referer?Referer的作用?空Referer是怎么回事?

    什么是Referer? Referer是 HTTP请求header 的一部分,当浏览器(或者模拟浏览器行为)向web 服务器发送请求的时候,头信息里有包含 Referer.比如我在www.sojson ...