답안 #1098606

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1098606 2024-10-09T16:07:17 Z flyingkite Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
606 ms 371792 KB
#include <bits/stdc++.h>
using namespace std;
 
using ll = long long;
using pll = pair<long long, long long>;
 
#define pb push_back
#define F first
#define S second  
#define all(x) (x).begin(), (x).end()
 
const ll N = 2e6 + 100;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll block = 520;
const ll base = 35711;
struct Trie{
    struct node{
        ll c[4];
        ll l, r;
        vector<ll>range;
    }; node x[N];
    ll cur;
    Trie() : cur(0){
        memset(x[0].c, -1, sizeof(x[0].c));
        x[0].l = inf;
        x[0].r = -inf;
    }
    ll init(){
        cur++;
        memset(x[cur].c, -1, sizeof(x[cur].c));
        x[cur].l = inf, x[cur].r = -inf;
        return cur;
    }  
    void add(string s, ll idx){
        ll pos = 0;
        for(auto f : s){
            ll c = f - 'A';
            if(x[pos].c[c] == -1) x[pos].c[c] = init();
            pos = x[pos].c[c];
            x[pos].range.pb(idx);
            x[pos].l = min(idx, x[pos].l);
            x[pos].r = max(idx, x[pos].r);
        }
    }
    pll find_range(string s){
        ll pos = 0;
        for(auto f : s){
            ll c = f - 'A';
            pos = x[pos].c[c];
            if(pos == -1) return {inf, -inf};
        }
        return {x[pos].l, x[pos].r};
    }
    vector<ll>dfs(string s){
        ll pos = 0;
        vector<ll>tmp;
        for(auto f : s){
            ll c = f - 'A';
            pos = x[pos].c[c];
            if(pos == -1) return tmp;
        }
        return x[pos].range;
    }
};
string cnv(string s){
    string ans = s;
    for(int i = 0; i < ans.size();i++){
        if(ans[i] == 'C') ans[i] = 'B';
        else if(ans[i] == 'G') ans[i] = 'C';
        else if(ans[i] == 'U') ans[i] = 'D'; 
    }
    return ans;
}
Trie rev, tr;
void to_nho_cau(){
    ll n,m; cin >> n >> m;
    vector<string>v;
    for(int i = 1; i <= n;i++){
        string s; cin >> s;
        s = cnv(s);
        v.pb(s);
    }
    sort(all(v));
    for(int i = 0; i < v.size();i++){
        tr.add(v[i], i);
        string t = v[i]; reverse(all(t));
        rev.add(t, i);
    }
    while(m--){
        string p,q; cin >> p >> q;
        p = cnv(p), q = cnv(q);    
        reverse(all(q));
        pll cur = tr.find_range(p);
        ll l = cur.F, r = cur.S;
        vector<ll>res = rev.dfs(q);
        if(!res.size() || l > r){
            cout << 0 << '\n';
            continue;
        }
        ll high = upper_bound(all(res), r) - res.begin();
        ll low = lower_bound(all(res), l) - res.begin();
        cout << high - low << '\n';
    }
}
 
signed main()
{   
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;   
    //cin >> t;
    while(t--) to_nho_cau();
}

Compilation message

selling_rna.cpp: In function 'std::string cnv(std::string)':
selling_rna.cpp:68:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |     for(int i = 0; i < ans.size();i++){
      |                    ~~^~~~~~~~~~~~
selling_rna.cpp: In function 'void to_nho_cau()':
selling_rna.cpp:85:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |     for(int i = 0; i < v.size();i++){
      |                    ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 282204 KB Output is correct
2 Correct 113 ms 282196 KB Output is correct
3 Correct 104 ms 282068 KB Output is correct
4 Correct 119 ms 282196 KB Output is correct
5 Correct 106 ms 282196 KB Output is correct
6 Correct 95 ms 282196 KB Output is correct
7 Correct 118 ms 282252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 219 ms 371792 KB Output is correct
2 Correct 241 ms 367052 KB Output is correct
3 Correct 260 ms 369748 KB Output is correct
4 Correct 274 ms 366272 KB Output is correct
5 Correct 290 ms 362820 KB Output is correct
6 Correct 231 ms 364056 KB Output is correct
7 Correct 158 ms 320900 KB Output is correct
8 Correct 293 ms 360372 KB Output is correct
9 Correct 251 ms 353084 KB Output is correct
10 Correct 253 ms 349392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 386 ms 287152 KB Output is correct
2 Correct 150 ms 285680 KB Output is correct
3 Correct 165 ms 286152 KB Output is correct
4 Correct 206 ms 285896 KB Output is correct
5 Correct 606 ms 285932 KB Output is correct
6 Correct 385 ms 286144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 282204 KB Output is correct
2 Correct 113 ms 282196 KB Output is correct
3 Correct 104 ms 282068 KB Output is correct
4 Correct 119 ms 282196 KB Output is correct
5 Correct 106 ms 282196 KB Output is correct
6 Correct 95 ms 282196 KB Output is correct
7 Correct 118 ms 282252 KB Output is correct
8 Correct 219 ms 371792 KB Output is correct
9 Correct 241 ms 367052 KB Output is correct
10 Correct 260 ms 369748 KB Output is correct
11 Correct 274 ms 366272 KB Output is correct
12 Correct 290 ms 362820 KB Output is correct
13 Correct 231 ms 364056 KB Output is correct
14 Correct 158 ms 320900 KB Output is correct
15 Correct 293 ms 360372 KB Output is correct
16 Correct 251 ms 353084 KB Output is correct
17 Correct 253 ms 349392 KB Output is correct
18 Correct 386 ms 287152 KB Output is correct
19 Correct 150 ms 285680 KB Output is correct
20 Correct 165 ms 286152 KB Output is correct
21 Correct 206 ms 285896 KB Output is correct
22 Correct 606 ms 285932 KB Output is correct
23 Correct 385 ms 286144 KB Output is correct
24 Correct 248 ms 359492 KB Output is correct
25 Correct 268 ms 359884 KB Output is correct
26 Correct 333 ms 358904 KB Output is correct
27 Correct 286 ms 358600 KB Output is correct
28 Correct 584 ms 335068 KB Output is correct
29 Correct 287 ms 307760 KB Output is correct