HDU 2222 AC自动机模板题
1、HDU 2222
2、题意:给出n个单词,一个字串,求有多少个单词在字串里出现了。注意给出的单词可能会重复,重复的不计。
3、总结:入门题。在查询这里还是不太懂。
//
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define F(i,a,b) for (int i=a;i<b;i++)
#define FF(i,a,b) for (int i=a;i<=b;i++)
#define mes(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
typedef long long ll;
const int N = 1e6+; struct node
{
int count;
node *next[];
node *fail;
void init() { //初始化
mes(next,NULL);
count=;
fail=NULL;
}
}*q[];
node *root;
int head,tail;
char str[N]; void insert(char *keyword)
{
int len=strlen(keyword);
node *p=root;
for(int i=; i<len; i++) {
int index=keyword[i]-'a';
if(p->next[index]==NULL) p->next[index]=new node(); //注:这里要new申请内存,不要忘了
p=p->next[index];
}
p->count++;
} void build_ac_automation() //初始化fail指针,BFS
{
q[tail++]=root;
while(head!=tail) {
node *p=q[head++]; //弹出队头
node *temp=NULL;
for(int i=; i<; i++) {
if(p->next[i]!=NULL) {
if(p==root) p->next[i]->fail=root; //第一个元素fail必指向根
else {
temp=p->fail;
while(temp) { //2种情况结束:匹配为空or找到匹配
if(temp->next[i]) { //找到匹配
p->next[i]->fail=temp->next[i];
break;
}
temp=temp->fail;
}
if(!temp) p->next[i]->fail=root; //为空则从头匹配
}
q[tail++]=p->next[i]; //入队
}
}
}
} void query() //扫描,查询这里还是没太搞懂
{
int i=,cnt=,index,len=strlen(str);
node *p=root;
for(int i=; str[i]; i++) {
index=str[i]-'a';
while(p->next[index]==NULL&&p!=root) p=p->fail; //跳转失败指针
p=p->next[index];
if(p==NULL) p=root;
node *temp=p; //p即是找到的当前结点,p不动,temp计算后缀串
while(temp!=root&&temp->count!=-) {
cnt+=temp->count;
temp->count=-;
temp=temp->fail;
}
}
printf("%d\n", cnt);
} int main()
{
int T,n;
char keyword[];
scanf("%d", &T);
while(T--) {
head=tail=;
scanf("%d", &n);
getchar();
root=new node();
while(n--) {
gets(keyword);
insert(keyword);
}
build_ac_automation();
gets(str);
query();
} return ;
}
HDU 2222 AC自动机模板题的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- hdu 2222(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 AC自动机 裸题
题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...
- HDU 2222 AC自动机模版题
所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...
- HDU 2222 & ac自动机模板
题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...
- HDU 2222 AC自动机(模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Keywords Search HDU - 2222 AC自动机板子题
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
随机推荐
- HTML 接收本地文件
HTML代码请把文件拖到下面的框里触发drop事件读取拖放的文件常用情况:结合XMLHttpRequest和拖放文件实现上传查看和管理本地文件和图片 <!DOCTYPE HTML> < ...
- scrapy加载cookies登陆
import scrapy from xxxx.items import XXXXItem from scrapy.http.request import Request class ZndsSpid ...
- 使用wget命令时发生错误
用的是centos6.5, 当我使用命今 sudo wget https://cmake.org/files/v3.6/cmake-3.6.1.tar.gz 下载个cmake的包时, 发生了这样的错误 ...
- 关于SimpleAdapter和ListView结合使用,实现列表视图的笔记
使用ListView需要为其添加适配器: 适配器有两种:1.ArrayAdapter --用于单独文字显示 2.SimpleAdapter --用于文字和图片显示 这里主要记录SimpleAdapt ...
- 采用MVC模式JDBC演示案例
MVC三层架构: Model 模型层,数据处理和业务逻辑 View 视图层,为客户展示内容 Control 控制层,协调控制,更新模型 案例如下: 1.获得数据库连接 package com.db; ...
- Apple Pay 初探
Apple Pay 一.概述 1.支付方式:Touch ID/ Passcode 2.设备要求:iPhone6以上(iphone:线上/线下 ipad:线上 watch:线下) 3.系统要求:iOS8 ...
- WinForm BaseClass类常用通用方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- React学习笔记-7-销毁阶段
销毁阶段可以使用的函数:componentWillUnmount:在删除组件之前进行清理操作,比如计时器和事件监听器.因为这些函数都是开发者手动加上去的,react不知道,必须进行手动清理. 实例第一 ...
- gloolooer
V1.2gloolooer金[http://sh.yun.ftn.qq.com/ftn_handler/0a9043ee7aa120d3c864281211dff2f08efe5e2d42438ec5 ...
- Python中的网络编程
TCPServer端: __author__ = 'Nature' # -*- coding: utf-8 -*- from socket import * from time import ctim ...