题目链接

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. nyoj_5:Binary String Matching

    简单字符串匹配 题目链接 #include<iostream> #include<cstring> using namespace std; int fun(char*aa,c ...

  2. UglifyJS-- 对你的js做了什么

    也不是闲着没事去看压缩代码,但今天调试自己代码的时候发现有点意思.因为是自己写的,虽然压缩了,格式化之后还是很好辨认.当然作为min的首要准则不是可读性,而是精简.那么它会尽量的缩短代码,尽量的保持一 ...

  3. iOS项目冗余资源扫描脚本

    iOS项目冗余资源扫描脚本 随着iOS项目的版本不断迭代,app中冗余文件会越来越多,app size也持续增加,是时候需要对app冗余资源进行检测,对app进行瘦身. 使用方法: 1. 运行环境为m ...

  4. 通过前端sdk 做facebook登录

    1.首先通过前端FB.init初始化,其中很重要的参数就是appId,该参数需要在facebook developer平台申请,创建应用,添加相应的ip 地址或者域名. 2.引入facebook 提供 ...

  5. JAVA多线程之Synchronized关键字--对象锁的特点

    一,介绍 本文介绍JAVA多线程中的synchronized关键字作为对象锁的一些知识点. 所谓对象锁,就是就是synchronized 给某个对象 加锁.关于 对象锁 可参考:这篇文章 二,分析 s ...

  6. 5.npm scripts 使用指南

    简单介绍 scripts里面的 "start": "node app" npm run start 相当于 node app { "name" ...

  7. Apache安装编译遇到APR的问题

    http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.27.tar.bz2Apache下载链接 在解压Apache源码包进入目录运行 ...

  8. UITableView移除某行的分割线和让分割线宽度为cell的宽度

    1.移除 UITableView 某一行的分割线 所谓移除,其实就是使其偏移出 cell 的显示范围,看不到即移除. 方法1: 移除系统的分割线,自己定义每行的分割线 self.tableView.s ...

  9. python进阶(6):多态,封装

    昨天说了面向对象三大特性的继承,今天来说多态和封装,很多其他程序员说python不支持多态没有多态,并不是python没有多态,而是python处处皆多态.今天的讲课重点会放在封装. 一.多态 1.多 ...

  10. ubuntu 15.10 安装jdk

    转http://www.bkjia.com/xtzh/881605.html 第一步,下载Linux版JDK 可以通过访问Oracle官网下载,或者直接通过命令行下载. lxh@ubuntu:~$ w ...