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 ...
随机推荐
- 下页小希学MVC5+EF6.2 学习记录三
目的:1 学习mvc+ef 2 写下日记也是对自己的督促 期待已久的EF终于来了. 学完本篇文章,你将会掌握基于EF数据模型的完整开发流程. 本次将会完成EF数据模型的搭建和使用. 基于这个模型 ...
- 剑指offer_2.1_Day_5
输入一个链表,按链表从尾到头的顺序返回一个ArrayList. import java.util.ArrayList; public class Solution { public ArrayList ...
- 发送邮件的几种方法(C#发邮件 和 js前台实现都有)C#后台自动发邮件 js发邮件
1.后台自动发邮件 1)首先设置邮件参数,这里写在configuration里面 <appSettings> <add key="SMTP" value=&quo ...
- POJ_3122 经典二分题
Pie Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8594 Accepted: 3124 Special Jud ...
- 一本通1402 Vigenère密码
[题目描述]6世纪法国外交家Blaise de Vigenère设计了一种多表密码加密算法——Vigenère密码.Vigenère密码的加密解密算法简单易用,且破译难度比较高,曾在美国南北战争中为南 ...
- spring aop中的propagation的7种配置
1.前言 在声明式的事务处理中,要配置一个切面,即一组方法,如 <tx:advice id="txAdvice" transaction-manager="txMa ...
- MySQL 存储引擎(MyISAM、InnoDB、NDBCluster)
前言 MySQL 的存储引擎可能是所有关系型数据库产品中最具有特色的了,不仅可以同时使用多种存储引擎,而且每种存储引擎和MySQL之间使用插件方式这种非常松的耦合关系. 由于各存储引擎功能特性差异较大 ...
- ubuntu16.04 pcl安装教程
https://blog.csdn.net/zkj126521/article/details/80157351 https://blog.csdn.net/e_small/article/detai ...
- Html4.0.1 标签使用笔记
1.lang=zh,en有什么用? 告诉搜索引擎爬虫,网站是关于什么内容的,优先显示网站排名.一般竞价排名,根据百度搜索引擎,需要签订关键词协议,首页第一个竞价排名大概是30-50元/条,竞价排名范围 ...
- SEO初步学习之新站优化
新站优化技巧:新站有两个月扶持期,在扶持期间仅做一件事,提交大量优质受众的原创,且内容为不间断,即每天定点定量发布文章,使得蜘蛛对网站形成爬行习惯,新站初期内容为王,优化为辅. 虽说优化为辅,却不可或 ...