답안 #668591

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
668591 2022-12-04T08:14:43 Z Shin Selling RNA Strands (JOI16_selling_rna) C++17
35 / 100
1500 ms 40684 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair

using namespace std;
template <class X, class Y> bool minimize(X &a, Y b) {
    if (a > b) return a = b, true;
    return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
    if (a < b) return a = b, true;
    return false;
}

const char base[] = {'A', 'G', 'C', 'U'};
const int N = 2e6 + 7;
int node[N][4];
int cnt[N];
int ind[26];
int f[N];
string p, q;
pair<int, int> kew[N];

signed main() {
  cin.tie(0)->sync_with_stdio(0);
  auto add = [&](string &s) {
    static int num_node = 0;
    int cur = 0;
    for (int i = 0; i < (int) s.size(); i ++) {
      int c = ind[s[i] - 'A'];
      if (node[cur][c] == 0) {
        node[cur][c] = ++ num_node;
      }
      cur = node[cur][c];
    }
    cnt[cur] ++;
  };

  int res = 0;
  function<void(int, int)> bfs = [&](int u, int sz) {
    int l = 0, r = 0;
    kew[r ++] = mp(u, f[sz]);
    while (l < r) {
      auto [u, n_j] = kew[l ++];
      if (n_j >= (int) q.size() - 1) {
        res += cnt[u];
      }
      for (char c: base) if (node[u][ind[c - 'A']]) {
        int j = n_j;
        while (j > 0 && q[j] != c) {
          j = f[j - 1];
        }
        if (q[j] == c) {
          j ++;
        }
        f[sz + 1] = j;
        kew[r ++] = mp(node[u][ind[c - 'A']], j);
      }
    }
  };

  for (int i = 0; i < 4; i ++) {
    ind[base[i] - 'A'] = i;
  }
  int n, m; cin >> n >> m;
  for (int i = 1; i <= n; i ++) {
    string s; cin >> s;
    add(s);
  }
  while (m --) {
    cin >> p >> q;
    q += "#";
    res = 0;
    int u = 0, found = true, sz = 0;
    for (int i = 1; i < (int) q.size(); i ++) {
      int j = f[sz];
      while (j > 0 && q[j] != q[i]) {
        j = f[j - 1];
      }
      if (q[j] == q[i]) {
        j ++;
      }
      f[++ sz] = j;
    }
    for (int i = 0; i < (int) p.size(); i ++) {
      if (node[u][ind[p[i] - 'A']] == 0) {
        found = false;
        break;
      }
      int j = f[sz];
      while (j > 0 && q[j] != p[i]) {
        j = f[j - 1];
      }
      if (q[j] == p[i]) {
        j ++;
      }
      f[++ sz] = j;
      u = node[u][ind[p[i] - 'A']];
    }
    if (found) {
      bfs(u, sz);
    }
    cout << res << '\n';
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 279 ms 612 KB Output is correct
2 Correct 564 ms 728 KB Output is correct
3 Execution timed out 1592 ms 40684 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 596 KB Output is correct
2 Correct 215 ms 460 KB Output is correct
3 Correct 191 ms 508 KB Output is correct
4 Correct 10 ms 468 KB Output is correct
5 Correct 193 ms 444 KB Output is correct
6 Correct 129 ms 560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 279 ms 612 KB Output is correct
9 Correct 564 ms 728 KB Output is correct
10 Execution timed out 1592 ms 40684 KB Time limit exceeded
11 Halted 0 ms 0 KB -