Problem Description
FF得到了一个有n个数字的集合。不要问我为什么,有钱,任性。
FF很好奇的想知道,对于数字x,集合中有多少个数字可以在x前面添加任意数字得到。
如,x = 123,则在x前面添加数字可以得到4123,5123等。
Input
多组输入。
对于每组数据
首先输入n(1<= n <= 100000)。
接下来n行。每行一个数字y(1 <= y <= 100000)代表集合中的元素。
接下来一行输入m(1 <= m <= 100000),代表有m次询问。
接下来的m行。
每行一个正整数x(1 <= x <= 100000)。
Output
对于每组数据,输出一个数字代表答案。
Sample Input
3
12345
66666
12356
3
45
12345
356
Sample Output
1
0
1
感觉这个题目如果数据再大一些,需要字符串存的话,可以用字典树。把每个串的后缀存起来。
但是这里数据范围较小,在int内,所以直接用的桶排序储存,查找的复杂度为O(m);
#include <bits/stdc++.h>
using namespace std;
int a[100010];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(a,0,sizeof(a));
for(int i=0; i<n; i++)
{
int x;
scanf("%d",&x);
int s=10;
while(x%s!=x)
{
int t=x%s;
if(t>=s/10)
a[t]++;
/*
使用通排序,储存数据的每个后缀,不包括他自己。
*/
s=s*10;
}
}
int m;
scanf("%d",&m);
for(int i=0; i<m; i++)
{
int y;
scanf("%d",&y);
printf("%d\n",a[y]);
}
}
return 0;
}