hdu2896&&3065
题: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的更多相关文章
- 【HDU2896】病毒侵袭 AC自动机
[HDU2896]病毒侵袭 Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋--我们能在有生之年看到500年 ...
- hdu 3065 AC自动机
// hdu 3065 AC自动机 // // 题目大意: // // 给你n个短串,然后给你一个长串,问:各个短串在长串中,出现了多少次 // // 解题思路: // // AC自动机,插入,构建, ...
- 【BZOJ】3065: 带插入区间K小值
http://www.lydsy.com/JudgeOnline/problem.php?id=3065 题意:带插入.修改的区间k小值在线查询.(原序列n<=35000, 询问<=175 ...
- AC自动机 - 多模式串的匹配运用 --- HDU 3065
病毒侵袭持续中 Problem's Link:http://acm.hdu.edu.cn/showproblem.php?pid=3065 Mean: 略 analyse: AC自动机的运用. 这一题 ...
- 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 ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 3065 病毒侵袭持续中
HDU 3065 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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 ...
- bzoj 3065: 带插入区间K小值 替罪羊树 && AC300
3065: 带插入区间K小值 Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 1062 Solved: 253[Submit][Status] Des ...
随机推荐
- DW1000芯片定位技术解析
近些年来随着物联网和机器人技术的大发展,精确定位技术的热度也随之攀升.目前精确定位的技术有很多,如基于wifi.RFID.zigbee.超声波.UWB等技术都可以实现精准定位.由于技术的不同,精度也不 ...
- 【转】Java线程面试题 Top 50(转)
不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特点就是内置了对并发的支持,让Java大受企业和程序员的欢迎.大多数待遇丰厚的Java开发职位都要求开发者精通多线程 ...
- 【LeetCode】二叉树的最大深度
[问题]给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数.说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null,null,15,7 ...
- java web实现在线编辑word,并将word导出(二)
前一篇文章介绍了后台将前台html转为word文档的一种方式,但却有一个问题是没法讲图片放置在生成的word报告中.我在网上找了很多方法,甚至将图片转换成base64编码的方式也不成功.效果如下: 由 ...
- JS常用的正则表达式包
结构: Code: /* 用途:检查输入的Email信箱格式是否正确 输入:strEmail:字符串 返回:如果通过验证返回true,否则返回false */ function checkEmail( ...
- 文献及代码阅读报告 - SS-LSTM:A Hierarchical LSTM Model for Pedestrian Trajectory Prediction
概览 简述 SS-LSTM全称Social-Scene-LSTM,是一种分层的LSTM模型,在已有的考虑相邻路人之间影响的Social-LSTM模型之上额外增加考虑了行人背景的因素.SS-LSTM架构 ...
- 短网址资料-nginx非root用户启动-systemctl启动脚本-分割root权限
https://www.cnblogs.com/aspnethot/articles/3492191.htmlhttps://www.cnblogs.com/aspnethot/articles/34 ...
- sql server ------创建本地数据库 SQL Server 排序规则
sql server完整复制数据库 sql server导入导出方法 SQL Server 排序规则
- MBR&/BOOT&GRUB
能正常工作的grub应该包 括一下文件:stage1.stage2.*stage1_5.menu.lst. 其中stage1要被安装(也就是写入)某个硬盘的主引导记录,或者某个活动分区(这个分区要 ...
- C语言中getopt()和getopt_long()函数的用法
一.参考文章 1.C语言中getopt()和getopt_long()函数的用法 2.linux 中解析命令行参数 (getopt_long用法) 二.调试经验