답안 #936142

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
936142 2024-03-01T08:55:21 Z AdamGS Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
464 ms 433516 KB
#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef struct item1 * pitem1;
typedef struct item2 * pitem2;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
struct item2 {
  pitem2 nxt[4]={nullptr, nullptr, nullptr, nullptr};
  int ile=0;
};
struct item1 {
  pitem1 nxt[4]={nullptr, nullptr, nullptr, nullptr};
  vector<int>pyt;
  pitem2 tr=nullptr;
};
vector<int>koduj(string s) {
  vector<int>P;
  for(auto i : s) {
    if(i=='A') P.pb(0);
    else if(i=='G') P.pb(1);
    else if(i=='C') P.pb(2);
    else P.pb(3);
  }
  return P;
}
const int LIM=1e5+7;
vector<int>V[LIM];
int wynik[LIM];
void dodaj2(pitem2 v, vector<int>&P, int x) {
  v->ile+=1;
  if(x==0) return;
  if(v->nxt[P[x-1]]==nullptr) v->nxt[P[x-1]]=new item2();
  dodaj2(v->nxt[P[x-1]], P, x-1);
}
void dodaj(pitem1 v, vector<int>&P, int x) {
  if(x==P.size()) {
    if(v->tr==nullptr) v->tr=new item2();
    dodaj2(v->tr, P, P.size());
    return;
  }
  if(v->nxt[P[x]]==nullptr) v->nxt[P[x]]=new item1();
  dodaj(v->nxt[P[x]], P, x+1);
}
void dodaj_pyt(pitem1 v, vector<int>&P, int x, int ind) {
  if(x==P.size()) {
    v->pyt.pb(ind);
    return;
  }
  if(v->nxt[P[x]]==nullptr) return;
  dodaj_pyt(v->nxt[P[x]], P, x+1, ind);
}
void lacz(pitem2 a, pitem2 b) {
  a->ile+=b->ile;
  rep(i, 4) if(b->nxt[i]!=nullptr) {
    if(a->nxt[i]==nullptr) a->nxt[i]=new item2();
    lacz(a->nxt[i], b->nxt[i]);
  }
}
pitem2 merguj(pitem2 a, pitem2 b) {
  if(a==nullptr) return b;
  if(b==nullptr) return a;
  if(a->ile<b->ile) swap(a, b);
  lacz(a, b);
  return a;
}
int zejdz(pitem2 v, vector<int>&P, int x) {
  if(x==P.size()) return v->ile;
  if(v->nxt[P[x]]==nullptr) return 0;
  return zejdz(v->nxt[P[x]], P, x+1);
}
void solve(pitem1 v) {
  rep(i, 4) if(v->nxt[i]!=nullptr) {
    solve(v->nxt[i]);
    v->tr=merguj(v->tr, v->nxt[i]->tr);
  }
  for(auto i : v->pyt) {
    wynik[i]=zejdz(v->tr, V[i], 0);
  }
}
int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);
  pitem1 root=new item1();
  int n, m;
  cin >> n >> m;
  rep(i, n) {
    string s;
    cin >> s;
    vector<int>P=koduj(s);
    dodaj(root, P, 0);
  }
  rep(i, m) {
    string a, b;
    cin >> a >> b;
    vector<int>P=koduj(a);
    V[i]=koduj(b);
    reverse(all(V[i]));
    dodaj_pyt(root, P, 0, i);
  }
  solve(root);
  rep(i, m) cout << wynik[i] << '\n';
}

Compilation message

selling_rna.cpp: In function 'void dodaj(pitem1, std::vector<int>&, int)':
selling_rna.cpp:41:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |   if(x==P.size()) {
      |      ~^~~~~~~~~~
selling_rna.cpp: In function 'void dodaj_pyt(pitem1, std::vector<int>&, int, int)':
selling_rna.cpp:50:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |   if(x==P.size()) {
      |      ~^~~~~~~~~~
selling_rna.cpp: In function 'int zejdz(pitem2, std::vector<int>&, int)':
selling_rna.cpp:72:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |   if(x==P.size()) return v->ile;
      |      ~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2908 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2908 KB Output is correct
4 Correct 1 ms 2908 KB Output is correct
5 Correct 1 ms 2908 KB Output is correct
6 Correct 1 ms 2908 KB Output is correct
7 Correct 1 ms 2908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 206 ms 200276 KB Output is correct
2 Correct 206 ms 212048 KB Output is correct
3 Correct 305 ms 300864 KB Output is correct
4 Correct 249 ms 284240 KB Output is correct
5 Correct 464 ms 427796 KB Output is correct
6 Correct 441 ms 433516 KB Output is correct
7 Correct 118 ms 90556 KB Output is correct
8 Correct 267 ms 257284 KB Output is correct
9 Correct 205 ms 213840 KB Output is correct
10 Correct 200 ms 230928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 5688 KB Output is correct
2 Correct 17 ms 5720 KB Output is correct
3 Correct 23 ms 5724 KB Output is correct
4 Correct 20 ms 4700 KB Output is correct
5 Correct 21 ms 5988 KB Output is correct
6 Correct 21 ms 5832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2908 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2908 KB Output is correct
4 Correct 1 ms 2908 KB Output is correct
5 Correct 1 ms 2908 KB Output is correct
6 Correct 1 ms 2908 KB Output is correct
7 Correct 1 ms 2908 KB Output is correct
8 Correct 206 ms 200276 KB Output is correct
9 Correct 206 ms 212048 KB Output is correct
10 Correct 305 ms 300864 KB Output is correct
11 Correct 249 ms 284240 KB Output is correct
12 Correct 464 ms 427796 KB Output is correct
13 Correct 441 ms 433516 KB Output is correct
14 Correct 118 ms 90556 KB Output is correct
15 Correct 267 ms 257284 KB Output is correct
16 Correct 205 ms 213840 KB Output is correct
17 Correct 200 ms 230928 KB Output is correct
18 Correct 22 ms 5688 KB Output is correct
19 Correct 17 ms 5720 KB Output is correct
20 Correct 23 ms 5724 KB Output is correct
21 Correct 20 ms 4700 KB Output is correct
22 Correct 21 ms 5988 KB Output is correct
23 Correct 21 ms 5832 KB Output is correct
24 Correct 234 ms 215100 KB Output is correct
25 Correct 225 ms 215584 KB Output is correct
26 Correct 210 ms 212184 KB Output is correct
27 Correct 284 ms 251744 KB Output is correct
28 Correct 132 ms 52308 KB Output is correct
29 Correct 82 ms 15072 KB Output is correct