迷之好奇

野性酷女 2022-07-12 06:40 202阅读 0赞

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

对于每组数据,输出一个数字代表答案。

Example Input

  1. 3
  2. 12345
  3. 66666
  4. 12356
  5. 3
  6. 45
  7. 12345
  8. 356

Example Output

  1. 1
  2. 0
  3. 1
  4. #include<stdio.h>
  5. #include<string.h>
  6. #define maxn 600000
  7. #define maxc 10
  8. typedef struct node
  9. {
  10. int cnt;
  11. struct node *next[maxc];
  12. }trietree;
  13. int sum;
  14. trietree trie[maxn];
  15. int top=-1;
  16. trietree * create()
  17. {
  18. int i;
  19. trietree * t;
  20. t=&trie[++top];
  21. t->cnt=0;
  22. for(i=0;i<maxc;i++)
  23. {
  24. t->next[i]=NULL;
  25. }
  26. return t;
  27. }
  28. trietree * init()
  29. {
  30. top=-1;
  31. return (create());
  32. }
  33. void insert(trietree * t,char str[],int len)
  34. {
  35. int i,id;
  36. trietree * p=t;
  37. for(i=len-1;i>=0;i--)
  38. {
  39. id=str[i]-'0';
  40. if(!p->next[id])
  41. {
  42. p->next[id]=create();
  43. }
  44. p=p->next[id];
  45. p->cnt++;
  46. }
  47. p->cnt--;
  48. }
  49. int find(trietree * t,char str[],int len)
  50. {
  51. int i,id,sum;
  52. trietree * p=t;
  53. for(i=len-1;i>=0;i--)
  54. {
  55. id=str[i]-'0';
  56. if(!p->next[id])
  57. {
  58. return 0;
  59. }
  60. p=p->next[id];
  61. }
  62. for(i=0;i<maxc;i++)
  63. {
  64. if(p->next[i]!=NULL)
  65. {
  66. break;
  67. }
  68. }
  69. if(i==maxc)
  70. {
  71. return 0;
  72. }
  73. else
  74. {
  75. return p->cnt;
  76. }
  77. }
  78. int main()
  79. {
  80. char str[8];
  81. int n,m,i,len,flag;
  82. trietree * tree;
  83. while(scanf("%d",&n)!=EOF)
  84. {
  85. tree=init();
  86. while(n--)
  87. {
  88. scanf("%s",str);
  89. len=strlen(str);
  90. insert(tree,str,len);
  91. }
  92. scanf("%d",&m);
  93. while(m--)
  94. {
  95. flag=0;
  96. scanf("%s",str);
  97. len=strlen(str);
  98. flag=find(tree,str,len);
  99. if(flag)
  100. {
  101. printf("%d\n",flag);
  102. }
  103. else
  104. {
  105. printf("0\n");
  106. }
  107. }
  108. }
  109. return 0;
  110. }

发表评论

表情:
评论列表 (有 0 条评论,202人围观)

还没有评论,来说两句吧...

相关阅读

    相关 LGTM? 那些缩写

    就像你可能不知道 现充 其实是 现实生活很充实的人生赢家 的缩写一样,我们经常看到 Github 上的码农们在 code review 时,把乱七八糟的缩写写得到处都是

    相关 AI

    AI属于当下热点,但是阿里巴巴人工智能实验室(AI Labs)已经解散这一消息无疑给资本市场泼了一盆冷水。 AI就目前来看,仍然是一个高投入产出低的科研项目。研究人员的研究进

    相关 好奇

    迷之好奇 Time Limit: 2000ms Memory limit: 65536K 有疑问?点这里^\_^ 题目描述 FF得到了一个有n个数字的

    相关

    Problem Description 通过悬崖的yifenfei,又面临着幽谷的考验—— 幽谷周围瘴气弥漫,静的可怕,隐约可见地上堆满了骷髅。由于此处长年不见天日,导

    相关 好奇

    Problem Description FF得到了一个有n个数字的集合。不要问我为什么,有钱,任性。 FF很好奇的想知道,对于数字x,集合中有多少个数字可以在x前面添加

    相关

    Problem Description 通过悬崖的yifenfei,又面临着幽谷的考验—— 幽谷周围瘴气弥漫,静的可怕,隐约可见地上堆满了骷髅。由于此处长年不见天日,导

    相关 蓝桥 积分

    小明开了个网上商店,卖风铃。共有3个品牌:A,B,C。 为了促销,每件商品都会返固定的积分。 小明开业第一天收到了三笔订单: 第一笔:3个A + 7个B + 1个C,

    相关

    迷瘴 Problem Description 通过悬崖的yifenfei,又面临着幽谷的考验—— 幽谷周围瘴气弥漫,静的可怕,隐约可见地上堆满了骷髅。由于此处长年不见