QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#735891#5312. Levenshtein DistancecaijianhongWA 0ms3860kbC++233.1kb2024-11-11 22:22:122024-11-11 22:22:14

Judging History

你现在查看的是最新测评结果

  • [2024-11-11 22:22:14]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3860kb
  • [2024-11-11 22:22:12]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define endl "\n"
#define debug(...) void(0)
#endif
using LL = long long;
template <class T> T& cmin(T& x, const T& y) { return x = min(x, y); }
template <class T> T& cmax(T& x, const T& y) { return x = max(x, y); }
struct machine {// {{{
  vector<int> sa, rk, st[30];
  machine(const string& a) {
    int n = a.size();
    sa.assign(n, 0);
    rk.assign(a.begin(), a.end());
    vector<int> h(n);
    auto bucketsort = [&](const auto& key) {
      vector<int> buc(max(n, 128));
      reverse(h.begin(), h.end());
      for (int i : h) buc[rk[i]] += 1;
      partial_sum(buc.begin(), buc.end(), buc.begin());
      for (int i : h) sa[--buc[rk[i]]] = i;
      vector<int> tmp(n);
      tmp[sa[0]] = 0;
      for (int i = 1; i < n; i++)
        tmp[sa[i]] = tmp[sa[i - 1]] + (key(sa[i - 1]) != key(sa[i]));
      rk = move(tmp);
    };
    for (int i = 0; i < n; i++) h[i] = i;
    bucketsort([&](int x) { return rk[x]; });
    for (int j = 1; j < n; j <<= 1) {
      h.clear();
      for (int i = n - j; i < n; i++) h.push_back(i);
      for (int i = 0; i < n; i++)
        if (sa[i] >= j) h.push_back(sa[i] - j);
      bucketsort(
          [&](int x) { return make_pair(rk[x], x + j < n ? rk[x + j] : -1); });
    }
    auto &lcp = st[0];
    lcp.assign(n - 1, 0);
    for (int i = 0, h = 0; i < n; i++) {
      if (!rk[i]) continue;
      if (h) h -= 1;
      int j = sa[rk[i] - 1];
      while (max(i, j) + h < n && a[i + h] == a[j + h]) h += 1;
      lcp[rk[i] - 1] = h;
    }
    for (int j = 1; 1 << j <= n; j++) {
      st[j].resize(n - (1 << j));
      for (int i = 0; i + (1 << j) - 1 < n - 1; i++) {
        st[j][i] = min(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
      }
    }
  }
  int operator()(int i, int j) const {
    if (j == (int)sa.size()) return 0;
    int l = min(rk[i], rk[j]), r = max(rk[i], rk[j]) - 1;
    int k = 31 - __builtin_clz(r - l + 1);
    return min(st[k][l], st[k][r - (1 << k) + 1]);
  }
};// }}}
int n, K, m;
int main() {
#ifndef LOCAL
  cin.tie(nullptr)->sync_with_stdio(false);
#endif
  cin >> K;
  string S, T;
  cin >> S >> T;
  n = (int)S.size(), m = (int)T.size();
  machine lcp(S + '#' + T);
//for (int i = 0; i < n; i++) {
//  for (int j = 0; j < m; j++) debug("lcp(%d, %d) = %d\n", i, j, lcp(i, j + n + 1));
//}
  vector<int> fans(K + 1);
  static int _[2010], *f[40];
  int *now = _;
  for (int i = 0; i <= K; i++) f[i] = now + K, now += K << 1 | 1;
  for (int st = 0; st < m; st++) {
    memset(_, ~0x3f, sizeof _);
    f[0][0] = 0;
    for (int p = 0; p <= K; p++) {
      for (int t = -K; t <= K; t++) if (f[p][t] >= 0) {
        int len = lcp(f[p][t], f[p][t] - t + n + 1);
        fans[p] += len;
        f[p][t] += len;
        if (p < K) {
          fans[p] += 1;
          cmax(f[p + 1][t + 1], f[p][t] + 1);
          cmax(f[p + 1][t - 1], f[p][t]);
          cmax(f[p + 1][t], f[p][t] + 1);
        }
      }
    }
  }
  for (int i = 0; i <= K; i++) cout << fans[i] << endl;
  return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3860kb

input:

4
aaa
aabbaab

output:

21
21
63
49
14

result:

wrong answer 1st numbers differ - expected: '0', found: '21'