QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#268539#7748. Karshilov's Matching Problem IInimaWA 63ms31464kbC++172.6kb2023-11-28 18:18:032023-11-28 18:18:04

Judging History

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

  • [2024-08-25 20:42:18]
  • hack成功,自动添加数据
  • (/hack/789)
  • [2023-11-28 18:18:04]
  • 评测
  • 测评结果:WA
  • 用时:63ms
  • 内存:31464kb
  • [2023-11-28 18:18:03]
  • 提交

answer

/**
 *    author:  NimaAryan
 *    created: 2023-11-28 12:55:08  
**/
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#endif

using i64 = long long;

vector<int> kmp(const string& s) {
  const int n = (int) s.size();
  vector<int> pi(n);
  for (int i = 1, j = 0; i < n; ++i) {
    while (j > 0 && s[j] != s[i]) {
      j = pi[j - 1];
    }
    j += (int) (s[j] == s[i]);
    pi[i] = j;
  }
  return pi;
}

vector<int> z_function(string a) {
  int n = a.size();
  vector<int> z(n);
  int l = 0, r = 0;
  for (int i = 1; i < n; ++i) {
    if (i < r) {
      z[i] = min(r - i, z[i - l]);
    }
    while (i + z[i] < n && a[z[i]] == a[i + z[i]]) {
      ++z[i];
    }
    if (i + z[i] > r) {
      l = i;
      r = i + z[i];
    }
  }
  return z;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  
  int n, m;
  cin >> n >> m;

  string S;
  cin >> S;
  string T;
  cin >> T;

  vector<int> w(n);
  for (int i = 0; i < n; ++i) {
    cin >> w[i];
  }

  vector<int> l(m), r(m);
  for (int i = 0; i < m; ++i) {
    cin >> l[i] >> r[i];
    --l[i], --r[i];
  }

  auto z = z_function(S + "#" + T);
  vector<int> lcp(n);
  for (int i = 0; i < n; ++i) {
    lcp[i] = z[n + 1 + i];
  }

  vector<vector<int>> del(n + 1);
  for (int i = 0; i < n; ++i) {
    del[i + lcp[i]].push_back(i);
  }

  vector<vector<int>> qry_r(n);
  for (int i = 0; i < m; ++i) {
    qry_r[r[i]].push_back(i);
  }

  vector<int> near(m);
  set<int> alive;
  for (int i = 0; i < n; ++i) {
    alive.insert(i);
    for (int j : del[i]) {
      alive.erase(j);
    }
    for (int x : qry_r[i]) {
      auto it = alive.lower_bound(l[x]);
      near[x] = (it != alive.end() ? *it : r[x] + 1);
    }
  }

  vector<vector<int>> qry_l(n);
  for (int i = 0; i < m; ++i) {
    qry_l[l[i]].push_back(i);
  }

  vector<i64> pref(n + 1);
  for (int i = 0; i < n; ++i) {
    pref[i + 1] = pref[i] + w[i];
  }

  vector<i64> f(n + 1);
  vector<i64> ans(m);
  for (int i = n - 1; i >= 0; --i) {
    f[i] = pref[lcp[i]] + f[i + 1];
    for (int x : qry_l[i]) {
      ans[x] += f[i] - f[near[x]];
    }
  }

  auto pi = kmp(S);

  vector<int> sum(n);
  for (int i = 0; i < n; ++i) {
    sum[i + 1] = w[i] + sum[pi[i]];
  }

  vector<i64> g(n + 1);
  for (int i = 0; i < n; ++i) {
    g[i + 1] = g[i] + sum[i + 1];
  }

  for (int i = 0; i < m; ++i) {
    ans[i] += g[r[i] - near[i] + 1];
  }

  for (int i = 0; i < m; ++i) {
    cout << ans[i] << "\n";
  }

  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3532kb

input:

8 5
abbabaab
aababbab
1 2 4 8 16 32 64 128
1 1
2 3
3 5
4 7
1 8

output:

1
3
3
16
38

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 3488kb

input:

15 4
heheheheehhejie
heheheheheheheh
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9
2 3
4 8
2 6
1 15

output:

3
13
13
174

result:

ok 4 lines

Test #3:

score: -100
Wrong Answer
time: 63ms
memory: 31464kb

input:

150000 150000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

108147037823514
221513513026814
454974735507482
439921126202661
147750675686077
88493386390345
350914805326443
58850354020997
65459362601845
270158033672354
197633774195261
297820232538301
239511187032650
27774082011165
408015099615067
267688358717242
32052048965805
104448476008515
43709853838459
76...

result:

wrong answer 2nd lines differ - expected: '221878585246974', found: '221513513026814'