답안 #474032

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
474032 2021-09-16T16:51:24 Z Lain Selling RNA Strands (JOI16_selling_rna) C++17
60 / 100
1500 ms 55616 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 = 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: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<1024> >&)':
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 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
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 6456 KB Output is correct
2 Correct 90 ms 5736 KB Output is correct
3 Correct 90 ms 5696 KB Output is correct
4 Correct 97 ms 5736 KB Output is correct
5 Correct 150 ms 49456 KB Output is correct
6 Correct 146 ms 48756 KB Output is correct
7 Correct 132 ms 9164 KB Output is correct
8 Correct 198 ms 55616 KB Output is correct
9 Correct 196 ms 52404 KB Output is correct
10 Correct 56 ms 4544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 145 ms 5408 KB Output is correct
2 Correct 138 ms 3284 KB Output is correct
3 Correct 206 ms 4420 KB Output is correct
4 Correct 96 ms 3700 KB Output is correct
5 Correct 126 ms 3296 KB Output is correct
6 Correct 202 ms 4388 KB Output is correct
# 결과 실행 시간 메모리 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 79 ms 6456 KB Output is correct
9 Correct 90 ms 5736 KB Output is correct
10 Correct 90 ms 5696 KB Output is correct
11 Correct 97 ms 5736 KB Output is correct
12 Correct 150 ms 49456 KB Output is correct
13 Correct 146 ms 48756 KB Output is correct
14 Correct 132 ms 9164 KB Output is correct
15 Correct 198 ms 55616 KB Output is correct
16 Correct 196 ms 52404 KB Output is correct
17 Correct 56 ms 4544 KB Output is correct
18 Correct 145 ms 5408 KB Output is correct
19 Correct 138 ms 3284 KB Output is correct
20 Correct 206 ms 4420 KB Output is correct
21 Correct 96 ms 3700 KB Output is correct
22 Correct 126 ms 3296 KB Output is correct
23 Correct 202 ms 4388 KB Output is correct
24 Correct 126 ms 6484 KB Output is correct
25 Correct 185 ms 8828 KB Output is correct
26 Correct 110 ms 5884 KB Output is correct
27 Correct 133 ms 6632 KB Output is correct
28 Execution timed out 1583 ms 15400 KB Time limit exceeded
29 Halted 0 ms 0 KB -