记录编号 397917 评测结果 AAAAAAAAAA
题目名称 [Youdao2010] 有道搜索框 最终得分 100
用户昵称 Gravatardevil 是否通过 通过
代码语言 C++ 运行时间 0.092 s
提交时间 2017-04-21 10:49:39 内存使用 0.32 MiB
显示代码纯文本
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <vector>
  6. #include <ctime>
  7. #include <map>
  8. #include <stack>
  9. #include <queue>
  10. #include <cstring>
  11. #include <algorithm>
  12. using namespace std;
  13.  
  14. #define sacnf scanf
  15. #define scnaf scanf
  16. #define scanfi(x) scanf("%d",&x)
  17. #define scanfd(x) scanf("%lf",&x)
  18. #define scanfl(x) scanf("%lld",&x)
  19. #define scanfc(x) scanf("%c",&x)
  20. #define scanfs(x) scanf("%s",x)
  21. #define maxn 55
  22. #define maxm 26
  23. #define inf 1061109567
  24. #define Eps 0.00001
  25. const double PI=acos(-1.0);
  26. #define mod 1000000007
  27. #define MAXNUM 10000
  28. void Swap(int &a,int &b) {int t=a;a=b;b=t;}
  29. int Abs(int x) {return (x<0)?-x:x;}
  30. typedef long long ll;
  31. typedef unsigned int uint;
  32.  
  33. struct node
  34. {
  35. bool End;
  36. node* next[maxm];
  37. } *root;
  38.  
  39. char s[maxn],temp[maxn];
  40.  
  41. node* create()
  42. {
  43. node *temp=(node*)malloc(sizeof(node));
  44. temp->End=false;for(int i=0;i<maxm;i++) temp->next[i]=NULL;
  45. return temp;
  46. }
  47.  
  48. void Insert(node *root,char *s)
  49. {
  50. while(*s!='\0')
  51. {
  52. int t=*s-'a';
  53. if(root->next[t]==NULL)
  54. root->next[t]=create();
  55. root=root->next[t];s++;
  56. }
  57. root->End=true;
  58. }
  59.  
  60. void Search(node *root,int len,int &cnt)
  61. {
  62. if(cnt>=8) return;
  63. if(root->End)
  64. {
  65. if(cnt!=0) printf(" ");
  66. printf("%s",temp);cnt++;
  67. }
  68. for(int i=0;i<26;i++)
  69. if(cnt<8&&root->next[i]!=NULL)
  70. {
  71. temp[len+1]='\0';temp[len]='a'+i;
  72. Search(root->next[i],len+1,cnt);
  73. temp[len]='\0';
  74. }
  75. }
  76.  
  77. bool found(node *root,char *s)
  78. {
  79. int len=strlen(s);
  80. for(int i=0;i<len;i++) temp[i]=s[i];temp[len]='\0';
  81. while(*s!='\0')
  82. {
  83. int t=*s-'a';
  84. if(root->next[t]==NULL) return false;
  85. root=root->next[t];s++;
  86. }
  87. int cnt=0;
  88. if(root->End) {printf("%s",temp);cnt=1;}
  89. for(int i=0;i<26;i++)
  90. if(cnt<8&&root->next[i]!=NULL)
  91. {
  92. temp[len+1]='\0';temp[len]='a'+i;
  93. Search(root->next[i],len+1,cnt);
  94. temp[len]='\0';
  95. }
  96. return true;
  97. }
  98.  
  99. int main()
  100. {
  101. freopen("youdao.in","r",stdin);
  102. freopen("youdao.out","w",stdout);
  103. //clock_t st=clock();
  104. int n;scanf("%d",&n);
  105. root=create();
  106. for(int i=1;i<=n;i++)
  107. {
  108. scanf("%s",s);
  109. Insert(root,s);
  110. }
  111. int q;scanfi(q);
  112. while(q--)
  113. {
  114. scanf("%s",s);
  115. if(found(root,s)) printf("\n");
  116. else printf("%s\n",s);
  117. }
  118. //clock_t ed=clock();
  119. //printf("\n\nTime Used : %.5lf Ms.\n",(double)(ed-st)/CLOCKS_PER_SEC);
  120. return 0;
  121. }