QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#746249#5312. Levenshtein DistancechroneZWA 1819ms140828kbC++173.3kb2024-11-14 13:59:102024-11-14 13:59:12

Judging History

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

  • [2024-11-14 13:59:12]
  • 评测
  • 测评结果:WA
  • 用时:1819ms
  • 内存:140828kb
  • [2024-11-14 13:59:10]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
inline void cmin(int &x, int y) {if(x > y) x = y;}
inline void cmax(int &x, int y) {if(x < y) x = y;}

constexpr int N = 1e5 + 5;
int K, n, m;
char a[N], b[N];

constexpr int mod[2] = {998244353, 1004535809};
constexpr int base[2] = {31, 29};
int Pow[2][N];
inline pair<int, int> add(pair<int, int> H, char ch) {
  int x = H.first, y = H.second;
  x = (1ll * x * base[0] + (ch - 'a' + 1)) % mod[0];
  y = (1ll * y * base[1] + (ch - 'a' + 1)) % mod[1];
  return {x, y};
}
struct Hash {
  char *s; int n;
  pair<int, int> w[N];
  inline void build() {
    for(int i = 1; i <= n; i++) {
      w[i] = add(w[i - 1], s[i]);
    }
  }
  inline pair<int, int> getH(int l, int r) {
    int x = w[r].first, y = w[r].second;
    x = (mod[0] + x - 1ll * w[l - 1].first * Pow[0][r - l + 1] % mod[0]) % mod[0];
    y = (mod[1] + y - 1ll * w[l - 1].second * Pow[1][r - l + 1] % mod[1]) % mod[1];
    return {x, y};
  }
} Ha, Hb;

unordered_map<int, int> rec[N];
inline int lcp(int x, int y) {
  // cerr << x << " " << y << "!\n";
  // int t = 0;
  // // cerr << x << " " << y << " ";
  // while(x <= n && y <= m && a[x] == b[y]) 
  //   t++, x++, y++;
  // return t;
  if(x > n || y > m) return 0;
  if(rec[x].find(y) != rec[x].end()) return rec[x][y];
  int l = 1, r = min(n - x + 1, m - y + 1), res = 0;
  while(l <= r) {
    int mid = l + r >> 1;
    if(Ha.getH(x, x + mid - 1) == Hb.getH(y, y + mid - 1)) {
      l = mid + 1;
      res = mid;
    } else {
      r = mid - 1;
    }
  }
  return rec[x][y] = res;
}

constexpr int M = 35;
int f[M][M << 1], pos[N];
int ans[M];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  
  Pow[0][0] = Pow[1][0] = 1;
  for(int i = 1; i < N; i++) {
    for(int o = 0; o < 2; o++) {
      Pow[o][i] = 1ll * Pow[o][i - 1] * base[o] % mod[o];
    }
  }

  cin >> K;
  cin >> a + 1 >> b + 1;
  n = strlen(a + 1), m = strlen(b + 1);
  Ha.s = a, Ha.n = n, Hb.s = b, Hb.n = m;
  Ha.build(), Hb.build();

  for(int L = 1; L <= m; L++) {
    memset(f, -1, sizeof(f));
    f[0][0 + K] = 0;
    for(int i = 0; i <= K; i++) {
      for(int j = -K; j <= K; j++) {
        if(f[i][j + K] != -1) {
          f[i][j + K] += lcp(f[i][j + K] + 1, f[i][j + K] + 1 + j + L - 1);
          cmax(f[i + 1][j + 1 + K], f[i][j + K]);
          if(f[i][j + K] < n) {
            cmax(f[i + 1][j + K], f[i][j + K] + 1);
            cmax(f[i + 1][j - 1 + K], f[i][j + K] + 1);
          }
        }
        // cerr << i << ", " << j << ", " << f[i][j + K] << "\n";
        // cerr << f[i][j + K] << " \n"[j == K];
      }
    }
    for(int j = -K; j <= K; j++) {
      if(n + j + L - 1 > m) continue;
      pos[n + j + L - 1] = K + 1;
      for(int i = 0; i <= K; i++) {
        if(f[i][j + K] >= n) {
          pos[n + j + L - 1] = i;
          break;
        }
      }
    }
    int lo = max(L, n - K + L - 1), hi = min(m, n + K + L - 1);
    // for(int i = lo; i <= hi; i++) cerr << pos[i] << " \n"[i == hi];
    for(int i = hi - 1; i >= lo; i--) cmin(pos[i], pos[i + 1] + 1);
    for(int i = lo + 1; i <= hi; i++) cmin(pos[i], pos[i - 1] + 1);
    for(int i = lo; i <= hi; i++) {
      ans[pos[i]]++;
    }
  }
  for(int i = 0; i <= K; i++) {
    cout << ans[i] << "\n";
  }
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 11680kb

input:

4
aaa
aabbaab

output:

0
5
15
7
1

result:

ok 5 number(s): "0 5 15 7 1"

Test #2:

score: 0
Accepted
time: 1819ms
memory: 140828kb

input:

30
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 31 numbers

Test #3:

score: -100
Wrong Answer
time: 632ms
memory: 42596kb

input:

30
BBAAABAABBBA
ABBABBBBAABAAAABABBBAABAABBAABAAAAAABAABABBAAAABAAAAABABAABAABBABBAAAABBBBAABBABAAABAABAABBBBBABBABABBBBABBBBAAABBABABBBBAAABBBBAAAABABABABABABABAAABBBBBAABAABAABBAABBBAABBBBBBBABAABAABAABABAABAAABAABBBAABABBABBAAABAAAABABABBABBBAAABBBAAAABBBBABABABAABAABBBBBBBBBAABBBABABBBABAABBABBA...

output:

17
662
8230
50302
166666
310121
345469
280387
227200
209178
203013
198561
105117
102147
99771
97730
96058
94730
93633
92720
92060
91525
91174
90861
90614
90447
90315
90226
90145
90092
90058

result:

wrong answer 23rd numbers differ - expected: '91143', found: '91174'