题目链接

Problem Description
Bob has a dictionary with N words in it.
Now there is a list of words in which the middle part of the word has continuous letters disappeared. The middle part does not include the first and last character.
We only know the prefix and suffix of each word, and the number of characters missing is uncertain, it could be 0. But the prefix and suffix of each word can not overlap.
For each word in the list, Bob wants to determine which word is in the dictionary by prefix and suffix.
There are probably many answers. You just have to figure out how many words may be the answer.
 
Input
The first line of the input gives the number of test cases T; T test cases follow.
Each test case contains two integer N and Q, The number of words in the dictionary, and the number of words in the list.
Next N line, each line has a string Wi, represents the ith word in the dictionary (0<|Wi|≤100000)
Next Q line, each line has two string Pi , Si, represents the prefix and suffix of the ith word in the list (0<|Pi|,|Si|≤100000,0<|Pi|+|Si|≤100000)
All of the above characters are lowercase letters.
The dictionary does not contain the same words.

Limits
T≤5
0<N,Q≤100000
∑Si+Pi≤500000
∑Wi≤500000

 
Output
For each test case, output Q lines, an integer per line, represents the answer to each word in the list.
 
Sample Input
1
4 4
aba
cde
acdefa
cdef
a a
cd ef
ac a
ce f
 
Sample Output
2
1
1
0
 
 
题意:有 n 个由小写字母构成的串,现在有q次询问,每次给一个前缀和后缀,求这n个串中有多少个串满足给的前缀和后缀,前缀和后缀不能在这个字符串中重叠?
 
思路:对于给的n个串s[i],修改为 s[i] = s[i] + "_" + s[i] ,对于每次询问输入的前缀pre和后缀suf,修改为str=suf+"_"+pre,对这q次询问的前缀后缀构成的串 构建tire树,然后用AC自动机匹配跑一下即可。但是这样会存在冗余,例如:aaa,前缀aa 后缀aa,那么也会匹配上,所以比较时还得判断一下长度。
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
using namespace std;
const int N=1e5+;
string s[N];
struct Node{
Node *son[];
Node *fail;
int flag;
int len;
}tr[*N];
Node *root;
queue<Node*>Q;
int ans[N];
vector<int>v[N];
int tot; void init()
{
tot=;
memset(ans,,sizeof(ans));
root=&tr[];
while(!Q.empty()) Q.pop();
for(int i=;i<N;i++) v[i].clear();
for(int i=;i<*N;i++)
{
tr[i].flag=;
tr[i].fail=NULL;
for(int j=;j<;j++) tr[i].son[j]=NULL;
}
}
void build(string s,int id)
{
Node *now=root;
for(int i=;i<s.length();i++)
{
int x=s[i]-'_';
if(!now->son[x]) now->son[x]=&tr[++tot];
now=now->son[x];
}
if(now->flag) {
v[now->flag].push_back(id);
return ;
}
now->flag=id;
now->len=s.length();
}
void setFail()
{
Q.push(root);
while(!Q.empty())
{
Node *now=Q.front(); Q.pop();
for(int i=;i<;i++)
{
if(now->son[i])
{
Node *p=now->fail;
while(p && (!(p->son[i]))) p=p->fail;
now->son[i]->fail=(p)?p->son[i]:root;
Q.push(now->son[i]);
}
else now->son[i]=(now!=root)?now->fail->son[i]:root;
}
}
}
void query(string s)
{
Node *now=root;
int len=s.length();
for(int i=;i<len;i++)
{
int x=s[i]-'_';
now=now->son[x];
Node *p=now;
while(p!=root)
{
if(p->flag && p->len<=len/+) ans[p->flag]++;
p=p->fail;
}
}
}
int main()
{
int T; cin>>T;
while(T--)
{
init();
int n,q; scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
{
cin>>s[i];
s[i]=s[i]+"_"+s[i];
}
for(int i=;i<=q;i++)
{
string s1,s2; cin>>s1>>s2;
string ss=s2+"_"+s1;
build(ss,i);
}
setFail();
for(int i=;i<=n;i++)
{
query(s[i]);
}
for(int i=;i<=q;i++) ///处理相同的前后缀;
{
for(int j=;j<v[i].size();j++)
ans[v[i][j]]=ans[i];
}
for(int i=;i<=q;i++) printf("%d\n",ans[i]);
}
return ;
}

