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自动机模板题的更多相关文章

  1. HDU 2222 AC自动机模板题

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

  2. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  3. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

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

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

  5. HDU 2222 AC自动机 裸题

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

  6. HDU 2222 AC自动机模版题

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

  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 ...

  10. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

随机推荐

  1. angularJs 控制文本长度,超出省略号过滤器

    1.控制文本长度,超出省略号 在模版中: <label>{{item.abbr| cut:true:20:' ...' }}<label> js: angular.module ...

  2. Intern---Microsoft Academic China Team

    项目二: AEther: 项目 一.项目需求:对搜索关键词进行类别的统计分析,为了后面的entity-rank做准备. 0,各种关键数据统计: 数据量:1个月数据:about 1000T. 1,对IE ...

  3. springmvc结合ajax实现跨域上传文件

    本方法的思路是:先在前端利用FileReader将图片转换成base64编码,然后将编码字符串形式传递到后台(前提是服务端设置了允许跨域),后端再把base64编码转换成图片. 前端代码: <! ...

  4. web前端页面性能优化

    影响用户访问的最大部分是前端的页面.网站的划分一般为二:前端和后台.我们可以理解成后台是用来实现网站的功能的,比如:实现用户注册,用户能够为文章发表评论等等.而前端呢?其实应该是属于功能的表现. 而我 ...

  5. printf对齐

    C语言中,将printf函数打印出的字符像表格一样分类对齐.%-10d表示这个字符型占10个字节,负号表示左对齐.即下面表格中的x1位置开始填充.如果是%10d,表示右对齐,即在x10位置对齐. x1 ...

  6. String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";作用!!!!!

    <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+re ...

  7. cout 格式化输出

    一直习惯于C语言的printf函数来打印,突然有一天要用cout来打印,发现有点不适应. 原来cout也是有格式化输出的. 首先要引入头文件 #include<iostream> // 在 ...

  8. SQL中TOP,LIMIT,ROWNUM的用法

    SQL SERVER/MS Access的Select Top的用法: Select TOP number|percent table_columname FROM tablename MySQL/O ...

  9. Linux学习笔记之兄弟连

    systemctl --user enable pulseaudio说明:安装完成后系统没有声音,用该命令可以打开.ifconfig eth0 192.168.118.1说明:给网卡设置IP地址.ap ...

  10. Delphi之通过代码示例学习XML解析、StringReplace的用法

    这个程序可以用于解析任何合法的XML字符串. 首先是看一下程序的运行效果: 以解析这样一个XML的字符串为例: <?xml version="1.0" encoding=&q ...