답안 #1074713

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1074713 2024-08-25T13:12:53 Z nima_aryan Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
106 ms 58448 KB
#include <bits/stdc++.h>
 
using namespace std;
 
vector<char> alpha{'A', 'G', 'C', 'U'};
 
string next(string s) {
  int n = s.size();
  for (int i = n - 1; i >= 0; --i) {
    for (int x = 0; x < 3; ++x) {
      if (s[i] == alpha[x]) {
        s[i] = alpha[x + 1];
        return s;
      }
    }
    s.pop_back();
  }
  return string(n, 'Z');
}
 
constexpr int N = 2E6;
vector<array<int, 4>> trie(N);
vector<int> cnt(N);
int tot = 1;
 
void insert(const string& s) {
  int p = 1;
  for (int i = 0; i < s.size(); ++i) {
    int& q = trie[p][find(alpha.begin(), alpha.end(), s[i]) - alpha.begin()];
    if (!q) {
      q = ++tot;
    }
    ++cnt[p = q];
  }
}
int count(const string& s) {
  int p = 1;
  for (int i = 0; i < s.size(); ++i) {
    int q = trie[p][find(alpha.begin(), alpha.end(), s[i]) - alpha.begin()];
    if (!q) {
      return 0;
    }
    p = q;
  }
  return cnt[p];
}
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
 
  sort(alpha.begin(), alpha.end());
 
  int n, m;
  cin >> n >> m;
 
  vector<string> s(n);
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
  }
 
  sort(s.begin(), s.end());
 
  vector<vector<pair<int, int>>> at(n + 1);
  vector<string> q(m);
  for (int i = 0; i < m; ++i) {
    string p;
    cin >> p >> q[i];
    reverse(q[i].begin(), q[i].end());
    int l = lower_bound(s.begin(), s.end(), p) - s.begin();
    int r = lower_bound(s.begin(), s.end(), next(p)) - s.begin();
    at[l].emplace_back(i, -1);
    at[r].emplace_back(i, +1);
  }
 
  vector<int> ans(m);
  for (int i = 0; i < n; ++i) {
    insert(string(s[i].rbegin(), s[i].rend()));
    for (auto [x, d] : at[i + 1]) {
      ans[x] += d * count(q[x]);
    }
  }
 
  for (int i = 0; i < m; ++i) {
    cout << ans[i] << "\n";
  }
 
  return 0;
}

Compilation message

selling_rna.cpp: In function 'void insert(const string&)':
selling_rna.cpp:28:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |   for (int i = 0; i < s.size(); ++i) {
      |                   ~~^~~~~~~~~~
selling_rna.cpp: In function 'int count(const string&)':
selling_rna.cpp:38:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |   for (int i = 0; i < s.size(); ++i) {
      |                   ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 39512 KB Output is correct
2 Correct 14 ms 39516 KB Output is correct
3 Correct 15 ms 39516 KB Output is correct
4 Correct 18 ms 39516 KB Output is correct
5 Correct 16 ms 39516 KB Output is correct
6 Correct 16 ms 39512 KB Output is correct
7 Correct 16 ms 39516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 41808 KB Output is correct
2 Correct 40 ms 46164 KB Output is correct
3 Correct 52 ms 48036 KB Output is correct
4 Correct 49 ms 48028 KB Output is correct
5 Correct 47 ms 44624 KB Output is correct
6 Correct 46 ms 44632 KB Output is correct
7 Correct 48 ms 48976 KB Output is correct
8 Correct 60 ms 49492 KB Output is correct
9 Correct 51 ms 49496 KB Output is correct
10 Correct 35 ms 46416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 45140 KB Output is correct
2 Correct 39 ms 43252 KB Output is correct
3 Correct 38 ms 44248 KB Output is correct
4 Correct 46 ms 43348 KB Output is correct
5 Correct 36 ms 43092 KB Output is correct
6 Correct 38 ms 44048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 39512 KB Output is correct
2 Correct 14 ms 39516 KB Output is correct
3 Correct 15 ms 39516 KB Output is correct
4 Correct 18 ms 39516 KB Output is correct
5 Correct 16 ms 39516 KB Output is correct
6 Correct 16 ms 39512 KB Output is correct
7 Correct 16 ms 39516 KB Output is correct
8 Correct 34 ms 41808 KB Output is correct
9 Correct 40 ms 46164 KB Output is correct
10 Correct 52 ms 48036 KB Output is correct
11 Correct 49 ms 48028 KB Output is correct
12 Correct 47 ms 44624 KB Output is correct
13 Correct 46 ms 44632 KB Output is correct
14 Correct 48 ms 48976 KB Output is correct
15 Correct 60 ms 49492 KB Output is correct
16 Correct 51 ms 49496 KB Output is correct
17 Correct 35 ms 46416 KB Output is correct
18 Correct 39 ms 45140 KB Output is correct
19 Correct 39 ms 43252 KB Output is correct
20 Correct 38 ms 44248 KB Output is correct
21 Correct 46 ms 43348 KB Output is correct
22 Correct 36 ms 43092 KB Output is correct
23 Correct 38 ms 44048 KB Output is correct
24 Correct 45 ms 47440 KB Output is correct
25 Correct 59 ms 49604 KB Output is correct
26 Correct 47 ms 46792 KB Output is correct
27 Correct 62 ms 49344 KB Output is correct
28 Correct 106 ms 58448 KB Output is correct
29 Correct 85 ms 54096 KB Output is correct