답안 #1081439

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1081439 2024-08-30T04:55:36 Z MinhKien Selling RNA Strands (JOI16_selling_rna) C++17
35 / 100
1500 ms 277072 KB
#include <iostream>
#include <string>

using namespace std;

const int max_child = 16;

string pre, suf;
int x, y;

struct Trie {
    struct node {
        node *child[max_child];
        int cnt;

        node () {
            fill_n(child, max_child, nullptr);
            cnt = 0;
        }
    };

    node *root;
    Trie () {
        root = new node();
    }

    void add_string(const string &s) {
        node *cur = root;

        int n = s.length();
        for (int i = 0; i < n; ++i) {
            int p = (s[i] - 'A') * 4 + (s[n - i - 1] - 'A');

            if (cur->child[p] == nullptr) cur->child[p] = new node();

            cur = cur->child[p];
            ++cur->cnt;
        }
    }

    void solve(int &ans, const int i, node *cur) {
        if (i == max(x, y)) {
            ans += cur->cnt;
            return;
        }

        if (i < min(x, y)) {
            int p = (pre[i] - 'A') * 4 + (suf[y - i - 1] - 'A');

            if (cur->child[p] == nullptr) return;
            
            solve(ans, i + 1, cur->child[p]);
        } else {
            if (i < x) {
                for (int j = 0, p = (pre[i] - 'A') * 4; j < 4; ++j, ++p) {
                    if (cur->child[p] != nullptr) {
                        solve(ans, i + 1, cur->child[p]);
                    }
                }
            } else {
                for (int j = 0, p = suf[y - i - 1] - 'A'; j < 4; ++j, p += 4) {
                    if (cur->child[p] != nullptr) solve(ans, i + 1, cur->child[p]);
                }
            }
        }
    }
} T;

int n, q;
string s;

void change (string &s) {
    for (int i = 0; i < s.length(); ++i) {
        if (s[i] == 'C') s[i] = 'B';
        else if (s[i] == 'G') s[i] = 'C';
        else if (s[i] == 'U') s[i] = 'D';
    }
}

int main() {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios_base::sync_with_stdio(false);

    cin >> n >> q;
    while (n--) {
        cin >> s;
        change(s);
        T.add_string(s);
    }

    while (q--) {
        cin >> pre >> suf;
        change(pre);
        change(suf);
        x = pre.length();
        y = suf.length();
        
        int ans = 0;
        T.solve(ans, 0, T.root);

        cout << ans << "\n";
    }

    return 0;
}

Compilation message

selling_rna.cpp: In function 'void change(std::string&)':
selling_rna.cpp:73:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |     for (int i = 0; i < s.length(); ++i) {
      |                     ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 404 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1533 ms 277072 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 600 KB Output is correct
2 Correct 14 ms 1116 KB Output is correct
3 Correct 21 ms 860 KB Output is correct
4 Correct 8 ms 604 KB Output is correct
5 Correct 15 ms 1140 KB Output is correct
6 Correct 12 ms 856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 404 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Execution timed out 1533 ms 277072 KB Time limit exceeded
9 Halted 0 ms 0 KB -