HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896
题目大意:多个模式串。多个匹配串。其中串的字符范围是(0~127)。问匹配串中含有哪几个模式串。
解题思路:
AC自动机模板题。注意一下字符范围。
cnt记录这个模式串的个数改为这个模式串的index。
find的时候,把找到的index压入vector里面即可。
注意有多个匹配串,每次find之后会把last->cnt修改,原因是防止一个模式串出现了多次被压入vector,所以先备份一下,再还原回来。
- #include "cstdio"
- #include "cstring"
- #include "string"
- #include "iostream"
- #include "queue"
- #include "vector"
- #include "algorithm"
- using namespace std;
- #define maxn 130
- struct Trie
- {
- Trie *next[maxn],*fail;
- int cnt;
- }*root;
- struct status
- {
- Trie *last;
- int cnt;
- status(Trie *last,int cnt):last(last),cnt(cnt) {}
- };
- Trie *newnode()
- {
- Trie *ret=new Trie;
- memset(ret->next,,sizeof(ret->next));
- ret->fail=;
- ret->cnt=;
- return ret;
- }
- void init() {root=newnode();}
- void Insert(string str,int index)
- {
- Trie *pos=root;
- for(int i=;i<str.size();i++)
- {
- int c=str[i];
- if(!pos->next[c]) pos->next[c]=newnode();
- pos=pos->next[c];
- }
- pos->cnt=index;
- }
- void getfail()
- {
- queue<Trie *> Q;
- for(int c=;c<maxn;c++)
- {
- if(root->next[c])
- {
- root->next[c]->fail=root;
- Q.push(root->next[c]);
- }
- else root->next[c]=root;
- }
- while(!Q.empty())
- {
- Trie *x=Q.front();Q.pop();
- for(int c=;c<maxn;c++)
- {
- if(x->next[c])
- {
- x->next[c]->fail=x->fail->next[c];
- Q.push(x->next[c]);
- }
- else x->next[c]=x->fail->next[c];
- }
- }
- }
- vector<int> find(string str)
- {
- Trie *pos=root,*last;
- queue<status> Q;
- vector<int> ans;
- for(int i=;i<str.size();i++)
- {
- int c=str[i];last;
- if(pos->next[c])
- {
- pos=pos->next[c];
- last=pos;
- while(last->cnt)
- {
- Q.push(status(last,last->cnt));
- ans.push_back(last->cnt);
- last->cnt=; //修改last->cnt
- last=last->fail;
- }
- }
- }
- while(!Q.empty()) //恢复last->cnt
- {
- status x=Q.front();Q.pop();
- x.last->cnt=x.cnt;
- }
- return ans;
- }
- int main()
- {
- //freopen("in.txt","r",stdin);
- ios::sync_with_stdio(false);
- int n,m;
- string tt;
- while(cin>>n)
- {
- init();
- for(int i=;i<=n;i++)
- {
- cin>>tt;
- Insert(tt,i);
- }
- getfail();
- cin>>m;
- int cnt=;
- for(int i=;i<=m;i++)
- {
- cin>>tt;
- vector<int> ans=find(tt);
- sort(ans.begin(),ans.end());
- if(!ans.size()) continue;
- cnt++;
- printf("web %d:",i);
- for(int j=;j<ans.size();j++) printf(" %d",ans[j]);
- printf("\n");
- }
- printf("total: %d\n",cnt);
- }
- }
2871595 | neopenx | HDU | 2896 | Accepted | 29988 | 343 | C++ | 2565 |
9 min ago
|
HDU 2896 (AC自动机模板题)的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- hdu 2896 AC自动机模版题
题意:输出出现模式串的id,还是用end记录id就可以了. 本题有个关键点:“以上字符串中字符都是ASCII码可见字符(不包括回车).” -----也就说AC自动机的Trie树需要128个单词分支. ...
- HDU 2896 AC自动机 裸题
中文题题意不再赘述 注意字符范围是可见字符,从32开始到95 char c - 32 #include <stdio.h> #include <string.h> #inclu ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- hdu 2896 AC自动机
// hdu 2896 AC自动机 // // 题目大意: // // 给你n个短串,然后给你q串长字符串,要求每个长字符串中 // 是否出现短串,出现的短串各是什么 // // 解题思路: // / ...
- HDU3695(AC自动机模板题)
题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...
- HDu-2896 病毒侵袭,AC自动机模板题!
病毒侵袭 模板题,不多说了.. 题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...
- [Bzoj3940] [AC自动机,USACO 2015 February Gold] Censor [AC自动机模板题]
AC自动机模板题(膜jcvb代码) #include <iostream> #include <algorithm> #include <cstdio> #incl ...
随机推荐
- Instance Variables in ruby
Dogs have many shared characteristics, like the abilities to wag their tails and drink water from a ...
- puppet之自定义fact(转载)
1.使用环境变量'FACTERLIB'创建fact 1.1.在自定义目录里面定义一个fact,列出当前系统登录的用户数 [root@agent1 ~]# vim /var/lib/puppet/kis ...
- apache ab压力测试报错(apr_socket_recv: Connection reset by peer (104))
apache ab压力测试报错(apr_socket_recv: Connection reset by peer (104)) 今天用apache 自带的ab工具测试,当并发量达到1000多的时 ...
- PHP编译支持mysqli
PHP编译支持mysqli前提是必须安装mysql直接上命令先进入源码包我的源码包是在/usr/local/php-5.2.1/ext/mysqli这样进入 cd /usr/local/php-5.2 ...
- shell脚本的调试技巧
请参考文章:http://www.ibm.com/developerworks/cn/linux/l-cn-shell-debug/index.html 读后的感觉,还是用shell的选项灵活,方便. ...
- Continuous Subarray Sum
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- 【USACO】checker
一看题目 经典的8皇后问题 不过是皇后数量可变而已 不用想 回溯法. 需要个生成每次可选择序列的函数, 在存储可选择的序列时按照先大后小的顺序排的.这样每次找最小和去掉最小都很方便,只要有个记录数量的 ...
- CodeForces - 405C
Unusual Product Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Sub ...
- Servlet、JSP选择题(2)
Java EE软件工程师认证考试 试题库-选择题 一. 选择题(包括单选和双选) 1.B 编写一个Filter,需要( ) A. 继承Filter 类 B. 实现Filter 接口 C. 继承 ...
- php 克隆和引用类
/*class Ren { public $name; public $sex; function __construct($n,$s) { $this->name=$n; $this-> ...