Guest User

Untitled

a guest
Dec 11th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define SIZE 200100
  6. #define ALPHABET 128
  7.  
  8. typedef struct {
  9. int lf, rf, parent, sl, next[ALPHABET];
  10. } node;
  11.  
  12. typedef struct {
  13. int v, pos;
  14. } pointer;
  15.  
  16. char s[SIZE], w[SIZE];
  17. node nodes[SIZE];
  18. pointer ap;
  19. int np, len;
  20.  
  21. void Node(int id, int parent, int lf, int rf) {
  22. nodes[id].parent = parent;
  23. nodes[id].lf = lf;
  24. nodes[id].rf = rf;
  25. nodes[id].sl = -1;
  26. memset(nodes[id].next, -1, ALPHABET * sizeof(int));
  27. }
  28.  
  29. pointer Pointer(int v, int pos) {
  30. pointer res;
  31.  
  32. res.v = v;
  33. res.pos = pos;
  34.  
  35. return res;
  36. }
  37.  
  38. int getNext(int id, char c) {
  39. return nodes[id].next[c];
  40. }
  41.  
  42. void setNext(int id, char c, int v) {
  43. nodes[id].next[c] = v;
  44. }
  45.  
  46. int isRoot(int id) {
  47. return (nodes[id].lf == -1);
  48. }
  49.  
  50. int length(int id) {
  51. return (nodes[id].rf - nodes[id].lf);
  52. }
  53.  
  54. void initTree() {
  55. np = 0;
  56. len = strlen(s);
  57. ap = Pointer(0, 0);
  58.  
  59. int root = np++;
  60. Node(root, 0, -1, -1);
  61. nodes[root].sl = 0;
  62. }
  63.  
  64. int addVertex(int parent, int lf, int rf) {
  65. int id = np++;
  66.  
  67. Node(id, parent, lf, rf);
  68. setNext(parent, s[lf], id);
  69.  
  70. return id;
  71. }
  72.  
  73. int split(pointer ap) {
  74. int down = ap.pos, up = length(ap.v) - down;
  75.  
  76. if(down == 0) {
  77. return ap.v;
  78. }
  79.  
  80. if(up == 0) {
  81. return nodes[ap.v].parent;
  82. }
  83.  
  84. int id = addVertex(nodes[ap.v].parent, nodes[ap.v].lf, nodes[ap.v].lf + up);
  85. nodes[ap.v].lf += up;
  86. nodes[ap.v].parent = id;
  87. setNext(id, s[nodes[ap.v].lf], ap.v);
  88.  
  89. return id;
  90. }
  91.  
  92. pointer down(int v, int lf, int rf) {
  93. if(lf == rf) {
  94. return Pointer(v, 0);
  95. }
  96.  
  97. v = getNext(v, s[lf]);
  98.  
  99. if(length(v) >= (rf - lf)) {
  100. return Pointer(v, length(v) - (rf - lf));
  101. }
  102.  
  103. return down(v, lf + length(v), rf);
  104. }
  105.  
  106. int link(int v) {
  107. if(nodes[v].sl == -1) {
  108. int new_lf = nodes[v].lf;
  109.  
  110. if(isRoot(nodes[v].parent)) {
  111. new_lf += 1;
  112. }
  113.  
  114. nodes[v].sl = split(down(link(nodes[v].parent), new_lf, nodes[v].rf));
  115. }
  116.  
  117. return nodes[v].sl;
  118. }
  119.  
  120. pointer go(pointer ap, char c) {
  121. if(ap.pos == 0) {
  122. int child = getNext(ap.v, c);
  123.  
  124. if(child != -1) {
  125. return Pointer(child, length(child) - 1);
  126. } else {
  127. return Pointer(-1, -1);
  128. }
  129. } else {
  130. if(s[nodes[ap.v].rf - ap.pos] == c) {
  131. return Pointer(ap.v, ap.pos - 1);
  132. } else {
  133. return Pointer(-1, -1);
  134. }
  135. }
  136. }
  137.  
  138. void printTree() {
  139. int i;
  140.  
  141. for(i = 0; i < np; i++) {
  142. printf("%d %d %d %d %d\n", i, nodes[i].lf, nodes[i].rf, nodes[i].parent, nodes[i].sl);
  143. }
  144. }
  145.  
  146. pointer addChar(pointer ap, int i) {
  147. pointer new_ap = go(ap, s[i]);
  148.  
  149. if(new_ap.v != -1) {
  150. return new_ap;
  151. }
  152.  
  153. int id = split(ap);
  154. addVertex(id, i, len);
  155.  
  156. ap = Pointer(link(id), 0);
  157.  
  158. if(isRoot(id)) {
  159. return ap;
  160. } else {
  161. return addChar(ap, i);
  162. }
  163. }
  164.  
  165. void buildTree() {
  166. int i;
  167.  
  168. for(i = 0; i < len; i++) {
  169. ap = addChar(ap, i);
  170. }
  171. }
  172.  
  173. int check(int v, int lf, int rf) {
  174. int p = nodes[v].lf;
  175.  
  176. while(p < nodes[v].rf && lf < rf && s[p] == w[lf]) {
  177. ++p;
  178. ++lf;
  179. }
  180.  
  181. if(lf == rf) {
  182. return 1;
  183. }
  184.  
  185. if(p < nodes[v].rf) {
  186. return 0;
  187. }
  188.  
  189. if(getNext(v, w[lf]) == -1) {
  190. return 0;
  191. }
  192.  
  193. return check(getNext(v, w[lf]), lf, rf);
  194. }
  195.  
  196. int main() {
  197. int tests, words, test, word;
  198.  
  199. scanf("%d", &tests);
  200.  
  201. for(test = 0; test < tests; test++) {
  202. scanf("%s", s);
  203. initTree();
  204. buildTree();
  205.  
  206. scanf("%d", &words);
  207.  
  208. for(word = 0; word < words; word++) {
  209. scanf("%s", w);
  210.  
  211. int status = check(0, 0, strlen(w));
  212.  
  213. if(status) {
  214. printf("y\n");
  215. } else {
  216. printf("n\n");
  217. }
  218. }
  219. }
  220.  
  221.  
  222. return 0;
  223. }
Add Comment
Please, Sign In to add comment