Submission #972929

# Submission time Handle Problem Language Result Execution time Memory
972929 2024-05-01T10:27:49 Z duckindog Selling RNA Strands (JOI16_selling_rna) C++17
35 / 100
1500 ms 203360 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 500'000 + 10;
int n, m;
string str[N];
pair<string, string> Q[N];
 
int convert(const auto& a) { return a == 'A' ? 0 : a == 'C' ? 1 : a == 'G' ? 2 : 3; }
int nwt[N << 2][4];

int answer[N];
struct Trie {
  int it;
  vector<array<int, 4>> nwt;
  vector<int> id;
 
  void add(string s, int idx) {
    int g = 0;
    for (int i = 1; i <= (int)s.size(); ++i) {
      while (nwt.size() <= g) nwt.push_back(array<int, 4>());
 
      int path = convert(s[i - 1]);
      if (!nwt[g][path]) nwt[g][path] = ++it;
      g = nwt[g][path];
 
      while (id.size() <= it) id.push_back(false);
    }
    id[g] = idx;
  }
 
  void get(string s) { 
    int ret = 0;
    int g = 0;
    for (int i = 1; i <= (int)s.size(); ++i) {
      int path = convert(s[i - 1]);
      if (nwt.size() <= g || !nwt[g][path]) return;
      g = nwt[g][path];
      answer[id[g]] += 1;
    }
  }
} T[N << 2];
 
int it;
void add(const auto& s, const auto& e, int idx) { 
  int g = 0;
  for (int i = 1; i <= (int)s.size(); ++i) { 
    int path = convert(s[i - 1]);
    if (!nwt[g][path]) nwt[g][path] = ++it;
    g = nwt[g][path];
  }
  T[g].add(string(e.rbegin(), e.rend()), idx);
}
 
void get(const auto& s) { 
  string rs = string(s.rbegin(), s.rend());
  int g = 0;
  for (int i = 1; i <= (int)s.size(); ++i) { 
    int path = convert(s[i - 1]);
    if (!nwt[g][path]) return;
    g = nwt[g][path];
    T[g].get(rs);
  }
}

int32_t main() { 
  cin.tie(0)->sync_with_stdio(0);
 
  cin >> n >> m;
 
  for (int i = 1; i <= n; ++i) cin >> str[i];
  for (int i = 1; i <= m; ++i) { 
    string s, e; cin >> s >> e;
    Q[i] = {s, e};
  }

  vector<pair<string, string>> que(Q + 1, Q + m + 1);
  sort(que.begin(), que.end());
  que.resize(unique(que.begin(), que.end()) - que.begin());

  {
    int it = 0;
    for (const auto& [s, e] : que) add(s, e, ++it);
    for (int i = 1; i <= n; ++i) get(str[i]);
  }

  for (int i = 1; i <= m; ++i) { 
    int idx = lower_bound(que.begin(), que.end(), Q[i]) - que.begin() + 1;
    cout << answer[idx] << "\n";
  }

}

Compilation message

selling_rna.cpp:12:19: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   12 | int convert(const auto& a) { return a == 'A' ? 0 : a == 'C' ? 1 : a == 'G' ? 2 : 3; }
      |                   ^~~~
selling_rna.cpp: In member function 'void Trie::add(std::string, int)':
selling_rna.cpp:24:25: warning: comparison of integer expressions of different signedness: 'std::vector<std::array<int, 4> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   24 |       while (nwt.size() <= g) nwt.push_back(array<int, 4>());
      |              ~~~~~~~~~~~^~~~
selling_rna.cpp:30:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   30 |       while (id.size() <= it) id.push_back(false);
      |              ~~~~~~~~~~^~~~~
selling_rna.cpp: In member function 'void Trie::get(std::string)':
selling_rna.cpp:40:22: warning: comparison of integer expressions of different signedness: 'std::vector<std::array<int, 4> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   40 |       if (nwt.size() <= g || !nwt[g][path]) return;
      |           ~~~~~~~~~~~^~~~
selling_rna.cpp:36:9: warning: unused variable 'ret' [-Wunused-variable]
   36 |     int ret = 0;
      |         ^~~
selling_rna.cpp: At global scope:
selling_rna.cpp:48:16: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   48 | void add(const auto& s, const auto& e, int idx) {
      |                ^~~~
selling_rna.cpp:48:31: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   48 | void add(const auto& s, const auto& e, int idx) {
      |                               ^~~~
selling_rna.cpp:58:16: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   58 | void get(const auto& s) {
      |                ^~~~
# Verdict Execution time Memory Grader output
1 Correct 75 ms 158372 KB Output is correct
2 Correct 44 ms 159872 KB Output is correct
3 Correct 68 ms 158136 KB Output is correct
4 Correct 104 ms 157992 KB Output is correct
5 Correct 89 ms 157976 KB Output is correct
6 Correct 79 ms 157916 KB Output is correct
7 Correct 42 ms 159516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 198 ms 168432 KB Output is correct
2 Correct 184 ms 168784 KB Output is correct
3 Correct 74 ms 172152 KB Output is correct
4 Correct 91 ms 172156 KB Output is correct
5 Correct 100 ms 191316 KB Output is correct
6 Correct 102 ms 191412 KB Output is correct
7 Execution timed out 1535 ms 203360 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 65 ms 163316 KB Output is correct
2 Correct 62 ms 161620 KB Output is correct
3 Correct 69 ms 162388 KB Output is correct
4 Correct 58 ms 162132 KB Output is correct
5 Correct 62 ms 161876 KB Output is correct
6 Correct 72 ms 162384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 158372 KB Output is correct
2 Correct 44 ms 159872 KB Output is correct
3 Correct 68 ms 158136 KB Output is correct
4 Correct 104 ms 157992 KB Output is correct
5 Correct 89 ms 157976 KB Output is correct
6 Correct 79 ms 157916 KB Output is correct
7 Correct 42 ms 159516 KB Output is correct
8 Correct 198 ms 168432 KB Output is correct
9 Correct 184 ms 168784 KB Output is correct
10 Correct 74 ms 172152 KB Output is correct
11 Correct 91 ms 172156 KB Output is correct
12 Correct 100 ms 191316 KB Output is correct
13 Correct 102 ms 191412 KB Output is correct
14 Execution timed out 1535 ms 203360 KB Time limit exceeded
15 Halted 0 ms 0 KB -