hdu 2896 AC自动机模版题
题意:输出出现模式串的id,还是用end记录id就可以了。
本题有个关键点:“以上字符串中字符都是ASCII码可见字符(不包括回车)。” -----也就说AC自动机的Trie树需要128个单词分支。
#include <cstdio>
#include <cstring>
#include <queue> using namespace std; const int maxw = 210 *500 + 10;
const int sigma_size = 128;
const int maxl = 10000 + 10; struct Trie{
int next[maxw][sigma_size],fail[maxw],end[maxw];
int root,L;
int newnode(){
for(int i=0;i<sigma_size;i++)
next[L][i]=-1;
end[L++]=-1;
return L-1;
}
void init(){
L=0;
root=newnode();
}
void insert(const char *s,int id){
int now=root,len=strlen(s);
for(int i=0;i<len;i++){
if(next[now][s[i]]==-1)
next[now][s[i]]=newnode();
now=next[now][s[i]];
}
end[now]=id;
}
void build(){
queue<int>Q;
fail[root]=root;
for(int i=0;i<sigma_size;i++)
if(next[root][i]==-1)
next[root][i]=root;
else{
fail[next[root][i]]=root;
Q.push(next[root][i]);
}
while(!Q.empty()){
int now=Q.front();
Q.pop();
for(int i=0;i<sigma_size;i++)
if(next[now][i]==-1)
next[now][i]=next[fail[now]][i];
else{
fail[next[now][i]]=next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
bool used[500 + 10];
bool query(const char *buf,int n,int id){
int now=root,len=strlen(buf);
memset(used,false,sizeof(used));
bool flag =false;
for(int i=0;i<len;i++){
now=next[now][buf[i]];
int tmp=now;
while(tmp!=root){
if(end[tmp]!=-1){
used[end[tmp]]=true;
flag=true;
}
tmp=fail[tmp];
}
}
if(!flag) return false;
printf("web %d:",id);
for(int i=1;i<=n;i++)
if(used[i]) printf(" %d",i);
printf("\n");
return true;
}
}; char buf[maxl];
Trie ac; int main()
{
int n,m;
while(~scanf("%d",&n)){
ac.init();
for(int i=1;i<=n;i++){
scanf("%s",buf);
ac.insert(buf,i);
}
ac.build();
int ans=0;
scanf("%d",&m);
for(int i=1;i<=m;i++){
scanf("%s",buf);
if(ac.query(buf,n,i)) ans++;
}
printf("total: %d\n",ans);
}
return 0;
}
hdu 2896 AC自动机模版题的更多相关文章
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- hdu 2222(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 AC自动机模版题
所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...
- HDU 2896 AC自动机 裸题
中文题题意不再赘述 注意字符范围是可见字符,从32开始到95 char c - 32 #include <stdio.h> #include <string.h> #inclu ...
- hdu 3065 AC自动机模版题
题意:输出每个模式串出现的次数,查询的时候呢使用一个数组进行记录就好. 同上题一样的关键点,其他没什么难度了. #include <cstdio> #include <cstring ...
- HDU 2222 AC自动机(模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 2896 AC自动机
// hdu 2896 AC自动机 // // 题目大意: // // 给你n个短串,然后给你q串长字符串,要求每个长字符串中 // 是否出现短串,出现的短串各是什么 // // 解题思路: // / ...
- HDU 2222 Keywords Search(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
随机推荐
- spring中使用@Value设置全局变量默认值
前几天在开发过程中遇到一个使用 spring 的 @Value 给类的全局变量设置默认值不成功的问题,最后通过查资料也是轻松解决,但是发现使用@Value也是有多种多样的方式,今天总算是将开发任务结束 ...
- 【BZOJ 2024】 2024: [SHOI2009] 舞会 (容斥原理+高精度)
2024: [SHOI2009] 舞会 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 368 Solved: 102 Description OIto ...
- 【UOJ 80】 二分图最大权匹配
[分析] 之前打的那种KM会TLE... why??明明说n^3的啊? #include<cstdio> #include<cstdlib> #include<cstri ...
- 韩梦飞沙-屏幕录像专家 win10 含注册机
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 韩梦飞沙-屏幕录像专家 win10 含注册机 百度云盘下载地址:https://pan. ...
- FileZilla提权的过程
话不多说,我们直接操作! 首先我们通过 webshell 找到 FileZilla 的文件目录,找到 FileZilla Server Interface.xml 文件,打开.我们可以看到它的连接地址 ...
- Java获取服务器IP和客户端IP
服务器IP: String addr = InetAddress.getLocalHost().getHostAddress(); 说明:很明显上面是没考虑到服务器有多个iP的情况. 客户顿啊IP: ...
- ExtJs 3 自定义combotree
ExtJs 3 自定义combotree /** * 自定义下拉树,支持初始化值时自动定位树节点. * 还没有考虑性能问题.继承自Ext.form.ComboBox也很浪费. * 代码中的cu.get ...
- One switched-capacitor IC simultaneously inverts, doubles, and halves the input voltage.
- POJ2352【树状数组】
个人NO.1 一开始题意理解有错. 一星星左下边有N颗星星,那它的等级就是N. 一开始理解必须X,Y两个坐标都小于,后来根据样例看了一下只要左下方即可,X,Y坐标都小于等于即可,但不包括星星本身. # ...
- Mac OS上的远程桌面
最近在做Mac上面的开发,经常在win7和Mac两台电脑上操作,两个键盘,两个鼠标,搞得头都大了,所以干脆把Mac机器远程到win7上面,统一来做,方便些..哈哈!说实话,Mac键盘那些按键真还有点特 ...