题:http://acm.hdu.edu.cn/showproblem.php?pid=2896

分析:ac自动机模板

   注意细节,1、128个ascii码都要;

        2、只要关键码含有只输出一个编号就行

#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
typedef long long ll;
#define pb push_back
const int M=*;
const int N=;
int vis[N];
int n;
struct ac{
int tot,root;
int trie[M][],end[M],fail[M];
int newnode(){
for(int i=;i<;i++){
trie[tot][i]=-;
}
end[tot]=;
tot++;
return tot-;
}
void init(){
tot=;
root=newnode();
}
void insert(char *buf,int id){
int now=root,len=strlen(buf);
for(int i=;i<len;i++){
if(trie[now][buf[i]]==-)
trie[now][buf[i]]=newnode();
now=trie[now][buf[i]];
}
end[now]=id;
}
void getfail(){
queue<int>que;
while(!que.empty())
que.pop();
fail[root]=root;
for(int i=;i<;i++)
if(trie[root][i]==-)
trie[root][i]=;
else{
fail[trie[root][i]]=root;
que.push(trie[root][i]);
}
while(!que.empty()){
int now=que.front();
que.pop();
for(int i=;i<;i++){
if(trie[now][i]!=-){
fail[trie[now][i]]=trie[fail[now]][i];
que.push(trie[now][i]);
}
else
trie[now][i]=trie[fail[now]][i];
}
}
}
int query(char *buf){
int len=strlen(buf);
int ans=;
int now=root;
for(int i=;i<len;i++){
now=trie[now][buf[i]];
int tmp=now;
while(tmp!=root){
if(end[tmp]!=){
vis[end[tmp]]=;
ans++;
}
tmp=fail[tmp]; }
}
return ans;
}
}AC;
char s1[],s[];
int main(){
while(~scanf("%d",&n)){
AC.init();
int ans=;
for(int i=;i<=n;i++){
scanf("%s",s);
AC.insert(s,i);
}
AC.getfail();
int m;
scanf("%d",&m);
for(int i=;i<=m;i++){
memset(vis,,sizeof(vis));
scanf("%s",s1);
if(AC.query(s1)>=){
printf("web %d:",i);
for(int j=;j<=n;j++)
if(vis[j])
printf(" %d",j);
printf("\n");
ans++;
}
}
printf("total: %d\n",ans);
}
return ;
}

题:http://acm.hdu.edu.cn/showproblem.php?pid=3065

分析:只要将上题的vis改为技术数组即可

#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
typedef long long ll;
#define pb push_back
const int M=*;
const int N=;
char s[N][],s1[];
int n;
struct ac{
int tot,root;
int trie[M][],end[M],fail[M],vis[M];
int newnode(){
for(int i=;i<;i++){
trie[tot][i]=-;
}
end[tot++]=;
return tot-;
}
void init(){
tot=;
root=newnode();
}
void insert(char *buf,int id){
int len=strlen(buf);
int now=root;
for(int i=;i<len;i++){
if(trie[now][buf[i]]==-)
trie[now][buf[i]]=newnode();
now=trie[now][buf[i]];
}
end[now]=id;
}
void getfail(){
queue<int>que;
while(!que.empty()){
que.pop();
}
fail[root]=root;
for(int i=;i<;i++){
if(trie[root][i]==-)
trie[root][i]=root;
else{
fail[trie[root][i]]=root;
que.push(trie[root][i]);
}
}
while(!que.empty()){
int now=que.front();
que.pop();
for(int i=;i<;i++){
if(trie[now][i]!=-){
fail[trie[now][i]]=trie[fail[now]][i];
que.push(trie[now][i]);
}
else
trie[now][i]=trie[fail[now]][i];
}
}
}
void query(char *buf){
memset(vis,,sizeof(vis));
int len=strlen(buf);
int now=root;
for(int i=;i<len;i++){
now=trie[now][buf[i]];
int tmp=now;
while(tmp!=root){
if(end[tmp]!=)
vis[end[tmp]]++;
tmp=fail[tmp];
}
}
for(int i=;i<=n;i++)
if(vis[i]>)
printf("%s: %d\n",s[i],vis[i]);
}
}AC;
int main(){
while(scanf("%d",&n)==){
AC.init();
for(int i=;i<=n;i++){
scanf("%s",s[i]);
AC.insert(s[i],i);
}
AC.getfail(); scanf("%s",s1);
AC.query(s1);
}
return ;
}

