Time Limit: 2000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
FF得到了一个有n个数字的集合。不要问我为什么,有钱,任性。
FF很好奇的想知道,对于数字x,集合中有多少个数字可以在x前面添加任意数字得到。
如,x = 123,则在x前面添加数字可以得到4123,5123等。
输入
多组输入。
对于每组数据
首先输入n(1<= n <= 100000)。
接下来n行。每行一个数字y(1 <= y <= 100000)代表集合中的元素。
接下来一行输入m(1 <= m <= 100000),代表有m次询问。
接下来的m行。
每行一个正整数x(1 <= x <= 100000)。
输出
对于每组数据,输出一个数字代表答案。
示例输入
3 12345 66666 12356 3 45 12345 356
示例输出
1 0 1
提示
来源
zmx
示例程序
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<algorithm>
#define N 500010
using namespace std;
struct node
{
int cont;
int next[10];
} head[N];
int top;
char s[11];
int creat()
{
head[top].cont=0;
memset(head[top].next,-1,sizeof(head[top].next));
return top++;
}
void build(int p)
{
int j,len=strlen(s);
for(int i=len-1; i>=0; i--)
{
j=s[i]-'0';
if(head[p].next[j]==-1)
head[p].next[j]=creat();
head[p].cont++;
p=head[p].next[j];
}
}
int Query(int p)
{
int j,len=strlen(s);
for(int i=len-1; i>=0; i--)
{
j=s[i]-'0';
if(head[p].next[j]==-1)
return 0;
p=head[p].next[j];
}
return head[p].cont;
}
int main()
{
int m,n;
int rt;
while(~scanf("%d",&n))
{
top=0;
rt=creat();
for(int i=0; i<n; i++)
{
scanf("%s",s);
build(rt);
}
scanf("%d",&m);
while(m--)
{
scanf("%s",s);
printf("%d\n",Query(rt));
}
}
return 0;
}