fork download
  1. #include <bits/stdc++.h>
  2.  
  3.  
  4. #define gc getchar
  5. #define pc putchar
  6.  
  7. using namespace std;
  8.  
  9. /*#include <ext/pb_ds/tree_policy.hpp>
  10. #include <ext/pb_ds/detail/standard_policies.hpp>
  11. #include <ext/pb_ds/assoc_container.hpp>
  12.  
  13. using namespace __gnu_pbds;
  14. */
  15.  
  16. /*
  17.   two functions for policy based data structure. it is
  18.  
  19.   find_by_order() and order_of_key().
  20.  
  21.   The first returns an iterator to the k-th largest element (counting from zero),
  22.   the second returns the number of items in a set that are strictly smaller than our item
  23.  
  24. */
  25.  
  26. #define vi vector<int>
  27. #define si set<int>
  28. #define vs vector<string>
  29. #define pii pair<int,int>
  30. #define vpi vector<pii>
  31. #define pri priority_queue<int>
  32. #define rev_pri priority_queue<int,vector<int>,greater<int> >
  33. #define mpi map<int,int>
  34. #define i64 long long int
  35. #define endl '\n'
  36. #define pi acos(-1)
  37. #define all(v) v.begin(),v.end()
  38. #define pb push_back
  39. #define mp make_pair
  40. #define mod 1000000007
  41. #define For(i,n) for(int i=0;i<n;i++)
  42. #define Rep(i,x,y) for(int i=x;i<=y;i++)
  43. #define eps 1e-8
  44. #define ff first
  45. #define ss second
  46. #define mem(a,b) memset(a,b,sizeof(a))
  47. #define min3(a,b,c) min(a,min(b,c))
  48. #define max3(a,b,c) max(a,max(b,c))
  49. #define READ freopen("input.txt", "r", stdin)
  50. #define WRITE freopen("output.txt","w", stdout)
  51. #define sz size()
  52. #define dbg(x) printf("yo is %d!\n",x)
  53. #define dbg2(x,y) printf("yo is %d! and %d!\n",x,y)
  54. #define foreach(i,c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
  55. #define sqr(a) (a) * (a)
  56. #define clr clear()
  57. #define CASE(a) printf("Case %d:\n",a)
  58. #define sf(n) scanf("%d", &n)
  59. #define sff(a,b) scanf("%d %d", &a, &b)
  60. #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
  61.  
  62. //int dx[] = {0,1,0,-1};
  63. //int dy[] = {1,0,-1,0};
  64. //int dx[] = { -1, -1, 0, 1, 1, 1, 0, -1 };
  65. //int dy[] = { 0, -1, -1, -1, 0, 1, 1, 1 };
  66. //int dxK[] = { -2, -2, -1, 1, 2, 2, 1, -1 };
  67. //int dyK[] = { -1, 1, 2, 2, 1, -1, -2, -2 };
  68.  
  69. //functions
  70.  
  71. //i64 gcd(i64 a,i64 b){if(!b)return a;return gcd(b,a%b);}
  72.  
  73. //inline void fastRead(i64 *a){register char c=0;while(c<33)c=gc();*a=0;while(c>33){*a=*a*10+c-'0';c=gc();}}
  74.  
  75. //inline void fastWrite(int a){char snum[20];int i=0;do{snum[i++]=a%10+48;a=a/10;}while(a!=0);i=i-1;while(i>=0)pc(snum[i--]);pc('\n');}
  76.  
  77. //i64 bigmod(i64 num,i64 n){if(!n)return 1;i64 x=(bigmod(num,n/2)*bigmod(num,n/2))%mod;if(n%2)x=(x*num)%mod;return x;}
  78.  
  79. //i64 modinverse(i64 num){return bigmod(num,mod-2);}
  80.  
  81. //void combination(int pos,int last){if(pos==k+1){for(int i=1;i<=k;i++)cout << tem[i];cout << endl;return;}
  82. //for(int i=last+1;i<=n-k+pos;i++){tem[pos] = num[i-1];combination(pos+1,i);}}
  83. //i64 power(i64 value, i64 base){i64 result = 1;For(i,base)result *= value;return result;}
  84. //int Set(int N,int pos){return N = (1<<pos);}
  85. //int reset(int N,int pos){return N &= (~(1<<pos));}
  86. //bool check(int N,int pos){return (bool)(N & (1<<pos));}
  87.  
  88. //typedef tree< int, null_type, less < int >, rb_tree_tag, tree_order_statistics_node_update > Set;
  89.  
  90. const int maxnode = 1000005;
  91. const int letters=26;
  92.  
  93. string str,key;
  94. vector<pair<string, int> > v;
  95. int id;
  96.  
  97. struct Trie
  98. {
  99. int ch[maxnode][letters];
  100. int val[maxnode];
  101. int f[maxnode];
  102. int ss;
  103. void init()
  104. {
  105. ss=1;
  106. mem(ch,0);
  107. mem(val,25);
  108. mem(f,0);
  109. }
  110.  
  111. void insert(string s)
  112. {
  113. int u=0;
  114. For(i,s.sz)
  115. {
  116. int c=s[i]-'a';
  117. if(!ch[u][c])
  118. ch[u][c]=ss++;;
  119. u=ch[u][c];
  120. }
  121. val[u] = min(val[u],id);
  122. }
  123. void build_fail()
  124. {
  125. queue<int>q;
  126. int i;
  127. For(i,26)
  128. {
  129. if(ch[0][i])
  130. q.push(ch[0][i]);
  131. }
  132. int r,u,v;
  133. while(!q.empty())
  134. {
  135. r=q.front();
  136. q.pop();
  137. For(c,26)
  138. {
  139. u=ch[r][c];
  140. if(!u)
  141. continue;
  142. q.push(u);
  143. v=f[r];
  144.  
  145. while(v&&ch[v][c]==0)
  146. v=f[v];
  147. f[u]=ch[v][c];
  148.  
  149. }
  150. }
  151. }
  152. bool search(string s)
  153. {
  154. int j=0;
  155. For(i,s.sz)
  156. {
  157. int c=s[i]-'a';
  158. while(j&&ch[j][c]==0)
  159. j=f[j];
  160. j=ch[j][c];
  161. int temp=j;
  162. while(temp)
  163. {
  164. if(val[temp]<=id)
  165. return true;
  166. temp=f[temp];
  167. }
  168. }
  169. return false;
  170. }
  171. }ac;
  172.  
  173. int main()
  174. {
  175. int q;
  176. cin >> q;
  177. ac.init();
  178.  
  179. Rep(i,1,q)
  180. {
  181. int n;
  182. cin >> n;
  183.  
  184. cin >> str;
  185.  
  186. if(n==0)
  187. {
  188. id = i;
  189. ac.insert(str);
  190. }
  191. else
  192. {
  193. v.pb(mp(str,i));
  194. }
  195. }
  196. ac.build_fail();
  197. For(i,v.sz)
  198. {
  199. id = v[i].ss;
  200. str = v[i].ff;
  201. if(ac.search(str))
  202. puts("YES");
  203. else
  204. puts("NO");
  205. }
  206. }
  207.  
  208.  
Success #stdin #stdout 0.02s 125440KB
stdin
12
0 cat
1 dogville
0 dog
1 dogville
1 gooutwithcat
1 gooutwithcrocodile
1 fancyconcatenation
0 crocodile
1 lacoste
1 gooutwithcrocodile
1 catalanreferendum
1 rocodile
stdout
NO
YES
YES
NO
YES
NO
YES
YES
NO