hdu 6096---String(AC自动机)的更多相关文章

  1. 2017多校第6场 HDU 6096 String AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...

  2. HDU 6096 String (AC自动机)

    题意:给出n个字符串和q个询问,每次询问给出两个串 p 和 s .要求统计所有字符串中前缀为 p 且后缀为 s (不可重叠)的字符串的数量. 析:真是觉得没有思路啊,看了官方题解,真是好复杂. 假设原 ...

  3. ZOJ 3228 Searching the String(AC自动机)

    Searching the String Time Limit: 7 Seconds      Memory Limit: 129872 KB Little jay really hates to d ...

  4. HDU 6096 String(AC自动机+树状数组)

    题意 给定 \(n\) 个单词,\(q\) 个询问,每个询问包含两个串 \(s_1,s_2\),询问有多少个单词以 \(s_1\) 为前缀, \(s_2\) 为后缀,前后缀不能重叠. \(1 \leq ...

  5. hdu 6086 -- Rikka with String(AC自动机 + 状压DP)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

  6. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  7. HDU 2222 (AC自动机模板题)

    题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...

  8. Keywords Search - HDU 2222(AC自动机模板)

    题目大意:输入几个子串,然后输入一个母串,问在母串里面包含几个子串.   分析:刚学习的AC自动机,据说这是个最基础的模板题,所以也是用了最基本的写法来完成的,当然也借鉴了别人的代码思想,确实是个很神 ...

  9. HDU 2296 Ring [AC自动机 DP 打印方案]

    Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...

随机推荐

  1. LVS服务原理以及搭建(理论+干货)

    LVS服务原理以及搭建(理论+干货) 版权声明:本文为yunshuxueyuan原创文章 如需转载请标明出处: https://my.oschina.net/yunshuxueyuan/blog QQ ...

  2. 实施软件测试风险分析&回归用例刷选

    [一两年前收集整理的资料,感觉不错,放到博客上来] 作为软件测试计划的一部分,软件测试风险的分析与控制是其中重要的环节.如果前期风险分析与控制比较充分,那么会使软件的测试成功性大大增加,且可将由风险异 ...

  3. .Net 调用微信公众号扫一扫

    1.绑定域名 去微信公众号平台中设置js接口安全域名,要注意的是不填写http://, 只填写域名即可,如 www.baidu.com. 一个月只能修改三次,要谨慎填写. 2.引入JS文件 在页面中引 ...

  4. BZOJ-1491-社交网络

    题目链接:https://vjudge.net/problem/HYSBZ-1491 题目大意:中文题目,题意见题目 题目分析:由于数据量很小,可以考虑进行Floyd算法. 第一次进行Floyd算法, ...

  5. ThreadLocal源码分析(转)

    阅读总结: ThreadLocal内部使用静态map存储,每个变量对应一个hashcode,不需要指定key值,后台动态生成,good! 每个变量ThreadLocal内部分配Entry,获取值时,通 ...

  6. 一步一步学Vue(九)

    接上篇,这次是真的接上篇,针对上篇未完成的部分,增加鉴权功能,开始之前,我们先要介绍一个新的知识,路由元数据. 在vue-router中,定义元数据的方式: const router = new Vu ...

  7. js常用内置对象及方法

    在js中万物皆对象:字符串,数组,数值,函数...... 内置对象都有自己的属性和方法,访问方法如下: 对象名.属性名称: 对象名.方法名称 1.Array数组对象 unshift( )    数组开 ...

  8. sql必知必会-总结篇

    总结: 1.全书总览:数据查询.新增.删除:表的新增.更新操作:视图.存储过程.事务.索引的描述,高级sql功能:约束.触发器.索引 2.特色:术语简明定义,讲述最简单化.简而全面. 3.长进的地方: ...

  9. [补档][Tvyj 1729]文艺平衡树

    [Tvyj 1729]文艺平衡树 题目 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结 ...

  10. [补档][Tyvj 1518]CPU监控

    [Tyvj 1518]CPU监控 题目 Bob需要一个程序来监视CPU使用率.这是一个很繁琐的过程,为了让问题更加简单,Bob会慢慢列出今天会在用计算机时做什么事. Bob会干很多事,除了跑暴力程序看 ...