Submission #474051

# Submission time Handle Problem Language Result Execution time Memory
474051 2021-09-16T17:51:48 Z Lain Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
990 ms 132912 KB
#include "bits/stdc++.h"
using namespace std;

const int S = 60000;
using bs = bitset<S>;

struct Trie {
  struct Node {
    Node* c[4];
    int idx;
    Node() {
      idx = -1;
      for (int i =0; i < 4; i++) c[i] = nullptr;
    }
  };

  Node* head;
  Trie() {
    head = new Node();
  }

  void insert(const string& s, int idx) {
    Node* curr = head;
    for (int i = 0; i < s.size(); i++){
      if (curr->c[s[i]-'A'] == nullptr)
        curr->c[s[i]-'A'] = new Node();
      curr = curr->c[s[i]-'A'];
    }
    curr->idx = idx;
  }

  void search(const string& s, int idx, vector<bs>& bits) {
    Node* curr = head;
    for (int i =0; i < s.size(); i++) {
      if (curr == nullptr) break;
      int j = s[i]-'A';
      if (curr->idx != -1)
        bits[curr->idx][idx] = 1;
      curr = curr->c[j];
    }
    if (curr != nullptr && curr->idx != -1)
      bits[curr->idx][idx] = 1;
  }
};


int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  auto simplify = [&](string& s)->void {
    for (auto& x : s) {
      if (x=='G') x = 'B';
      else if (x=='U') x = 'D';
    }
  };

  int n,m;
  cin >> n >> m;
  vector<string> base(n);
  for (auto& x : base) {
    cin >> x;
    simplify(x);
  }
  vector<string> prefs(m), suffs(m);
  vector<bs> pref_bits, suff_bits;
  map<string,int> pref_need, suff_need;
  Trie pref_trie, suff_trie;
  for (int i =0; i < m; i++) {
    cin >> prefs[i] >> suffs[i];
    reverse(suffs[i].begin(),suffs[i].end());
    simplify(prefs[i]);
    simplify(suffs[i]);
    if (pref_need.count(prefs[i]) == 0) {
      pref_need[prefs[i]] = pref_bits.size();
      pref_bits.emplace_back();
      pref_trie.insert(prefs[i],pref_need[prefs[i]]);
    }
    if (suff_need.count(suffs[i]) == 0) {
      suff_need[suffs[i]] = suff_bits.size();
      suff_bits.emplace_back();
      suff_trie.insert(suffs[i],suff_need[suffs[i]]);
    }
  }
  vector<int> ans(m);
  for (int b = 0; b < n; b += S) {
    for (int i = b; i < min(b+S,n); i++) {
      pref_trie.search(base[i],i-b,pref_bits);
      reverse(base[i].begin(),base[i].end());
      suff_trie.search(base[i],i-b,suff_bits);
    }
    for (int i =0; i < m; i++)
      ans[i] += (pref_bits[pref_need[prefs[i]]]&suff_bits[suff_need[suffs[i]]]).count();
    for (auto& x : pref_bits) x.reset();
    for (auto& x : suff_bits) x.reset();
  }
  for (auto& x : ans) cout << x << '\n';
}
// Idea 1:
// Scan it by splitting it into sqrt(n) parts and then matching

Compilation message

selling_rna.cpp: In member function 'void Trie::insert(const string&, int)':
selling_rna.cpp:24:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     for (int i = 0; i < s.size(); i++){
      |                     ~~^~~~~~~~~~
selling_rna.cpp: In member function 'void Trie::search(const string&, int, std::vector<std::bitset<60000> >&)':
selling_rna.cpp:34:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |     for (int i =0; i < s.size(); i++) {
      |                    ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1232 KB Output is correct
2 Correct 2 ms 1124 KB Output is correct
3 Correct 2 ms 1180 KB Output is correct
4 Correct 2 ms 1084 KB Output is correct
5 Correct 2 ms 1180 KB Output is correct
6 Correct 2 ms 1084 KB Output is correct
7 Correct 2 ms 1232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 21308 KB Output is correct
2 Correct 78 ms 20720 KB Output is correct
3 Correct 80 ms 20720 KB Output is correct
4 Correct 82 ms 20632 KB Output is correct
5 Correct 247 ms 132912 KB Output is correct
6 Correct 260 ms 132736 KB Output is correct
7 Correct 154 ms 35772 KB Output is correct
8 Correct 218 ms 100304 KB Output is correct
9 Correct 218 ms 107132 KB Output is correct
10 Correct 53 ms 6032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 226 ms 5560 KB Output is correct
2 Correct 156 ms 4320 KB Output is correct
3 Correct 193 ms 5344 KB Output is correct
4 Correct 155 ms 3848 KB Output is correct
5 Correct 151 ms 4448 KB Output is correct
6 Correct 194 ms 5240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1232 KB Output is correct
2 Correct 2 ms 1124 KB Output is correct
3 Correct 2 ms 1180 KB Output is correct
4 Correct 2 ms 1084 KB Output is correct
5 Correct 2 ms 1180 KB Output is correct
6 Correct 2 ms 1084 KB Output is correct
7 Correct 2 ms 1232 KB Output is correct
8 Correct 67 ms 21308 KB Output is correct
9 Correct 78 ms 20720 KB Output is correct
10 Correct 80 ms 20720 KB Output is correct
11 Correct 82 ms 20632 KB Output is correct
12 Correct 247 ms 132912 KB Output is correct
13 Correct 260 ms 132736 KB Output is correct
14 Correct 154 ms 35772 KB Output is correct
15 Correct 218 ms 100304 KB Output is correct
16 Correct 218 ms 107132 KB Output is correct
17 Correct 53 ms 6032 KB Output is correct
18 Correct 226 ms 5560 KB Output is correct
19 Correct 156 ms 4320 KB Output is correct
20 Correct 193 ms 5344 KB Output is correct
21 Correct 155 ms 3848 KB Output is correct
22 Correct 151 ms 4448 KB Output is correct
23 Correct 194 ms 5240 KB Output is correct
24 Correct 139 ms 9268 KB Output is correct
25 Correct 291 ms 10320 KB Output is correct
26 Correct 106 ms 12184 KB Output is correct
27 Correct 145 ms 9236 KB Output is correct
28 Correct 990 ms 16924 KB Output is correct
29 Correct 990 ms 11720 KB Output is correct