hdu2896&&3065的更多相关文章

  1. 【HDU2896】病毒侵袭 AC自动机

    [HDU2896]病毒侵袭 Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋--我们能在有生之年看到500年 ...

  2. hdu 3065 AC自动机

    // hdu 3065 AC自动机 // // 题目大意: // // 给你n个短串,然后给你一个长串,问:各个短串在长串中,出现了多少次 // // 解题思路: // // AC自动机,插入,构建, ...

  3. 【BZOJ】3065: 带插入区间K小值

    http://www.lydsy.com/JudgeOnline/problem.php?id=3065 题意:带插入.修改的区间k小值在线查询.(原序列n<=35000, 询问<=175 ...

  4. AC自动机 - 多模式串的匹配运用 --- HDU 3065

    病毒侵袭持续中 Problem's Link:http://acm.hdu.edu.cn/showproblem.php?pid=3065 Mean: 略 analyse: AC自动机的运用. 这一题 ...

  5. Sublime Text 3 Build 3065 All System CracKed By Hmily[LCG]

    Sublime Text 3 Build 3065 All System CracKed By Hmily[LCG] <ignore_js_op> 程序员文本编辑器 Sublime Tex ...

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

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

  7. HDU 3065 病毒侵袭持续中

    HDU 3065 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. How to Cracked Sublime Text 3 Build 3065 in Ubuntu (Linux)

    整理自How to Cracked Sublime Text 3 Build 3065 in Ubuntu (Linux) Sublime Text 3 Build 3065 Release Date ...

  9. bzoj 3065: 带插入区间K小值 替罪羊树 && AC300

    3065: 带插入区间K小值 Time Limit: 60 Sec  Memory Limit: 512 MBSubmit: 1062  Solved: 253[Submit][Status] Des ...

随机推荐

  1. bzoj 4195程序自动分析

    先离散一下,然后并查集就好了. (一开始作大死,没全离散,WA一片) #include<bits/stdc++.h> #define INF 0x7fffffff #define LL l ...

  2. LabVIEW面向对象的ActorFramework(3)

    四.LabVIEW面向对象的编程架构:Actor Framework Actor Framework是一个软件类库,用以支持编写有多个VI独立运行且相互间可通信的应用程序,在该类型应用程序中,每个VI ...

  3. Android自定义View——实现字母导航栏

    1.自定义View实现字母导航栏 2.ListView实现联系人列表 3.字母导航栏滑动事件处理 4.字母导航栏与中间字母的联动 5.字母导航栏与ListView的联动 1.先看主布局,方便后面代码的 ...

  4. Nginx系列p4:进程结构

    Nginx 有两种进程结构:单进程结构,多进程结构.本篇文章我们主要说多进程结构. 问:那为什么 Nginx 采用多进程结构,而不是多线程结构呢? 答:这是因为 Nginx 最核心的目的就是要保证高可 ...

  5. html_位置偏移属性position

    定位属性 位置属性position:static.relative.absolute.fixed 偏移属性:top.bottom.left.right 浮动定位属性:float/clear 1.浮动定 ...

  6. Java自学-集合框架 聚合操作

    聚合操作 步骤 1 : 聚合操作 JDK8之后,引入了对集合的聚合操作,可以非常容易的遍历,筛选,比较集合中的元素. 像这样: String name =heros .stream() .sorted ...

  7. JavaScript 之 原型及原型链

    对象[回顾] 通过字面量创建对象 //通过字面量创建对象 var obj1 = { name:'Jack', age: 18 } 通过系统自带的构造函数构造对象 // 通过系统自带的构造函数构造对象 ...

  8. 吴裕雄--天生自然 JAVASCRIPT开发学习: 验证 API

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> ...

  9. TCP三次握手和四次挥手相关

    客户端A 服务端BSYN (建立连接位标识 1为建立联机) ACK (确认位标识 1为确认) seq (一个随机顺序码) ack(一个确认号码,通常为seq+1) 三次握手:1.A 发起建立 连接 的 ...

  10. 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 A题

    A - ^&^ Bit operation is a common computing method in computer science ,Now we have two positive ...