hdu 2222 Keywords_ac自动机模板
题意:给你n个单词,再给你一串字符,求在字符中有多少个单词出现过
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 10010
#define MAXLEN 1000010
struct node{
node *child[26];
node *fail;
int count;
void init(){
for(int i=0;i<26;i++)
child[i]=NULL;
count=0;
fail=NULL;
}
}*q[50*N];
node *root;
int head,tail;
void insert(char *str){//构建前缀树
node *now,*next;
int i=0,t;
now=root;
while(str[i]){
t=str[i]-'a';
if(now->child[t]!=NULL)
now=now->child[t];
else{
next=new node;
next->init();
now->child[t]=next;
now=next;
}
i++;
}
now->count++;
}
void build_fail(){
int i;
node *now,*p;
head=tail=0;
now=root;
q[tail++]=now;
while(head<tail){
now=q[head];
for(i=0;i<26;i++){
if(now->child[i]==NULL) continue;
if(now==root) now->child[i]->fail=root;
else{
p=now->fail;
while(p!=NULL){
if(p->child[i]!=NULL){
now->child[i]->fail=p->child[i];
break;
}
p=p->fail;
}
if(p==NULL) now->child[i]->fail=root;
}
q[tail++]=now->child[i];
}
head++;
}
}
int query(char *str){
node *now,*temp;
int i,t,cnt;
i=cnt=0;
now=root;
while(str[i]){
t=str[i]-'a';
while(now->child[t]==NULL&&now!=root) now=now->fail;
now=now->child[t];
if(now==NULL) now=root;
temp=now;
while(temp!=NULL&&temp->count!=-1){
cnt+=temp->count;
temp->count=-1;
temp=temp->fail;
}
i++;
}
return cnt;
}
int main(int argc, char** argv) {
int t,n;
char str[MAXLEN];
scanf("%d",&t);
while(t--){
scanf("%d",&n);
root=new node;
root->init();
while(n--){
scanf("%s",str);
insert(str);
}
build_fail();
scanf("%s",str);
printf("%d\n",query(str));
}
return 0;
}
hdu 2222 Keywords_ac自动机模板的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 2222 & ac自动机模板
题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...
- 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自动机)
HDU 2222 Keywords search Problem : 给若干个模式串,询问目标串中出现了多少个模式串. Solution : 复习了一下AC自动机.需要注意AC自动机中的fail,和n ...
- hdu 2222 ac自动机更新模板 for onSite contest
http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 #include <cstdio> #include <cstdlib> ...
- HDU 2222 ----AC自动机
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
- HDU 2222 AC自动机 裸题
题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...
- HDU 2222 AC自动机模版题
所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...
随机推荐
- LeeCode(Database)-Duplicate Emails
Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...
- linux 之 tar 命令
通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar 命令可以为linu ...
- Segment(技巧 相乘转换成相加 + java)
Segment Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- Android项目实战-云词典
前段时间在网上看到滴答滴答的一个项目,字典文章,找到一个简单的字.整句翻译功能,词汇,但漫长的岁月项目,二手API数据不再可用,我决定使用其现有的框架来实现其功能,主要采用的技术GSON和Volley ...
- Ext布局
Ext主要包括11种标准布局方式:Auto(自动布局).CheckboxGroup(复选框组布局).Fit(自适应布局).Column(列布局).Accordion(折叠布局).Table(表格布局) ...
- 利用cookie改变背景色
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 你想不到的IT运维前途
本人一毕业就走上了IT系统运维的道路,我之所以踏上这条路并一直坚持了下来,因为觉得运维工作并非一味关注技术,而是关注包括技术在内的更综合的解决方案,也就是说,做运维,自己要学的知识面更广,考虑问题要更 ...
- FtpClient的使用
摘自:http://hi.baidu.com/yuanhotel/item/000b6334894d11f42784f4da Java的ftp操作 package com.why.ftp; impor ...
- ios 文字图标
如何使用自定义字体 在讲icon font之前,首先先来看看普通自定义字体是如何在ios中使用的,两个原理是一样的.这里以KaushanScript-Regular为例: Step 1: 导入字体文件 ...
- Nginx反向代理匹配部分二级域名或二级目录配置
server { charset utf-; client_max_body_size 128M; # Add index.php to the list if you are using PHP i ...