답안 #1031509

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1031509 2024-07-22T22:40:35 Z vjudge1 Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
173 ms 98384 KB
#include<bits/stdc++.h>
using namespace std;
#define N 1<<23
int rts[N],ch1[N][4],ch2[N][4],mmpp[128],CC1,CC2,rt,sz[N],val[N],ans[200100];
bitset<N> hasaQ;
map<int,vector<pair<vector<int>,int>>> qrs;
int a_node(string str){
    vector<int>v;
    for(auto i:str)
        v.push_back(mmpp[i]);
    int k=rt;
    for(auto i:v){
        if(!ch1[k][i])
            ch1[k][i]=++CC1;
        k=ch1[k][i];
    }
    return k;
}
void Adtri2(int nod,string str,int b=1){
    vector<int>v;
    for(auto i:str)
        v.push_back(mmpp[i]);
    if(!rts[nod])
        rts[nod]=++CC2;
    int k=rts[nod];
    sz[nod]+=str.size();
    for(auto i:v){
        if(!ch2[k][i])
            ch2[k][i]=++CC2;
        k=ch2[k][i],val[k]+=b;
    }
    sz[k]+=b;
}
int mrg(int a,int b){
    if(!a||!b)return a+b;
    if(sz[b]>sz[a])swap(a,b);
    sz[a]+=sz[b],val[a]+=val[b];
    for(int i=0;i<4;i++)
        ch2[a][i]=mrg(ch2[a][i],ch2[b][i]);
    return a;
}
int gothrough(int n,vector<int>v){
    for(auto i:v)
        n=ch2[n][i];
    return n;
}
void do_a_dfs(int n){
    if(!n)return;
    for(auto k:ch1[n])
        do_a_dfs(k),rts[n]=mrg(rts[n],rts[k]);
    if(hasaQ[n]){
        for(auto [x,y]:qrs[n])
            ans[y]+=val[gothrough(rts[n],x)];
    }
}
int main(){
    mmpp['A']=0;
    mmpp['C']=rt=CC1=1;
    mmpp['G']=2;
    mmpp['U']=3;
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        string str;
        cin>>str;
        int k=a_node(str);
        reverse(str.begin(),str.end());
        Adtri2(k,str);
    }
    for(int i=0;i<m;i++){
        string a,b;
        cin>>a>>b;
        reverse(b.begin(),b.end());
        int k=a_node(a);
        vector<int>v;
        for(auto i:b)
            v.push_back(mmpp[i]);
        hasaQ[k]=1;
        qrs[k].push_back({v,i});
    }
    for(int x=CC2;x;x--) {
        sz[x]=1;
        for(auto k:ch2[x])
            sz[x]+=sz[k];
    }
    do_a_dfs(rt);
    for(int i=0;i<m;i++)
        cout<<ans[i]<<'\n';
}

Compilation message

selling_rna.cpp: In function 'int a_node(std::string)':
selling_rna.cpp:10:26: warning: array subscript has type 'char' [-Wchar-subscripts]
   10 |         v.push_back(mmpp[i]);
      |                          ^
selling_rna.cpp: In function 'void Adtri2(int, std::string, int)':
selling_rna.cpp:22:26: warning: array subscript has type 'char' [-Wchar-subscripts]
   22 |         v.push_back(mmpp[i]);
      |                          ^
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:77:30: warning: array subscript has type 'char' [-Wchar-subscripts]
   77 |             v.push_back(mmpp[i]);
      |                              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 51896 KB Output is correct
2 Correct 99 ms 50976 KB Output is correct
3 Correct 167 ms 98384 KB Output is correct
4 Correct 161 ms 94224 KB Output is correct
5 Correct 111 ms 61012 KB Output is correct
6 Correct 116 ms 61780 KB Output is correct
7 Correct 141 ms 51284 KB Output is correct
8 Correct 173 ms 76076 KB Output is correct
9 Correct 163 ms 68508 KB Output is correct
10 Correct 106 ms 47696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 6596 KB Output is correct
2 Correct 22 ms 5008 KB Output is correct
3 Correct 25 ms 5828 KB Output is correct
4 Correct 23 ms 3188 KB Output is correct
5 Correct 22 ms 5468 KB Output is correct
6 Correct 27 ms 3916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 99 ms 51896 KB Output is correct
9 Correct 99 ms 50976 KB Output is correct
10 Correct 167 ms 98384 KB Output is correct
11 Correct 161 ms 94224 KB Output is correct
12 Correct 111 ms 61012 KB Output is correct
13 Correct 116 ms 61780 KB Output is correct
14 Correct 141 ms 51284 KB Output is correct
15 Correct 173 ms 76076 KB Output is correct
16 Correct 163 ms 68508 KB Output is correct
17 Correct 106 ms 47696 KB Output is correct
18 Correct 29 ms 6596 KB Output is correct
19 Correct 22 ms 5008 KB Output is correct
20 Correct 25 ms 5828 KB Output is correct
21 Correct 23 ms 3188 KB Output is correct
22 Correct 22 ms 5468 KB Output is correct
23 Correct 27 ms 3916 KB Output is correct
24 Correct 109 ms 45132 KB Output is correct
25 Correct 121 ms 47444 KB Output is correct
26 Correct 100 ms 43600 KB Output is correct
27 Correct 162 ms 82552 KB Output is correct
28 Correct 150 ms 19792 KB Output is correct
29 Correct 124 ms 14804 KB Output is correct