题目链接

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个查询,每个查询包含A、B两个字符串,问在给定的n个字符串中,有多少个字符串满足前缀是A,后缀是B且前缀后缀没有重叠部分

分析:

对查询离线处理,给定的字符串保存下来,而对查询的前缀后缀建立字典树,建树过程如下,假设有ac ef这种查询情况:

先将ef翻转过来使得查询变为ac fe, 之后再加一个特殊字符将前缀于后缀连接起来变为ac#fe,对ac#fe建立字典树,并在这个字符串的结尾处设置一个值,这个值为当前查询前后缀的下标k(即第几个查询),如果查询的前缀后缀都相同的,取第一个出现的就好了。

然后是对给定的字符串查询,就是查询每个字符串对查询的贡献,假设有ac ef的查询,有给定的字符串acdef,在字典树上匹配的时候有先有0->a,发现没有a#,继续,然后0->a->c发现ac#有,那么就开始将acdef的字符串反过来匹配了,变成0->a->c->#->f->e 因为f没有值,到达e的时候发现e这处节点有值k,那么就是ans[k]++。

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = 6e5 + 10;
const int INF = 1e9 + 10;
const int mod = 1e9 + 7;
using namespace std; int val[maxn], sz;
int ch[maxn][27];
char str[maxn];
char s[maxn];
int n,T,q;
int len[maxn], ans[maxn];
int nxt[maxn]; int __insert(int st, int l, int r, int v, int flag)///根节点,左右区间,表示前后缀而且只需要在后缀记录数目,正逆序标记
{
int u = st, t = abs(r - l) + 1;
for(int i = l; t--; i += flag)///循环遍历整个前缀或者后缀
{
int c = s[i] - 'a';
if(!ch[u][c])
{
memset(ch[sz], 0, sizeof ch[sz]);
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
if(v)///表示当前插入的这个是后缀的情况
{
if(val[u] == 0) val[u] = v;
else nxt[v] = val[u];///如果有多个的后缀是以同一个字母结尾的,都取同一个就好了
}
return u;
} void query(int L, int R)///查询一个单词
{
int u = 0;
for(int i = L; i <= R; i++)
{
int c = str[i] - 'a';
if(!ch[u][c]) return ;///压根就没有这个前缀的单词
u = ch[u][c];
if(!ch[u][26]) continue;///找到这个前缀了
int st = ch[u][26];
for(int j = R; j > i; j--)////反过来找后缀
{
int k = str[j] - 'a';
st = ch[st][k];
if(!st) break;
if(val[st]) ans[val[st]]++;
}
}
} int main()
{
scanf("%d", &T);
while(T--)
{
sz = 1;
memset(ch[0], 0, sizeof ch[0]);
val[0] = 0;
scanf("%d %d", &n, &q);
for(int i = 1; i <= q; i++) nxt[i] = i;
int num = 0;
for(int i = 0; i < n; i++)
{
scanf("%s", str + num);
len[i] = strlen(str + num);
num += len[i];
}
for(int i = 1; i <= q; i++)
{
ans[i] = 0;
scanf("%s", s);
int l1 = strlen(s);
s[l1++] = 'a' + 26;
scanf("%s", s + l1);
int l2 = strlen(s + l1);
int node = __insert(0, 0, l1 - 1, 0, 1);
__insert(node, l2 + l1 - 1, l1, i, -1);
}
num = 0;
for(int i = 0; i < n; i++)
{
query(num, num + len[i] - 1);
num += len[i];
}
for(int i = 1; i <= q; i++)
{
printf("%d\n", ans[nxt[i]]);
}
}
return 0;
}

2017ACM暑期多校联合训练 - Team 6 1001 HDU 6096 String (字符串处理 字典树)的更多相关文章

  1. 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)

    题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...

  2. 2017ACM暑期多校联合训练 - Team 5 1001 HDU 6085 Rikka with Candies (模拟)

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

  3. 2017ACM暑期多校联合训练 - Team 2 1001 HDU 6045 Is Derek lying? (模拟)

    题目链接 Problem Description Derek and Alfia are good friends.Derek is Chinese,and Alfia is Austrian.Thi ...

  4. 2017ACM暑期多校联合训练 - Team 2 1003 HDU 6047 Maximum Sequence (线段树)

    题目链接 Problem Description Steph is extremely obsessed with "sequence problems" that are usu ...

  5. 2017ACM暑期多校联合训练 - Team 1 1001 HDU 6033 Add More Zero (数学)

    题目链接 Problem Description There is a youngster known for amateur propositions concerning several math ...

  6. 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)

    题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...

  7. 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)

    题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...

  8. 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)

    题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...

  9. 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)

    题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...

随机推荐

  1. 3dContactPointAnnotationTool开发日志(三一)

      在玩的时候遇到了一个python的问题: Traceback (most recent call last): File ".\convert.py", line 13, in ...

  2. 软工网络15团队作业4——Alpha阶段敏捷冲刺之Scrum 冲刺博客(Day1)

    概述 Scrum 冲刺博客对整个冲刺阶段起到领航作用,应该主要包含三个部分的内容: ① 各个成员在 Alpha 阶段认领的任务 ② 明日各个成员的任务安排 ③ 整个项目预期的任务量(使用整数表示,与项 ...

  3. PHP内置标准类

    PHP内置标准类 php语言内部,有“很多现成的类”,其中有一个,被称为“内置标准类”. 这个类“内部”可以认为什么都没有,类似这样: class  stdclass{ } 其作用,可以用于存储一些临 ...

  4. 【UNIX环境编程、操作系统】孤儿进程和僵尸进程

    基本概念: 在类UNIX系统中,僵尸进程是指完成执行(通过exit系统调用,或运行时发生致命错误或收到终止信号所致)但在操作系统的进程表中仍然有一个进程表表项(进程控制块PCB),处于"终止 ...

  5. 第207天:HTTP协议头字段详解大全

    本篇重点介绍一下HTTP常用的Header HTTP Header非常之多,很少有人能完全分清这些Header到底是干什么的.鉴于RFC文件规范艰深晦涩难懂,本文对协议规范中列出的HTTP Heade ...

  6. Codeforces 627D Preorder Test(二分+树形DP)

    题意:给出一棵无根树,每个节点有一个权值,现在要让dfs序的前k个结点的最小值最大,求出这个值. 考虑二分答案,把>=答案的点标记为1,<答案的点标记为0,现在的任务时使得dfs序的前k个 ...

  7. 【明哥报错簿】之【inside the host appBase has been specified, and will be ignored】和【did not find a matching property.】

    tomcat启动时有时候会报一些警告,项目有时候也是可以正常运行.但是警告出现还是要找到原因消灭掉,两个典型的警告解决办法如下: 1.[inside the host appBase has been ...

  8. http2.0可行性研究

     一.http2比http1有了更多新特性 1.使用了多路复用的技术,并发量支持比http1大几个数量级: 2.二进制分帧,改善网络延迟情况,提高传输速率: 3.支持header的数据压缩,数据体积变 ...

  9. [TJOI2008]彩灯 线性基

    题面 题面 题解 题意:给定n个01串,求互相异或能凑出多少不同的01串. 线性基的基础应用. 对于线性基中的01串,如果我们取其中一些凑成一个新的01串,有一个重要的性质:任意2个不同方案凑出的01 ...

  10. CF954F Runner's Problem(动态规划,矩阵快速幂)

    CF954F Runner's Problem(动态规划,矩阵快速幂) 题面 CodeForces 翻译: 有一个\(3\times M\)的田野 一开始你在\((1,2)\)位置 如果你在\((i, ...