Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define INPUT ""
  4.  
  5. using namespace std;
  6. typedef long long ll;
  7. typedef long double ld;
  8. typedef unsigned long long ull;
  9. const ll MAXN = 1234567;
  10. const ld EPS = 1e-6;
  11. const ll INF = 1e9;
  12. ll MOD = 1e9 + 13LL;
  13. const ll chars = 28;
  14. ll BASE = 31;
  15.  
  16. void pre();
  17.  
  18. void post();
  19.  
  20. ll n, m;
  21.  
  22. struct node
  23. {
  24. int next[chars];
  25. int go[chars];
  26. int link;
  27. int pred_v;
  28. char pred_c;
  29. bool leaf;
  30. ll ans;
  31. ll myans;
  32.  
  33. node()
  34. {
  35. for(int i=0;i<chars;i++) next[i] = go[i] = -1;
  36. link = -1;
  37. pred_v = -1;
  38. ans = 0;
  39. myans = 0;
  40. pred_c = '\0';
  41. leaf = false;
  42. ans = 0;
  43. }
  44. };
  45.  
  46. vector<node> bor;
  47. vector<node> karas;
  48.  
  49. ll link(vector<node> &mas, ll v);
  50.  
  51. ll go(vector<node> &mas, ll v, char c)
  52. {
  53. if (mas[v].go[c-'a'] != -1)
  54. return mas[v].go[c-'a'];
  55. if (mas[v].next[c-'a'] != -1)
  56. return mas[v].go[c-'a'] = mas[v].next[c-'a'];
  57. if (mas[v].pred_v == v)
  58. return mas[v].go[c-'a'] = 0LL;
  59. return mas[v].go[c-'a'] = go(mas, link(mas, v), c);
  60. }
  61.  
  62. ll link(vector<node> &mas, ll v)
  63. {
  64. if (mas[v].link != -1)
  65. return mas[v].link;
  66. if (mas[mas[v].pred_v].pred_v == mas[v].pred_v)
  67. return mas[v].link = 0LL;
  68. return mas[v].link = go(mas, link(mas, mas[v].pred_v), mas[v].pred_c);
  69. }
  70.  
  71. ll addstring(vector<node> &mas, string &s)
  72. {
  73. ll cur = 0;
  74. for (auto i = s.begin(); i != s.end(); i++)
  75. {
  76. if (mas[cur].next[*i-'a'] == -1)
  77. {
  78. mas[cur].next[*i-'a'] = mas.size();
  79. mas.push_back(node());
  80. mas[mas[cur].next[*i-'a']].pred_v = cur;
  81. mas[mas[cur].next[*i-'a']].pred_c = *i;
  82. cur = mas.size() - 1;
  83. }
  84. else
  85. {
  86. cur = mas[cur].next[*i-'a'];
  87. }
  88. }
  89. mas[cur].leaf = true;
  90. return cur;
  91. }
  92.  
  93. ll dfs1(ll curbor)
  94. {
  95. ll ans=0;
  96. for(int i=0;i<chars;i++)
  97. {
  98. if(bor[curbor].next[i]!=-1)
  99. ans += dfs1(bor[curbor].next[i]);
  100. }
  101. bor[curbor].myans+=ans;
  102. return bor[curbor].myans;
  103. }
  104.  
  105. void dfs(ll curbor, ll curkaras)
  106. {
  107. karas[curkaras].myans += bor[curbor].myans;
  108. for(int i=0;i<chars;i++)
  109. {
  110. if(bor[curbor].next[i]!=-1)
  111. dfs(bor[curbor].next[i], go(karas, curkaras, i+'a'));
  112. }
  113. return;
  114. }
  115. bool used[1000099];
  116.  
  117. void dfs2(ll cur)
  118. {
  119. for(int i=0;i<chars;i++)
  120. {
  121. if(karas[cur].next[i]!=-1)
  122. dfs2(karas[cur].next[i]);
  123. }
  124. if (!used[cur] && karas[cur].myans > 0)
  125. {
  126. used[cur]=true;
  127. ll add=karas[cur].myans;
  128. ll now = link(karas, cur);
  129. while (karas[now].pred_v != now)
  130. {
  131. karas[now].ans += add;
  132. if(!used[now] && karas[now].myans!=0)
  133. {
  134. used[now]=true;
  135. add+=karas[now].myans;
  136. }
  137. now = link(karas, now);
  138. }
  139. }
  140. }
  141.  
  142. int main(int argv, char **argc)
  143. {
  144. pre();
  145. cin.sync_with_stdio(false);
  146. cin >> n;
  147. bor.resize(1000099);
  148. karas.resize(1);
  149. karas.reserve(1000099);
  150. karas[0].pred_v=0;
  151. for (int i = 1; i <= n; i++)
  152. {
  153. ll p, t;
  154. char c;
  155. cin >> p >> c >> t;
  156. bor[p].next[c-'a'] = i;
  157. bor[i].pred_c = c;
  158. bor[i].pred_v = p;
  159. bor[i].leaf = (t == 1);
  160. bor[i].myans = (t==1);
  161. }
  162. cin >> m;
  163. pair<string, ll> *strs = new pair<string, ll>[m + 5];
  164. for (int i = 0; i < m; i++)
  165. {
  166. cin >> strs[i].first;
  167. strs[i].second = addstring(karas, strs[i].first);
  168. }
  169. dfs1(0);
  170. dfs(0, 0);
  171. dfs2(0);
  172. for (int i = 0; i < m; i++)
  173. {
  174. cout << karas[strs[i].second].myans + karas[strs[i].second].ans << endl;
  175. }
  176. post();
  177. return 0;
  178. }
  179.  
  180. void pre()
  181. {
  182. #ifdef DEBUG
  183. freopen("input.txt", "r", stdin);
  184. freopen("output.txt", "w", stdout);
  185. #else
  186. if (strcmp(INPUT, "") != 0)
  187. {
  188. freopen(INPUT ".in", "r", stdin);
  189. freopen(INPUT "out", "w", stdout);
  190. }
  191. #endif
  192. }
  193.  
  194. void post()
  195. {
  196. #ifdef DEBUG
  197. fprintf(stderr, "Execution time: %Lf", (long double) clock() / CLOCKS_PER_SEC);
  198. #endif
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement