QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#390607#2343. First of Her Nameneal2022AC ✓506ms281192kbC++232.6kb2024-04-15 17:48:492024-04-15 17:48:50

Judging History

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

  • [2024-04-15 17:48:50]
  • 评测
  • 测评结果:AC
  • 用时:506ms
  • 内存:281192kb
  • [2024-04-15 17:48:49]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using ll = long long;

constexpr int SZ = 26;

struct GSAM {
  vector<vector<int>> e = {vector<int>(SZ)};  // the labeled edges from node i
  vector<int> parent = {-1};                  // the parent of i
  vector<int> length = {0};                   // the length of the longest string

  GSAM(int n) { e.reserve(2 * n), parent.reserve(2 * n), length.reserve(2 * n); };
  int extend(int c, int p) {  // character, last
    bool f = true;            // if already exist
    int r = 0;                // potential new node
    if (!e[p][c]) {           // only extend when not exist
      f = false;
      e.push_back(vector<int>(SZ));
      parent.push_back(0);
      length.push_back(length[p] + 1);
      r = (int)e.size() - 1;
      for (; ~p && !e[p][c]; p = parent[p]) e[p][c] = r;  // update parents
    }
    if (f || ~p) {
      int q = e[p][c];
      if (length[q] == length[p] + 1) {
        if (f) return q;
        parent[r] = q;
      } else {
        e.push_back(e[q]);
        parent.push_back(parent[q]);
        length.push_back(length[p] + 1);
        int qq = parent[q] = (int)e.size() - 1;
        for (; ~p && e[p][c] == q; p = parent[p]) e[p][c] = qq;
        if (f) return qq;
        parent[r] = qq;
      }
    }
    return r;
  }
};

int main() {
  std::cin.tie(nullptr)->sync_with_stdio(false);
  int n, k;
  std::cin >> n >> k;

  std::vector<int> cnt, id(n);
  auto gsam = GSAM(n);
  for (int i = 0, parent; i < n; i++) {
    char c;
    std::cin >> c >> parent, parent--;
    id[i] = gsam.extend(c - 'A', (parent == -1 ? 0 : id[parent]));
    if (cnt.size() < gsam.e.size()) {
      cnt.resize(gsam.e.size());
    }
    cnt[id[i]]++;
  }

  ll sz = gsam.e.size();
  vector<int> c(sz + 1);
  vector<int> order(sz);
  for (int i = 1; i < sz; i++) c[gsam.length[i]]++;
  for (int i = 1; i < sz; i++) c[i] += c[i - 1];
  for (int i = 1; i < sz; i++) order[c[gsam.length[i]]--] = i;
  reverse(order.begin(), order.end());  // reverse so that large len to small

  for (auto i : order) {
    if (gsam.parent[i] != -1) {
      cnt[gsam.parent[i]] += cnt[i];
    }
  }

  while (k--) {
    std::string s;
    std::cin >> s;
    reverse(s.begin(), s.end());
    int m = int(s.size()), p = 0;
    [&] {
      for (int i = 0; i < m; i++) {
        int c = s[i] - 'A';
        if (gsam.e[p][c] == 0) {
          std::cout << "0\n";
          return;
        }
        p = gsam.e[p][c];
      }
      std::cout << cnt[p] << "\n";
    }();
  }
}

详细

Test #1:

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

Test #2:

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

Test #3:

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

Test #4:

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

Test #5:

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

Test #6:

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

Test #7:

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

Test #8:

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

Test #9:

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

Test #10:

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

Test #11:

score: 0
Accepted
time: 131ms
memory: 165256kb

Test #12:

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

Test #13:

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

Test #14:

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

Test #15:

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

Test #16:

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

Test #17:

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

Test #18:

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

Test #19:

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

Test #20:

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

Test #21:

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

Test #22:

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

Test #23:

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

Test #24:

score: 0
Accepted
time: 2ms
memory: 4932kb

Test #25:

score: 0
Accepted
time: 3ms
memory: 6044kb

Test #26:

score: 0
Accepted
time: 3ms
memory: 6264kb

Test #27:

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

Test #28:

score: 0
Accepted
time: 2ms
memory: 5272kb

Test #29:

score: 0
Accepted
time: 2ms
memory: 5572kb

Test #30:

score: 0
Accepted
time: 2ms
memory: 5360kb

Test #31:

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

Test #32:

score: 0
Accepted
time: 7ms
memory: 10620kb

Test #33:

score: 0
Accepted
time: 6ms
memory: 11968kb

Test #34:

score: 0
Accepted
time: 107ms
memory: 162996kb

Test #35:

score: 0
Accepted
time: 506ms
memory: 281192kb

Test #36:

score: 0
Accepted
time: 495ms
memory: 271428kb

Test #37:

score: 0
Accepted
time: 136ms
memory: 215104kb

Test #38:

score: 0
Accepted
time: 192ms
memory: 216784kb

Test #39:

score: 0
Accepted
time: 289ms
memory: 172420kb

Test #40:

score: 0
Accepted
time: 286ms
memory: 173972kb

Test #41:

score: 0
Accepted
time: 184ms
memory: 183416kb

Test #42:

score: 0
Accepted
time: 197ms
memory: 244068kb

Test #43:

score: 0
Accepted
time: 130ms
memory: 242104kb

Test #44:

score: 0
Accepted
time: 184ms
memory: 161900kb

Test #45:

score: 0
Accepted
time: 413ms
memory: 215800kb

Test #46:

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

Test #47:

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

Test #48:

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

Test #49:

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

Test #50:

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

Test #51:

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

Test #52:

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

Test #53:

score: 0
Accepted
time: 1ms
memory: 4020kb

Test #54:

score: 0
Accepted
time: 2ms
memory: 5072kb

Test #55:

score: 0
Accepted
time: 1ms
memory: 3900kb

Test #56:

score: 0
Accepted
time: 6ms
memory: 8124kb