답안 #752799

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
752799 2023-06-03T21:24:33 Z racsosabe Selling RNA Strands (JOI16_selling_rna) C++14
100 / 100
154 ms 115212 KB
#include<bits/stdc++.h>
using namespace::std;
 
const int N = 100000 + 5;
const int SUML = 2000000 + 5;
const int E = 4;
const string alphabet = "AGCU";

int id(char c) {
    for(int i = 0; i < alphabet.size(); i++) {
        if(alphabet[i] == c) return i;
    }
    assert(false);
}

int n;
int m;
int T;
int nodes;
int res[N];
string s[N];
string Q[N];
int in[SUML];
int out[SUML];
int sorted[N];
int frec[SUML];
int trie[E][SUML];
vector<int> ids[SUML];
vector<int> pending[N];

void add_node() {
    for(int i = 0; i < E; i++) trie[i][nodes] = 0;
    frec[nodes] = 0;
    ids[nodes].clear();
    nodes += 1;
}

void insert(string &s, int to) {
    int pos = 0;
    for(int i = 0; i < s.size(); i++) {
        int c = id(s[i]);
        if(trie[c][pos] == 0) {
            trie[c][pos] = nodes;
            add_node();
        }
        pos = trie[c][pos];
        frec[pos] += 1;
    }
    ids[pos].emplace_back(to);
}

void sort_strings(int u = 0) {
    in[u] = T;
    for(auto x : ids[u]) {
        sorted[T++] = x;
    }
    for(int c = 0; c < E; c++) {
        if(!trie[c][u]) continue;
        sort_strings(trie[c][u]);
    }
    out[u] = T - 1;
}

int query(string &s) {
    int pos = 0;
    for(int i = 0; i < s.size(); i++) {
        int c = id(s[i]);
        if(!trie[c][pos]) return 0;
        pos = trie[c][pos];
    }
    return frec[pos];
}

void solve() {
    nodes = 0;
    add_node();
    for(int i = 0; i < n; i++) {
        insert(s[sorted[i]], i);
        for(auto to : pending[i]) {
            if(to < 0) {
                res[-to] -= query(Q[-to]);
            }
            else { 
                res[to] += query(Q[to]);
            }
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    nodes = 1;
    for(int i = 1; i <= n; i++) {
        cin >> s[i];
        insert(s[i], i);
    }
    sort_strings();
    for(int i = 1; i <= m; i++) {
        string P;
        cin >> P >> Q[i];
        reverse(Q[i].begin(), Q[i].end());
        int pos = 0;
        for(int j = 0; j < P.size(); j++) {
            int c = id(P[j]);
            if(!trie[c][pos]) {
                pos = -1;
                break;
            }
            pos = trie[c][pos];
        }
        if(~pos) {
            if(in[pos]) pending[in[pos] - 1].emplace_back(-i);
            pending[out[pos]].emplace_back(i);
        }
    }
    for(int i = 1; i <= n; i++) reverse(s[i].begin(), s[i].end());
    solve();
    for(int i = 1; i <= m; i++) cout << res[i] << '\n';
	return 0;
}

Compilation message

selling_rna.cpp: In function 'int id(char)':
selling_rna.cpp:10:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   10 |     for(int i = 0; i < alphabet.size(); i++) {
      |                    ~~^~~~~~~~~~~~~~~~~
selling_rna.cpp: In function 'void insert(std::string&, int)':
selling_rna.cpp:40:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |     for(int i = 0; i < s.size(); i++) {
      |                    ~~^~~~~~~~~~
selling_rna.cpp: In function 'int query(std::string&)':
selling_rna.cpp:66:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |     for(int i = 0; i < s.size(); i++) {
      |                    ~~^~~~~~~~~~
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:105:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |         for(int j = 0; j < P.size(); j++) {
      |                        ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 55892 KB Output is correct
2 Correct 26 ms 55968 KB Output is correct
3 Correct 26 ms 55864 KB Output is correct
4 Correct 25 ms 55900 KB Output is correct
5 Correct 26 ms 55876 KB Output is correct
6 Correct 30 ms 55968 KB Output is correct
7 Correct 27 ms 55912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 101 ms 96892 KB Output is correct
2 Correct 95 ms 94856 KB Output is correct
3 Correct 154 ms 115212 KB Output is correct
4 Correct 154 ms 114788 KB Output is correct
5 Correct 132 ms 94236 KB Output is correct
6 Correct 141 ms 94692 KB Output is correct
7 Correct 63 ms 65356 KB Output is correct
8 Correct 96 ms 85964 KB Output is correct
9 Correct 91 ms 82728 KB Output is correct
10 Correct 76 ms 78568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 57556 KB Output is correct
2 Correct 41 ms 56976 KB Output is correct
3 Correct 43 ms 57172 KB Output is correct
4 Correct 44 ms 56876 KB Output is correct
5 Correct 40 ms 57044 KB Output is correct
6 Correct 43 ms 57152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 55892 KB Output is correct
2 Correct 26 ms 55968 KB Output is correct
3 Correct 26 ms 55864 KB Output is correct
4 Correct 25 ms 55900 KB Output is correct
5 Correct 26 ms 55876 KB Output is correct
6 Correct 30 ms 55968 KB Output is correct
7 Correct 27 ms 55912 KB Output is correct
8 Correct 101 ms 96892 KB Output is correct
9 Correct 95 ms 94856 KB Output is correct
10 Correct 154 ms 115212 KB Output is correct
11 Correct 154 ms 114788 KB Output is correct
12 Correct 132 ms 94236 KB Output is correct
13 Correct 141 ms 94692 KB Output is correct
14 Correct 63 ms 65356 KB Output is correct
15 Correct 96 ms 85964 KB Output is correct
16 Correct 91 ms 82728 KB Output is correct
17 Correct 76 ms 78568 KB Output is correct
18 Correct 41 ms 57556 KB Output is correct
19 Correct 41 ms 56976 KB Output is correct
20 Correct 43 ms 57172 KB Output is correct
21 Correct 44 ms 56876 KB Output is correct
22 Correct 40 ms 57044 KB Output is correct
23 Correct 43 ms 57152 KB Output is correct
24 Correct 100 ms 94280 KB Output is correct
25 Correct 107 ms 94896 KB Output is correct
26 Correct 93 ms 93644 KB Output is correct
27 Correct 139 ms 108280 KB Output is correct
28 Correct 115 ms 70524 KB Output is correct
29 Correct 82 ms 62156 KB Output is correct