Submission #474030

# Submission time Handle Problem Language Result Execution time Memory
474030 2021-09-16T16:50:40 Z Lain Selling RNA Strands (JOI16_selling_rna) C++17
60 / 100
1500 ms 61500 KB
#include "bits/stdc++.h"
using namespace std;

const int S = 1024;
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<1024> >&)':
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 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 10432 KB Output is correct
2 Correct 54 ms 9772 KB Output is correct
3 Correct 51 ms 9732 KB Output is correct
4 Correct 59 ms 9764 KB Output is correct
5 Correct 130 ms 52012 KB Output is correct
6 Correct 134 ms 51372 KB Output is correct
7 Correct 94 ms 14472 KB Output is correct
8 Correct 161 ms 61500 KB Output is correct
9 Correct 157 ms 58244 KB Output is correct
10 Correct 38 ms 7904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 302 ms 5916 KB Output is correct
2 Correct 189 ms 3656 KB Output is correct
3 Correct 306 ms 4784 KB Output is correct
4 Correct 160 ms 4164 KB Output is correct
5 Correct 175 ms 3652 KB Output is correct
6 Correct 299 ms 4804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 46 ms 10432 KB Output is correct
9 Correct 54 ms 9772 KB Output is correct
10 Correct 51 ms 9732 KB Output is correct
11 Correct 59 ms 9764 KB Output is correct
12 Correct 130 ms 52012 KB Output is correct
13 Correct 134 ms 51372 KB Output is correct
14 Correct 94 ms 14472 KB Output is correct
15 Correct 161 ms 61500 KB Output is correct
16 Correct 157 ms 58244 KB Output is correct
17 Correct 38 ms 7904 KB Output is correct
18 Correct 302 ms 5916 KB Output is correct
19 Correct 189 ms 3656 KB Output is correct
20 Correct 306 ms 4784 KB Output is correct
21 Correct 160 ms 4164 KB Output is correct
22 Correct 175 ms 3652 KB Output is correct
23 Correct 299 ms 4804 KB Output is correct
24 Correct 103 ms 10444 KB Output is correct
25 Correct 181 ms 12968 KB Output is correct
26 Correct 80 ms 9776 KB Output is correct
27 Correct 106 ms 10460 KB Output is correct
28 Execution timed out 1590 ms 19752 KB Time limit exceeded
29 Halted 0 ms 0 KB -