Keywords Search HDU - 2222(ac自动机板题。。)
求一个字符串上有多少个匹配的单词
看着卿学姐的板子写的
指针形式:
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , maxm = , INF = 0x7fffffff;
int tot;
queue<int> q; struct state
{
int next[];
int fail, cnt;
}trie[]; void init()
{
while(!q.empty()) q.pop();
for(int i=; i<maxm; i++)
{
mem(trie[i].next, );
trie[i].fail = trie[i].cnt = ;
}
tot = ;
} void insert(char *s)
{
int n = strlen(s);
int rt = ;
for(int i=;i<n; i++)
{
int x = s[i] - 'a';
if(!trie[rt].next[x])
{
trie[rt].next[x] = ++tot;
}
rt = trie[rt].next[x];
}
trie[rt].cnt++;
} void build()
{
trie[].fail= -;
q.push();
while(!q.empty())
{
int u = q.front(); q.pop();
for(int i=; i<; i++)
{
if(trie[u].next[i])
{
if(u == ) trie[trie[u].next[i]].fail = ;
else{
int v = trie[u].fail;
while(v != -)
{
if(trie[v].next[i])
{
trie[trie[u].next[i]].fail = trie[v].next[i];
break;
}
v = trie[v].fail;
}
if(v == -) trie[trie[u].next[i]].fail = ;
}
q.push(trie[u].next[i]);
}
}
}
} int Get(int u)
{
int res = ;
while(u)
{
res = res + trie[u].cnt;
trie[u].cnt = ;
u = trie[u].fail;
}
return res;
} int math(char *s)
{
int n = strlen(s);
int rt = , res = ;
for(int i=; i<n; i++)
{
int x = s[i] - 'a';
if(trie[rt].next[x]) //如果有儿子 就直接下传
rt = trie[rt].next[x];
else //如果没有 就去fail去找
{
int p = trie[rt].fail;
while(p != - && trie[p].next[x] == ) p = trie[p].fail;
if(p == -) rt = ; //一直没找到 则返回祖结点
else rt = trie[p].next[x];
}
if(trie[rt].cnt) //如果以这个点为后缀的串还没有被匹配 则匹配
res = res + Get(rt);
}
return res; } int T, n;
char s[maxn];
int main()
{
scanf("%d", &T);
while(T--)
{
init();
scanf("%d", &n);
rap(i, , n)
{
scanf("%s", s);
insert(s);
}
build();
scanf("%s", s);
printf("%d\n", math(s));
}
return ;
}
数组形式:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<string>
#include<iostream>
#define maxn 1000010
using namespace std;
int n;
int nxt[maxn][],fail[maxn],edd[maxn],root,L;//nxt记录节点,在这里edd指针代表以当前节点为字符串尾的字符串个数
int vis[maxn];
int newnode()
{
for(int i=;i<;i++)
nxt[L][i]=-;//节点连接的边初始化为-1
edd[L]=;
vis[L]=;
return L++;
}
void init()
{
L=;
root=newnode();
} void insert(char *str)//trie树的建立
{
int l = strlen(str);
int now=root;
for(int i=;i<l;i++)
{
int x = str[i] - 'a';
if(nxt[now][x]==-)nxt[now][x]=newnode();
now=nxt[now][x];
}
edd[now]++;
}
void build()//建立ac自动机
{
queue<int> Q;
for(int i=;i<;i++)
{
if(nxt[root][i]==-)nxt[root][i]=root;
else //若有连边则将节点加入队列 ,并将fail指针指向root
{
fail[nxt[root][i]]=root;
Q.push(nxt[root][i]);
}
}
while(!Q.empty())
{
int now=Q.front();
Q.pop();
for(int i=;i<;i++)
{
if(nxt[now][i]==-) //若无连边,则将该边指向当前节点fail指针指向的相应字符连接的节点
nxt[now][i]=nxt[fail[now]][i];
else //若有连边,则将儿子节点的fail指针指向当前节点fail指针指向相应字符接的节点
{
fail[nxt[now][i]]=nxt[fail[now]][i];
Q.push(nxt[now][i]); //加入队列继续遍历
}
}
}
}
int query(char str[])
{
int l = strlen(str);
int now=root;
int res=;
for(int i=;i<l;i++)
{
int x = str[i] - 'a';
now=nxt[now][x];
int temp=now;
while(temp!=root&&vis[temp]==)//根据题目要求改变形式
{
res+=edd[temp];
edd[temp]=;
vis[temp]=;
temp=fail[temp];
}
}
return res; //在这里返回的是匹配到的模式串的数量
}
char str[maxn];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
init();
int ma=;
for(int i=;i<n;i++)
{
scanf("%s",str);
insert(str);
}
scanf("%s", str);
build();
int sum=query(str);
printf("%d\n", sum);
}
}
Keywords Search HDU - 2222(ac自动机板题。。)的更多相关文章
- 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 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- hdu 2222(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- 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 ...
- AC日记——Keywords Search hdu 2222
2222 思路: ac自动机模板题: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ...
- 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 Keywords Search 字符串问题 AC自动机
题目链接:https://cn.vjudge.net/problem/HDU-2222 题意 给一些关键词,和一个待查询的字符串 问这个字符串里包含多少种关键词 思路 AC自动机模版题咯 注意一般情况 ...
随机推荐
- 同步备份工具之 rsync
1.常用同步方法 SCP. NFS. SFTP. http. samba. rsync. drbd(基于文件系统同步,效率高) 2.rsync 介绍 rsync,英文全称是 remote synchr ...
- protobuf工程的编译以及使用
一. 获取Protocol Buffer 1.1 获得源码 Github:ProtocolBuffer源码 Git clone之:git clone https://github.com/google ...
- scrapy (一)
scrapy框架 scrapy 是一个爬虫框架,能够高效率,高层次的爬取页面的数据并进行处理. 在scrapy的英文文档中,有这样的一个流程图 scrapy 框架主要分为五大部分,spider, en ...
- Selenide 阶段性总结介绍(UI自动化测试工具)
今天给大家介绍一个比较新的UI自动化测试工具-- Selenide.确实是比较新的,国内应该还没有多少人用它.在百度和google上你只能搜到一个中文帖子简单介绍了一下.如果你想用这个工具,不可避免的 ...
- 【转】自动化测试 - Appium + Python史上最全最简环境搭建步骤
一,为什么是Appium借一张图: 1.1 Appium优点 l 开源 l 跨架构:NativeApp.Hybird App.Web App l 跨设备:Android.iOS.Firefox ...
- 第五章Web应用与应用层协议
Web应用与应用层协议 本篇博文中的主要参考文献是<计算机网络高级教程>,分别是吴功宜老先生和吴英教授合著.这部教程是我研究生老师所推荐的网络必读科目,由于该教程讲解的基础知识详细,但内容 ...
- 杂谈微服务架构下SSO&OpenAPI访问的方案。
本篇杂谈下微服务架构下WEB应用的浏览器与OpenAPI访问架构与方案.读者可对比传统架构下应用的此话话题的区别.或者有其它方案的欢迎交流
- linux云主机小技巧
微信服务器安装 安装库 python 3.5环境下 pip安装web.py时 会报错 "no module named "utils" 等问题 更换命令为“pip ins ...
- 论文笔记:DeepFace: Closing the Gap to Human-Level Performance in Face Verification
2014 CVPR Facebook AI研究院 简单介绍 人脸识别中,通常经过四个步骤,检测,对齐(校正),表示,分类 论文主要阐述了在对齐和表示这两个步骤上提出了新的方法,模型的表现超越了前人的工 ...
- Maven私有仓库搭建以及使用
一.使用Docker安装Nexus Docker search nexus docker pull docker.io/sonatype/nexus3 mkdir -p /usr/local/nexu ...