#include<bits/stdc++.h> usingnamespace std; using ll = longlong;
constexprint ALPHABET = 27;
structNode { int icnt{}; array<Node*, ALPHABET> next{}; };
signedmain(){ int N, Q; cin >> N >> Q; Node* root = newNode(); array<array<ll, ALPHABET>, ALPHABET> cnt{}; while (N--) { string s; cin >> s; for (auto& c : s) c = c - 'a' + 1; s = s + char(0); // 为方便和空串比较,令空为比 a 小的字符 Node* p = root; p->icnt++; for (auto& c : s) { auto& q = p->next[c]; if (!q) q = newNode(); for (int i = 0; i < ALPHABET; i++) { if (i == c) continue; if (p->next[i]) { cnt[c][i] += p->next[i]->icnt; // 边插入边处理 } } p = q; p->icnt++; } } while (Q--) { string s; cin >> s; for (auto& c : s) c = c - 'a' + 1; s = char(0) + s; ll res = 0; for (int l = 0; l < ALPHABET; l++) { for (int r = l + 1; r < ALPHABET; r++) { res += cnt[s[l]][s[r]]; } } cout << res << endl; } return0; }
// 先写个池子 structnode { int icnt{}; array<Node, 2> next{}; }; voidsolve(){ int N, Q; cin >> N >> Q; vector<Node> pre(N + 1); pre[0] = newNode(); for (int i = 0; i < N; i++) { uint x; cin >> x; Node rt = newNode(); auto p = rt; p->icnt++; for (int bit = B; bit >= 0; bit--) { bool c = x >> bit & 1; if (!p->next[c]) p->next[c] = newNode(); p = p->next[c]; p->icnt++; } auto merge = [&](auto&& merge, Node x, Node y) { if (!x) return y; if (!y) return x; x->icnt += y->icnt; for (int i = 0; i < 2; i++) { x->next[i] = merge(merge, x->next[i], y->next[i]); } return x; }; pre[i + 1] = merge(merge, rt, pre[i]); } while (Q--) { int l, r, x; cin >> l >> r >> x; l--; auto pl = pre[l], pr = pre[r]; uint max = 0; for (int bit = B; bit >= 0; bit--) { bool c = x >> bit & 1 ^ 1; uint sum = 0; if (pl && pl->next[c]) sum -= pl->next[c]->icnt; if (pr && pr->next[c]) sum += pr->next[c]->icnt; if (sum > 0) { max |= 1u << bit; if (pl) pl = pl->next[c]; if (pr) pr = pr->next[c]; } else { if (pl) pl = pl->next[!c]; if (pr) pr = pr->next[!c]; } } cout << max << endl; } }