1 #include <cstdio>
2 #include <cstring>
3
4 #ifdef Lightning
5 #define NOTHING() printf ("\033[0m")
6 #define RED() printf ("\033[31m")
7 #define PURPLE() printf ("\033[35m")
8 #define BLUE() printf ("\033[34m")
9 #define GREEN() printf ("\033[32m")
10 #endif
11
12 struct Trie
13 {
14 long long key;
15 int next[27], fail, frmq[23], fd;
16 } trie[1000010];
17
18 int n, m;
19 int sta[1000010], city[2000010], num;
20 int len, prev, size, now;
21 int p1, p2, p3, p4;
22 int que[1000010], head, rear;
23 char str[1000010];
24
25 inline int new_node (int ch, int father)
26 {
27 size++;
28 trie[father].next[ch] = size;
29 trie[size].key = (trie[father].key * 26 + ch) % 1000000007;
30 return size;
31 }
32
33 void bfs ()
34 {
35 int node;
36 for (que[head = rear = 0] = 0; head <= rear; head++)
37 {
38 node = que[head];
39 for (int i = 0; i < 26; i++)
40 {
41 if (node) trie[node].fd = trie[trie[node].fail].fd + 1;
42 else trie[node].fd = 0;
43 if (trie[node].next[i])
44 {
45 if (node) trie[trie[node].next[i]].fail = trie[trie[node].fail].next[i];
46 que[++rear] = trie[node].next[i];
47 }else trie[node].next[i] = trie[trie[node].fail].next[i];
48 }
49 }
50 }
51
52 void frmq_build ()
53 {
54 for (int i = 1; i <= size; i++)
55 trie[i].frmq[0] = trie[i].fail;
56 for (int k = 0; k < 22; k++)
57 for (int i = 1; i <= size; i++)
58 trie[i].frmq[k + 1] = trie[trie[i].frmq[k]].frmq[k];
59 }
60
61 int lca (int e1, int e2)
62 {
63 if (trie[e1].fd > trie[e2].fd)
64 e1 ^= e2, e2 ^= e1, e1 ^= e2;
65 #ifdef Lightning
66 GREEN ();
67 printf ("DEPTH : %d %d\n", trie[e1].fd, trie[e2].fd);
68 NOTHING ();
69 #endif
70 for (int i = 22; i >= 0; i--)
71 if (trie[trie[e2].frmq[i]].fd >= trie[e1].fd)
72 e2 = trie[e2].frmq[i];
73 if (e1 == e2) return e1;
74 #ifdef Lightning
75 if (trie[e1].fd != trie[e2].fd)
76 {
77 RED ();
78 printf ("\nWrong!!!\n");
79 printf ("%d %d\n\n", trie[e1].fd, trie[e2].fd);
80 NOTHING ();
81 }
82 #endif
83 for (int i = 22; i >= 0; i--)
84 if (trie[e1].frmq[i] != trie[e2].frmq[i])
85 e1 = trie[e1].frmq[i], e2 = trie[e2].frmq[i];
86 return trie[e1].frmq[0];
87 }
88
89 int main ()
90 {
91 #ifdef Lightning
92 BLUE ();
93 printf ("How many descriptions?\n - ");
94 NOTHING ();
95 #endif
96 scanf ("%d", &n);
97 size = 0;
98 for (int i = 1; i <= n; i++)
99 {
100 #ifdef Lightning
101 BLUE ();
102 printf ("Description %d - ", i);
103 NOTHING ();
104 #endif
105 scanf ("%s", str);
106 len = strlen (str);
107 prev = 0;
108 sta[i] = num;
109 #ifdef Lightning
110 BLUE ();
111 printf ("Index : %d Start : %d\n", i, sta[i]);
112 NOTHING ();
113 #endif
114 for (int j = 0; j < len; j++)
115 {
116 now = trie[prev].next[str[j] - ‘a‘];
117 if (!now) now = new_node (str[j] - ‘a‘, prev);
118 num++;
119 city[num] = prev = now;
120 }
121 }
122 #ifdef Lightning
123 GREEN ();
124 printf ("Checking...\nThe Size : %d\n", size);
125 printf ("Aho-Corasick Automation Building...\n");
126 #endif
127 bfs ();
128 #ifdef Lightning
129 printf ("Done...\n");
130 printf ("RMQ Building...\n");
131 #endif
132 frmq_build ();
133 #ifdef Lightning
134 BLUE ();
135 printf ("How many couples are there?\n - ");
136 NOTHING ();
137 #endif
138 scanf ("%d", &m);
139 for (int i = 1, node = 0; i <= m; i++)
140 {
141 #ifdef Lightning
142 BLUE ();
143 printf ("Couple %d - ", i);
144 NOTHING ();
145 #endif
146 scanf ("%d %d %d %d", &p1, &p2, &p3, &p4);
147 #ifdef Lightning
148 BLUE ();
149 printf ("Input : %d %d %d %d\n", p1, p2, p3, p4);
150 GREEN ();
151 printf ("Working...\n");
152 NOTHING ();
153 #endif
154 #ifdef Lightning
155 BLUE ();
156 printf ("City 1 : %d City 2 : %d\n", sta[p1] + p2, sta[p3] + p4);
157 NOTHING ();
158 #endif
159 node = lca (city[sta[p1] + p2], city[sta[p3] + p4]);
160 #ifdef Lightning
161 GREEN ();
162 printf ("The LCA : %d ", node);
163 printf ("The Answer : ");
164 NOTHING ();
165 #endif
166 printf ("%lld\n", trie[node].key);
167 }
168 }