http://acm.split.hdu.edu.cn/showproblem.php?pid=2222

#include <cstdio>
#include <cstdlib>
#include <cstring>
const int maxn = + ;
const int N = ; struct AC {
int son[maxn][N], fail[maxn * N], endPos[maxn * N];
int t, root;
int create() {
++t;
for (int i = ; i < N; ++i) son[t][i] = NULL;
fail[t] = endPos[t] = NULL;
return t;
}
void init() {
t = ;
root = create();
}
int getID(char ch) {
return ch - 'a';
}
void toInsert(char str[]) {
int now = root;
for (int i = ; str[i]; ++i) {
int id = getID(str[i]);
if (son[now][id] == NULL) son[now][id] = create();
now = son[now][id];
}
endPos[now]++;
}
int que[maxn * N];
void buildFail() {
fail[root] = root;
int head = , tail = ;
for (int i = ; i < N; ++i) {
if (son[root][i] == NULL) son[root][i] = root;
else {
fail[son[root][i]] = root;
que[tail++] = son[root][i];
}
}
while (head < tail) {
int cur = que[head++];
for (int i = ; i < N; ++i) {
if (son[cur][i] == NULL) son[cur][i] = son[fail[cur]][i];
else {
fail[son[cur][i]] = son[fail[cur]][i]; //虚拟边已经存在
que[tail++] = son[cur][i];
}
}
}
}
int query(char str[]) {
int now = root, ans = ;
for (int i = ; str[i]; ++i) {
int id = getID(str[i]);
now = son[now][id];
int p = now;
while (p != root && endPos[p] != -) {
ans += endPos[p];
endPos[p] = -;
p = fail[p];
}
}
return ans;
}
} ac; char str[maxn];
void work () {
ac.init();
int n;
scanf("%d", &n);
while (n--) {
scanf("%s", str + );
ac.toInsert(str);
}
scanf("%s", str + );
ac.buildFail();
printf("%d\n", ac.query(str));
return ;
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
#endif
int t;
scanf("%d", &t);
while (t--) {
work ();
}
return ;
}

hdu 2222 ac自动机更新模板 for onSite contest的更多相关文章

  1. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  2. HDU 2222 (AC自动机)

    HDU 2222 Keywords search Problem : 给若干个模式串,询问目标串中出现了多少个模式串. Solution : 复习了一下AC自动机.需要注意AC自动机中的fail,和n ...

  3. HDU 2222 ----AC自动机

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  4. HDU 2222 AC自动机 裸题

    题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...

  5. HDU 2222 AC自动机模版题

    所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...

  6. hdu 2222(AC自动机模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  7. HDU 2222 & ac自动机模板

    题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...

  8. HDU 2222 AC自动机(模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  9. Keywords Search HDU - 2222 AC自动机板子题

    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...

随机推荐

  1. backstop bsg6

  2. AR# 30522:LogiCORE RapidIO - How do system_reset and link_reset work?

    Description How do system_reset and link_rest work? Solution lnk_linkreset_n (input): In Xilinx SRIO ...

  3. WebAPI 请求跨域问题

    本人采用的是利用CORS解决跨越问题. 首先利用Nuget 安装“microsoft.aspnet.webapi.cors”,如下图所示: 紧接着,在WebApiConfig文件中加入 config. ...

  4. 基于ef core 2.0的数据库增删改审计系统

    1.首先是建审计存储表 CREATE TABLE [dbo].[Audit] ( [Id] [uniqueidentifier] NOT NULL, [EntityName] [nvarchar](1 ...

  5. GDI+绘图基础

    GDI+ 指的是.NET Framwork中提供的二维图像.图像处理等功能,是构成Windows操作系统的一个子系统,它提供了图形图像操作的应用程序编程接口(API). 使用GDI+可以用相同的方式在 ...

  6. 背包DP【洛谷P2113】 看球泡妹子

    P2113 看球泡妹子 题目背景 2014年巴西世界杯开幕了,现在满城皆是世界杯,商家们利用它大赚一笔,小明和小红也借此机会增进感情. 题目描述 本届世界杯共有N支球队,M场比赛.男球迷小明喜欢看比赛 ...

  7. [比赛|考试]nowcoder NOIPpj组第二场

    nowcoder NOIPpj组第二场 370pts/400pts(100,100,100,70) rank3 给自己的反思:前3题都A了,T4O(N^2)不会就是不会(没准是我懒得推了),DP了70 ...

  8. log4j配置文件及java调用 每个级别输出到不同的文件2

    log4j.rootLogger=debug,stdout,info,debug,error log4j.appender.stdout=org.apache.log4j.ConsoleAppende ...

  9. POI操作Excel:cell的背景颜色类型

    POI中背景颜色设置方法如下: HSSFCellStyle style = workbook.createCellStyle(); style.setFillBackgroundColor(HSSFC ...

  10. jenkins在windows系统下部署安装,使用

    首先需要从官网上下载下来war包,让进入tomcat中 启动tomcat,然后可以看一堆日志 再在网站输入 localhost:8080/jenkins就会进去下面界面: 会出现上面状况: 需要进入: ...