题目链接

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个询问,每次询问给出两个串 p 和 s 。要求统计所有字符串中前缀为 p 且后缀为 s (不可重叠)的字符串的数量。

首先我们需要考虑的一点就是如何建立字典树,因为要寻找的是所有字符串中前缀为p后缀为q的情况,所以肯定是以前后缀来建立字典树,既然是要以前后缀来建立字典树,那么前后缀应该如何表示呢?

对于一个单词查询前缀的话,肯定是从建立好的字典树中查询有没有这个单词,这一点比较好理解,但是我们应该如何查找后缀呢?

对于后缀,我们知道一个字符串可以对应许多长度不相同的后缀,这样如果我们正序来寻找后缀的话,就需要考虑到后缀的每一种情况,显然是不可取的,那么能不能用前缀的思想来转化后缀呢?

对此我们可以将后缀逆序存进字典树中,字符串在字典树中查找后缀的话,肯定也是将字符串逆着在字典树里面查找。

前缀后缀的问题解决了,还需要考虑的就是一个前缀肯定有一个确定的后缀来与之对应,那么就应该将前缀+后缀(逆序)一块存进字典树里面,但是这样的话会造成我们没有办法区分前缀与后缀的边界,所以存的时候可以在前后缀之间加上一个通配符,这样在查找的时候,如果遇到通配符,就相当于前缀是满足条件的了,然后将字符串逆序寻找后缀。

代码:

#include<stdio.h>
#include<iostream>
#include<malloc.h>
#include<string.h>
using namespace std;
char str[10000009],s1[10000009],s2[10000009];///str表示的是一共的输入的字符串,s1表示前缀,s2表示后缀
int id[10000009];///用来表示一个前后缀最开始在第几个单词中出现的
int len[10000009]; ///len[i]代表第i个字符串的长度。
int ans[10000009]; ///ans[i]是第i个最后输出的满足第i个询问的字符串数量 typedef struct TrieNode//字典树的节点
{
int num;//该节点的查询编号
struct TrieNode *son[27];//26个字母外加上一个通配符,通配符用来分割前后缀
} Trie;
Trie * createNode()//每一个节点都要进行初始化工作
{
Trie *node;
node=(Trie*)malloc(sizeof(Trie));
for(int i=0; i<27; i++)//每一个的孩子节点都是没有值的
node->son[i]=NULL;
node->num=0;//所代表的是第几个前后缀
return node;
}
void insertWord(Trie *root,int index)//建立字典树
{
Trie *p;
p=root;
int i=0;
while(s1[i]!='\0')//当字符串没有王文到末尾的时候
{
int lowerCase=s1[i]-'a';//小写字母对应的数字
if(p->son[lowerCase]==NULL)//这个节点当前没有被建立
{
p->son[lowerCase]=createNode();//那就建立一个新的节点
}
p=p->son[lowerCase];//不管节点是新建立的还是之前就已经存在的,指针都应该指向当前的这个节点
i++;
}
if(p->num==0)//还没有询问到这里
p->num=index;
else
id[index]=p->num;////遇到相同的数据只存最初出现的一个。
} void query(Trie *root,int strBegin,int strEnd)//现在字典树当中寻找
{
Trie *p,*temp;
p=root;
int i=strBegin;
while(p!=NULL&&i<=strEnd)//p!=NULL表示有可能可以找到当前结点,i查找字符串还没有结束
{
int lowerCase=str[i]-'a';//单词转数字
if(p->son[lowerCase]==NULL) return ;//匹配失败
p=p->son[lowerCase];
if(p->son[26]!=NULL)//这里是说找到了通配符,也就意味着前缀是符合情况的了,我们应该去查找有没有满足情况的后缀
{
temp=p->son[26];//temp表示的是通配符,则继续找后缀
for(int j=strEnd; j>i; j--)//既然是查找后缀,当前的字符串肯定就得反着来找
{
lowerCase=str[j]-'a';
if(temp->son[lowerCase]==NULL)//后缀失配
break;//失配
temp=temp->son[lowerCase];
if(temp->num)//找到合适的后缀了
ans[temp->num]++;//这个前后追对应的数量+
}
}
i++;
}
}
void free_Trie(Trie *root)//清空字典树
{
if(root != NULL)
{
for(int i = 0; i < 27; i++)
if(root->son[i]!=NULL)
free_Trie(root->son[i]);
}
free(root);
}
int main()
{
int T,n,q;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&q);
Trie *root;
root=createNode();
int prelen=0;//当前的串的长度
for(int i=0; i<n; i++)
{
scanf("%s",str+prelen);//从str+prelen这个长度开始给str赋值
len[i] = strlen(str+prelen);//存储每个字符串的长度,方便下面操作
prelen += len[i];
}
//printf("%s\n",str);
for(int i=1; i<=q; i++)
{
id[i]=i;
ans[i]=0;
}
for(int i=1; i<=q; i++)
{
scanf("%s",s1);
int len1=strlen(s1);
s1[len1++]='a'+26;//充当通配符
scanf("%s",s2);
int len2=strlen(s2);
for(int j=len2-1; j>=0; j--)
{
s1[len1++]=s2[j];
}
s1[len1]='\0';//字符串结束标志
//printf("%s\n",s1);
insertWord(root,i);//建立字典树
}
prelen=0;
for(int i=0; i<n; i++)
{
query(root,prelen,prelen+len[i]-1);
prelen+=len[i];
}
for(int i=1; i<=q; i++)
{
printf("%d\n",ans[id[i]]);
}
free_Trie(root);
}
return 0;
}

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 6096---String(AC自动机)

    题目链接 Problem Description Bob has a dictionary with N words in it.Now there is a list of words in whi ...

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

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

