답안 #474033

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
474033 2021-09-16T16:52:44 Z Lain Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
422 ms 132760 KB
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#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:27:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |     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:37:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |     for (int i =0; i < s.size(); i++) {
      |                    ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1232 KB Output is correct
2 Correct 2 ms 1152 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 1 ms 1084 KB Output is correct
7 Correct 1 ms 1232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 21460 KB Output is correct
2 Correct 111 ms 20736 KB Output is correct
3 Correct 111 ms 20804 KB Output is correct
4 Correct 106 ms 20600 KB Output is correct
5 Correct 235 ms 132760 KB Output is correct
6 Correct 247 ms 132692 KB Output is correct
7 Correct 177 ms 35752 KB Output is correct
8 Correct 244 ms 100400 KB Output is correct
9 Correct 243 ms 107172 KB Output is correct
10 Correct 59 ms 5944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 80 ms 5560 KB Output is correct
2 Correct 57 ms 4372 KB Output is correct
3 Correct 75 ms 5344 KB Output is correct
4 Correct 54 ms 3856 KB Output is correct
5 Correct 61 ms 4448 KB Output is correct
6 Correct 78 ms 5216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1232 KB Output is correct
2 Correct 2 ms 1152 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 1 ms 1084 KB Output is correct
7 Correct 1 ms 1232 KB Output is correct
8 Correct 102 ms 21460 KB Output is correct
9 Correct 111 ms 20736 KB Output is correct
10 Correct 111 ms 20804 KB Output is correct
11 Correct 106 ms 20600 KB Output is correct
12 Correct 235 ms 132760 KB Output is correct
13 Correct 247 ms 132692 KB Output is correct
14 Correct 177 ms 35752 KB Output is correct
15 Correct 244 ms 100400 KB Output is correct
16 Correct 243 ms 107172 KB Output is correct
17 Correct 59 ms 5944 KB Output is correct
18 Correct 80 ms 5560 KB Output is correct
19 Correct 57 ms 4372 KB Output is correct
20 Correct 75 ms 5344 KB Output is correct
21 Correct 54 ms 3856 KB Output is correct
22 Correct 61 ms 4448 KB Output is correct
23 Correct 78 ms 5216 KB Output is correct
24 Correct 113 ms 9228 KB Output is correct
25 Correct 165 ms 10388 KB Output is correct
26 Correct 102 ms 12164 KB Output is correct
27 Correct 120 ms 9252 KB Output is correct
28 Correct 422 ms 16888 KB Output is correct
29 Correct 412 ms 14904 KB Output is correct