1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| #include <queue> #include <cstdio> #include <cstring> #include <algorithm> using namespace std;
const int N = 2e5 + 10;
inline int Ri() { register int a = 0, c = getchar(); while (c < '0' || '9' < c) c = getchar(); while ('0' <= c && c <= '9') a = (a << 1) + (a << 3) + c - '0', c = getchar(); return a; }
struct SAM { struct NODE { int nxt[26], lnk, len; int &operator[](const int &c) { return nxt[c]; } } nod[N << 1]; int ncnt, lst, ton[N << 1], sot[N << 1], anc[N << 1][20]; void Init() { ncnt = lst = 1; nod[1] = nod[0], nod[1].lnk = -1; } int NewNode() { int p = ++ncnt; nod[p] = nod[0]; return p; } int Expand(const int &c) { int cur = NewNode(), p = lst; lst = cur; nod[cur].len = nod[p].len + 1; while ((~p) && !nod[p][c]) nod[p][c] = cur, p = nod[p].lnk; if (~p) { int q = nod[p][c]; if (nod[q].len == nod[p].len + 1) nod[cur].lnk = q; else { int nq = NewNode(); nod[nq] = nod[q], nod[nq].len = nod[p].len + 1; nod[cur].lnk = nod[q].lnk = nq; while ((~p) && nod[p][c] == q) nod[p][c] = nq, p = nod[p].lnk; } } else nod[cur].lnk = 1; return cur; } void IniAnc(int len) { memset(ton, 0, sizeof ton); memset(anc, 0, sizeof anc); for (int i = 1; i <= ncnt; i++) ton[nod[i].len]++; for (int i = 1; i <= len; i++) ton[i] += ton[i - 1]; for (int i = 1; i <= ncnt; i++) sot[ton[nod[i].len]--] = i; for (int it = 1; it <= ncnt; it++) { int i = sot[it]; anc[i][0] = nod[i].lnk; for (int j = 1; j < 20; j++) anc[i][j] = anc[anc[i][j - 1]][j - 1]; } } int Search(int u, int len) { for (int i = 19; i >= 0; i--) if (nod[anc[u][i]].len >= len) u = anc[u][i]; return u; } } Sa;
struct GRAPH { int head[N * 5], du[N * 5], nxt[N * 8], to[N * 8], ncnt; void AddEdge(int u, int v) { int p = ++ncnt; nxt[p] = head[v], to[p] = u, head[v] = p; du[u]++; } void Init(int siz) { for (int i = 0; i <= siz; i++) head[i] = du[i] = 0; ncnt = 0; } int operator[](const int &u) { return head[u]; } } Gr;
char str[N]; int nA, nB, n, m, mcnt, ed[N]; int rem[N << 1], rlen[N << 1]; int sotA[N], sotB[N]; long long dp[N * 5];
struct RANG { int le, ri, lo; inline void Read() { le = Ri(), ri = Ri(); lo = Sa.Search(ed[le], ri - le + 1); } inline int Len() { return ri - le + 1; } }; RANG Ra[N], Rb[N];
bool cmpA(const int &a, const int &b) { return Ra[a].Len() > Ra[b].Len(); } bool cmpB(const int &a, const int &b) { return Rb[a].Len() > Rb[b].Len(); } void Match(RANG &A) { int org = A.lo; if (rlen[org] == A.Len()) A.lo = rem[org]; else { mcnt++; rlen[org] = A.Len(); Gr.AddEdge(mcnt, rem[org]); rem[org] = mcnt; A.lo = mcnt; } } queue<int> que; int main() { for (int cas = Ri(); cas; cas--) { scanf("%s", str + 1); n = strlen(str + 1); Sa.Init(); for (int i = n; i >= 1; i--) ed[i] = Sa.Expand(str[i] - 'a'); Sa.IniAnc(n); nA = Ri(); for (int i = 1; i <= nA; i++) Ra[i].Read(), sotA[i] = i; nB = Ri(); for (int i = 1; i <= nB; i++) Rb[i].Read(), sotB[i] = i; for (int i = 1; i <= Sa.ncnt; i++) rem[i] = i, rlen[i] = Sa.nod[i].len; sort(sotA + 1, sotA + nA + 1, cmpA); sort(sotB + 1, sotB + nB + 1, cmpB); int pA = 1, pB = 1, qA = sotA[pA], qB = sotB[pB]; mcnt = Sa.ncnt; while (pA <= nA && pB <= nB) { if (Ra[qA].Len() >= Rb[qB].Len()) Match(Ra[qA]), qA = sotA[++pA]; else Match(Rb[qB]), qB = sotB[++pB]; } while (pA <= nA) Match(Ra[qA]), qA = sotA[++pA]; while (pB <= nB) Match(Rb[qB]), qB = sotB[++pB]; for (int i = 2; i <= Sa.ncnt; i++) Gr.AddEdge(Sa.nod[i].lnk, rem[i]); for (int i = 1; i <= nA; i++) Gr.AddEdge(Ra[i].lo, mcnt + i), dp[Ra[i].lo] = Ra[i].Len(); m = Ri(); for (int i = 1; i <= m; i++) { int A = Ri(), B = Ri(); Gr.AddEdge(mcnt + A, Rb[B].lo); } for (int i = 1; i <= mcnt + nA; i++) if (!Gr.du[i]) que.push(i); int cir = mcnt + nA; long long ans = 0; while (!que.empty()) { cir--; int u = que.front(); que.pop(); ans = max(ans, dp[u]); for (int it = Gr[u]; it; it = Gr.nxt[it]) { int v = Gr.to[it]; if (v <= mcnt) dp[v] = max(dp[v], dp[u]); else dp[v] = max(dp[v], dp[u] + Ra[v - mcnt].Len()); Gr.du[v]--; if (!Gr.du[v]) que.push(v); } } if (cir) printf("-1\n"); else printf("%lld\n", ans); Gr.Init(mcnt + nA); for (int i = 1; i <= mcnt + nA; i++) dp[i] = 0; } return 0; }
|