随机推荐

  1. Java容器深入浅出之PriorityQueue、ArrayDeque和LinkedList

    Queue用于模拟一种FIFO(first in first out)的队列结构.一般来说,典型的队列结构不允许随机访问队列中的元素.队列包含的方法为: 1. 入队 void add(Object o ...

  2. Whitecoin区块链钱包高级功能使用命令

    格式:NO-命令-命令概要-主要对象-参数 <必须参数> [可选参数]-含义-描述-需要未锁定钱包   1 addmultisigaddress 新增多重签名地址 地址 <nrequ ...

  3. 【.Net】c# 让double保留两位小数

    1.Math.Round(0.333333,2);//按照四舍五入的国际标准2.    double dbdata=0.335333;    string str1=String.Format(&qu ...

  4. HDU4641_K-string

    若它的一个子串出现的次数不少于K次,那么这个子串就是一个K-string. 现给出原串,每次可以向该串后面添加一个字符或者询问当前有多少个不同的K-string. 在线添加查询,解法直指SAM. 其实 ...

  5. STM32标准外设库、 HAL库、LL库

    工作以来一直使用ST的STM32系列芯片,ST为开发者提供了非常方便的开发库.到目前为止,有标准外设库(STD库).HAL库.LL库 三种.前两者都是常用的库,后面的LL库是ST最近才添加,目前支持的 ...

  6. Getting logback and slf4j to work in JBoss AS 7

    As usual, it has to do with classloading and that JBoss internally also uses slf4j and logback. As e ...

  7. [BZOJ2733][HNOI2010]永无乡 解题报告 启发式合并,线段树合并

    好久没更新博客了,前段时间一直都在考试,都没时间些,现在终于有点闲了(cai guai)... 写了一道题,[HNOI2012]永无乡,其实是一道板子题,我发现我写了好多板子题...还是太菜了... ...

  8. 洛谷 P3197 [HNOI2008]越狱 解题报告

    P3197 [HNOI2008]越狱 题目描述 监狱有连续编号为\(1-N\)的\(N\)个房间,每个房间关押一个犯人,有\(M\)种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可 ...

  9. 解题:HEOI 2012 采花

    题面 题外话:LYD说他当时看错题了,考场爆零了,然后有了作诗这道题=.= 离线处理询问,按右端点递增排序,然后对于每种花$flw[i]$,我们求一个$pre[flw[i]]$表示这种花上一次出现的位 ...

  10. redis3.2装完后 其它机子访问爆protocol error, got 'n' as reply type byte

    服务器上装了reids3.2版本,配置文件中已将bind的选项注释掉, linux的iptables的redis端口也开放 其它机子的PHP访问redis爆“protocol error, got ' ...