记录编号 411505 评测结果 AAAAAAAAAA
题目名称 情书 最终得分 100
用户昵称 GravatarHzoi_Hugh 是否通过 通过
代码语言 C++ 运行时间 1.395 s
提交时间 2017-06-05 12:28:39 内存使用 1.98 MiB
显示代码纯文本
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<vector>
  5. #define L 1350000
  6. #define MAXN 100000
  7. using namespace std;
  8. char w[L+100];
  9. bool via[101];
  10. int n,ans;
  11. struct Trie
  12. {
  13. vector<int>q;
  14. Trie *ch[26],*fail;
  15. Trie(){q.clear();memset(ch,0,sizeof(ch));fail=NULL;}
  16. void* operator new(size_t size);
  17. }*root,*C,*mempool,*q[MAXN];
  18. inline int read()
  19. {
  20. memset(w,0,sizeof(w));
  21. int l=0;
  22. char ch;
  23. if(cin>>ch){ while(ch!='$'){w[l++]=ch;ch=getchar();} w[l]=ch;return l;}
  24. else return 0;
  25. }
  26. void* Trie :: operator new(size_t size)
  27. {
  28. if(C==mempool)
  29. {
  30. C=new Trie[(1<<15)+10];
  31. mempool=C+(1<<15)+10;
  32. }
  33. return C++;
  34. }
  35. inline void insert(char *s,int x)
  36. {
  37. int len=strlen(s);
  38. Trie *now=root;
  39. for(int i=0;i<len;i++)
  40. {
  41. if(now->ch[s[i]-'a']==NULL)
  42. now->ch[s[i]-'a']=new Trie();
  43. now=now->ch[s[i]-'a'];
  44. }
  45. now->q.push_back(x);
  46. }
  47. inline void build()
  48. {
  49. root->fail=NULL;
  50. q[0]=root;
  51. for(int i=0,j=0;i<=j;i++)
  52. for(int l=0;l<26;l++)
  53. if(q[i]->ch[l])
  54. {
  55. Trie *now=q[i]->fail;
  56. while(now&&!now->ch[l])now=now->fail;
  57. q[i]->ch[l]->fail=now?now->ch[l]:root;
  58. q[++j]=q[i]->ch[l];
  59. }
  60. }
  61. inline void work()
  62. {
  63. memset(via,0,sizeof(via));
  64. Trie *now=root;
  65. for(int i=0;w[i]!='$';i++)
  66. {
  67. int to=w[i]-'a';
  68. //cout<<"("<<now<<")";
  69. while(now&&!now->ch[to])now=now->fail;
  70. now=now?now->ch[to]:root;
  71. //cout<<"("<<now<<")";
  72. for(Trie *nown=now;nown;nown=nown->fail)
  73. if(!nown->q.empty())
  74. {
  75. //cout<<"("<<nown<<")";
  76. int l=nown->q.size();
  77. for(int j=0;j<l;j++)
  78. via[nown->q[j]]=1;
  79. }
  80. }
  81. ans=0;
  82. for(int i=1;i<=n;i++)
  83. if(via[i])ans++;
  84. if(ans==n)
  85. printf("Yes\n");
  86. else
  87. printf("No\n");
  88. }
  89. int main()
  90. {
  91. freopen("lettera.in","r",stdin);
  92. freopen("lettera.out","w",stdout);
  93. root=new Trie();
  94. scanf("%d",&n);
  95. for(int i=1;i<=n;i++)
  96. {
  97. char s[1000];
  98. scanf("%s",s);
  99. insert(s,i);
  100. }
  101. build();
  102. while(read()) work();
  103. return 0;
  104. }