QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#268543#7748. Karshilov's Matching Problem IInimaRE 0ms3388kbC++172.6kb2023-11-28 18:19:482023-11-28 18:19:49

Judging History

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

  • [2024-08-25 20:42:18]
  • hack成功,自动添加数据
  • (/hack/789)
  • [2023-11-28 18:19:49]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3388kb
  • [2023-11-28 18:19:48]
  • 提交

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<i64> 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<i64> 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: 3388kb

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: -100
Runtime Error

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:


result: