ICPC North Central NA Contest 2018 Pokegene
思路
题意不就不说了,刚开始用trie妥妥超时(后来看标程, 发现是trie + dp)
我然后又看到长度小于2e5,想后 缀数组的rmq(主要是学rmq部分) , 结果死在了求heigh数组, 毕竟heigh,你往后延伸不能从一个字符串跨到另一个字符串
折磨了半天, 突然想到heigh是求每个位置,(后缀数组的主要应用就在heigh数组的应用)为何不直接 暴力求 f[i][0] 呢,反正复杂度够
所以正解就出来了, 先暴力,在rmq
代码
#include <bits/stdc++.h> #define all(n) (n).begin(), (n).end() #define se second #define fi first #define pb push_back #define mp make_pair #define rep(i,a,b) for(int i=a;i<=b;++i) #define per(i,a,b) for(int i=a;i>=b;--i) using namespace std; typedef long long ll; typedef pair<int, int> PII; typedef vector<int> VI; typedef double db; const int maxn = 2e5 + 5; int n, _, k, l, m; int f[maxn][30], a[maxn]; PII rk[maxn]; string s[maxn]; void rmq_init() { memset(f, 0x3f, sizeof f); rep (i, 1, n - 1) { int len = 0; for (; len < s[rk[i].fi].size() && len < s[rk[i + 1].fi].size(); ++len) if (s[rk[i].fi][len] != s[rk[i + 1].fi][len]) break; f[i][0] = len; } for (int j = 1, mj = 2; mj <= n; ++j, mj <<= 1) for (int i = 1; i + mj <= n; ++i) f[i][j] = min(f[i][j - 1], f[i + (mj >> 1)][j - 1]); } int rmq_query(int l, int r) { l = rk[l].se, r = rk[r].se; if (l > r) swap(l, r); int k = r - l < 2 ? 0 : ceil(log2(r - l)) - 1; return min(f[l][k], f[r - (1 << k)][k]); } bool cmp(PII a, PII b) { return s[a.first] < s[b.first]; } bool cmp2(int a, int b) { return rk[a].se < rk[b].se; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; rep (i, 1, n) cin >> s[i], rk[i].fi = i; sort(rk + 1, rk + 1 + n, cmp); rep (i, 1, n) rk[rk[i].fi].se = i; rmq_init(); rep (_, 1, m) { cin >> k >> l; ll ans = 0; rep (i, 1, k) cin >> a[i]; sort(a + 1, a + 1 + k, cmp2); rep (i, l, k) { int x = 0, y = 0, z = 0; if (l == 1) x = s[a[i]].size(); else x = rmq_query(a[i], a[i - l + 1]); if (i > l) z = rmq_query(a[i], a[i - l]); if (i < k) y = rmq_query(a[i + 1], a[i - l + 1]); if (x > y && x > z) ans += min(x - y, x - z); } cout << ans << '\n'; } return 0; }