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|≤1000000<|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|≤1000000<|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≤5T≤5
0<N,Q≤1000000<N,Q≤100000
∑Si+Pi≤500000∑Si+Pi≤500000
∑Wi≤500000∑Wi≤500000Output
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 fSample Output
2 1 1 0题意是给n个字符串,然后m次查询,每次查询有给定的前缀和给定的后缀的字符串有多少个 对于每一个字符串构建字典树,但是不同于一般的构建,构建Trie树时,把每一个字符串的第1个字符放在第一个,第n-1个字符放在第二个,第2个字符放在第三个,n-2个字符放在第四个,以此类推,一共放进2n个字符,然后查询时,也把给定的前缀后缀如此处理,对于后缀前缀长度不够的情况用$替代,字典树遇到*则查询所有字母。
AC代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
using namespace std;
const ll inf=0x3f3f3f3f;
const int MAXN=100010;
char s[MAXN<<1],t[MAXN<<1];
int ch[MAXN<<3][30];
int val[MAXN<<3];
int sz;
struct node
{
string s;
int len;
int cnt;
int id;
int res;
} a[MAXN],b[MAXN];
int cmp(node x,node y)
{
return x.len>y.len;
}
int cmp1(node x,node y)
{
return x.id<y.id;
}
void init()
{
sz=1;
memset(ch[0],0,sizeof ch[0]);
memset(val,0,sizeof val);
}
int idx(char c)
{
return c-'a';
}
void add(const char *s)
{
int u=0,n=(int)strlen(s);
for(int i=0; i<n; i++)
{
int c=idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz],0,sizeof ch[sz]);
val[sz]=0;
ch[u][c]=sz++;
}
u=ch[u][c];
val[u]++;
}
}
int query(const char *s,int rt,int n,int cur)
{
int res=0;
if(cur==n)
{
return val[rt];
}
if(s[cur]=='$')
{
for(int i=0; i<26; i++)
{
if(ch[rt][i])
{
res+=query(s, ch[rt][i], n, cur+1);
}
}
}
else
{
int c=idx(s[cur]);
if(ch[rt][c])
{
return query(s,ch[rt][c],n,cur+1);
}
return 0;
}
return res;
}
int T;
int main()
{
scanf("%d",&T);
while(T--)
{
int n,q;
init();
scanf("%d %d",&n,&q);
for(int i=0; i<n; i++)
{
scanf("%s",s);
a[i].s="";
int len=(int)strlen(s);
for(int j=0; j<len; j++)
{
a[i].s+=s[j];
a[i].s+=s[len-j-1];
}
a[i].len=len;
}
sort(a,a+n,cmp);
for(int i=0; i<q; i++)
{
scanf("%s %s",s,t);
int l1=(int)strlen(s);
int l2=(int)strlen(t);
int L=max(l1,l2);
b[i].s="";
int x=0,y=0;
for(int j=0; j<L; j++)
{
if(x<l1)
{
b[i].s+=s[x++];
}
else
{
b[i].s+='$';
}
if(y<l2)
{
b[i].s+=t[l2-y-1];
y++;
}
else
{
b[i].s+='$';
}
}
b[i].len=l1+l2;
b[i].cnt=2*L;
b[i].id=i;
}
sort(b,b+q,cmp);
int temp=0;
for(int i=0; i<q; i++)
{
int l=b[i].len;
while(temp<n&&a[temp].len>=l)
{
add(a[temp].s.c_str());
temp++;
}
b[i].res=query(b[i].s.c_str(),0,b[i].cnt,0);
}
sort(b,b+q,cmp1);
for(int i=0; i<q; i++)
{
printf("%d\n",b[i].res);
}
}
return 0;
}