hdu 3056 病毒侵袭持续中 AC自己主动机
http://acm.hdu.edu.cn/showproblem.php?pid=3065
刘汝佳的模板真的非常好用,这道题直接过
学到:
cnt数组记录单词出现次数
以及map存储单词编号与字符串,便于处理相关信息
上代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <map>
using namespace std; const int SIGMA_SIZE=128;
const int MAXNODE = 1005*55;
const int SSIZE = 2000000+100; struct AC
{
int f[MAXNODE];
int ch[MAXNODE][SIGMA_SIZE];
int cnt[MAXNODE];
int val[MAXNODE];
int last[MAXNODE];
int sz;
void init()
{
memset(cnt,0,sizeof(cnt));
memset(ch[0],0,sizeof(ch[0]));
sz=1;
f[0]=last[0]=val[0]=0;
}
inline void clear(){memset(cnt,0,sizeof(cnt));}
void insert(string s,int v)
{
int u=0,n=s.size();
for(int i=0;i<n;i++)
{
int c=s[i];
if(!ch[u][c])
{
val[sz]=0;
memset(ch[sz],0,sizeof(ch[sz]));
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]=v;
}
void print(int j)
{
if(j)
{
cnt[val[j]]++;
print(last[j]);
}
}
void getfail()
{
queue<int>q;
for(int c=0;c<SIGMA_SIZE;c++)
{
int u=ch[0][c];
if(u){f[u]=0;q.push(u);last[u]=0;}
}
while(!q.empty())
{
int r=q.front();q.pop();
for(int c=0;c<SIGMA_SIZE;c++)
{
int u=ch[r][c];
if(!u)continue;
q.push(u);
int v=f[r];
while(v && !ch[v][c])v=f[v];
f[u]=ch[v][c];
last[u]=val[f[u]]?f[u]:last[f[u]];
}
}
}
void find(char *T)
{
int n=strlen(T);
int j=0;
for(int i=0;i<n;i++)
{
int c = T[i];
while(j && !ch[j][c])j=f[j];
j=ch[j][c];
if(val[j])print(j);
else
if(last[j])print(last[j]);
}
}
}; AC ac;
char str[SSIZE];
int main()
{
//freopen("hdu3056.txt","r",stdin);
int n;
string word;
while(~scanf("%d",&n))
{
ac.init();
map<int, string> ms;
for(int i=1;i<=n;i++)
{
cin >> word;
ms[i]=word;
ac.insert(word,i);
}
ac.getfail();
scanf("%s",str);
ac.find(str);
for(int i=1;i<=n;i++)
{
////////////////
//printf("%d %d\n",i,ac.cnt[i]);
///////////////////
if(ac.cnt[i])
{
cout << ms[i] << ": " << ac.cnt[i] << endl;
}
}
}
return 0;
}
hdu 3056 病毒侵袭持续中 AC自己主动机的更多相关文章
- HDU 3065 病毒侵袭持续中 (AC自动机)
题目链接 Problem Description 小t非常感谢大家帮忙解决了他的上一个问题.然而病毒侵袭持续中.在小t的不懈努力下,他发现了网路中的"万恶之源".这是一个庞大的病毒 ...
- HDU 3065 病毒侵袭持续中
HDU 3065 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3065 病毒侵袭持续中(AC自己主动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3065 Problem Description 小t非常感谢大家帮忙攻克了他的上一个问题.然而病毒侵袭 ...
- hdu 3065 病毒侵袭持续中【AC自动机】
<题目链接> 题目大意: 小t非常感谢大家帮忙解决了他的上一个问题.然而病毒侵袭持续中.在小t的不懈努力下,他发现了网路中的“万恶之源”.这是一个庞大的病毒网站,他有着好多好多的病毒,但是 ...
- hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。
/** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...
- hdu----(3065)病毒侵袭持续中(AC自动机)
病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- hdu 3065病毒侵袭持续中
病毒侵袭持续中 http://acm.hdu.edu.cn/showproblem.php?pid=3065 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU3065 病毒侵袭持续中 —— AC自动机
题目链接:https://vjudge.net/problem/HDU-3065 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- HDU 3065 病毒侵袭持续中 (模板题)
病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
随机推荐
- [Angular 2] Simple intro Http
To use http, we need to import the HTTP_PROVIDER, so that we can inject http to other component: imp ...
- (转)Facade模式
Facade模式要求一个子系统的外部与其内部的通信必须通过一个统一的Facade对象进行.Facade模式提供一个高层次的接口,使得子系统更易于使用. 就如同医院的接待员一样,Facade模式的Fac ...
- SqlServer死锁与阻塞检测脚本
IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'sp_Lock_Scan') DROP PROCEDURE sp_Lock_Scan GO CR ...
- centos静默式安装Oracle11g
1. Centos及Oracle版本 Centos:CentOS release 6.4 (Final) Oracle:linux.x64_Oracle_11gR2_database 2. 硬 ...
- POJ 1064 Cable master(二分查找+精度)(神坑题)
POJ 1064 Cable master 一开始把 int C(double x) 里面写成了 int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条 ...
- linux下enum的使用
enum T { status1, status2, } Linux下: 1.做函数返回值时enum T f():不能写成T f(): 2.if(i == status1)不能写成 if(i == T ...
- Mysql 索引的基础(上)
要理解Mysql 中索引是如何工作的,最简单的方法是去看一看书的"索引部分":如果想在一本书中找到某个特定的主题,一般先看书的"索引",找到对应的页码. 在My ...
- MySQL 忘记密码后的重置操作
一.修改配置文件方式 1.关闭 MySQL linux: 1)service mysqld stop ...
- ECSTORE关于MONGODB安装
1.安装mongodb wget http://www.phpwindow.com/linux/mongodb-linux-x86_64-2.2.1.tgz tar zxvf mongodb-linu ...
- ubuntu 14.04解决gedit中文乱码的问题
终端输入 gsettings set org.gnome.gedit.preferences.encodings auto-detected "['UTF-8','GB18030','GB2 ...