답안 #155612

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
155612 2019-09-29T08:34:26 Z georgerapeanu Selling RNA Strands (JOI16_selling_rna) C++11
100 / 100
719 ms 154864 KB
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

const int SIGMA = 4;

const int BASE = 5;
const int MOD1 = 1e9 + 7;
const int MOD2 = 666013;

map<char,int> to_norm;

struct hsh_t{
    int h1;
    int h2;
    int len;

    hsh_t(){
        h1 = h2 = len = 0;
    }

    void add_back(int x){
        x++;
        this->len++;
        this->h1 = (1LL * this->h1 * BASE + x) % MOD1;
        this->h2 = (1LL * this->h2 * BASE + x) % MOD2;
    }

    bool operator < (const hsh_t &other)const{
        if(this->h1 != other.h1){
            return this->h1 < other.h1;
        }
        if(this->h2 != other.h2){
            return this->h2 < other.h2;
        }
        return this->len < other.len;
    }
};

struct trie{
    int st,dr;
    int cnt;
    trie *sons[SIGMA];

    trie(){
        st = dr = 0;
        cnt = 0;
        memset(sons,0,sizeof(sons));
    }
} *root;

void ins(trie *root,char *a){
    if(*a == '\0' || *a == '\n'){
        root->cnt++;
        return ;
    }

    if(root->sons[to_norm[(*a)]] == NULL){
        root->sons[to_norm[(*a)]] = new trie();
    }

    ins(root->sons[to_norm[(*a)]],a + 1);
}

vector<pair<hsh_t,int> > pos;

int last = 0;
vector<int> stuff;
void dfs(trie *root){
    root->st = ++last;
    if(root->cnt > 0){
        hsh_t tmp;
        for(int i = (int)stuff.size() - 1;i >= 0;i--){
            tmp.add_back(stuff[i]);
            for(int x = 0;x < root->cnt;x++){
                pos.push_back({tmp,root->st});
            }
        }
    }
    for(int i = 0;i < SIGMA;i++){
        if(root->sons[i] != NULL){
            stuff.push_back(i);
            dfs(root->sons[i]);
            stuff.pop_back();
        }
    }
    root->dr = last;
}

int n,m;
char a[int(1e5) + 5];
char b[int(1e5) + 5];

int get_ans(trie *root,char *a,char *b){
    if(*a != '\n' && *a != '\0'){
        if(root->sons[to_norm[*a]] == NULL){
            return 0;
        }
        return get_ans(root->sons[to_norm[*a]],a + 1,b);
    }

    int len = 0;

    while(b[len] != '\n' && b[len] != '\0'){
        len++;
    }

    len--;

    hsh_t tmp;
    
    while(len >= 0){
        tmp.add_back(to_norm[b[len]]);
        len--;
    }


    return lower_bound(pos.begin(),pos.end(),make_pair(tmp,root->dr + 1)) - lower_bound(pos.begin(),pos.end(),make_pair(tmp,root->st));
}

int main(){

    to_norm['A'] = 0;
    to_norm['C'] = 1;
    to_norm['G'] = 2;
    to_norm['U'] = 3;

    root = new trie();

    scanf("%d %d\n",&n,&m);

    for(int i = 1;i <= n;i++){
        scanf("%s\n",a);
        ins(root,a);
    }

    dfs(root);
    
    sort(pos.begin(),pos.end());
     
    for(int i = 1;i <= m;i++){
        scanf("%s %s\n",a,b);
        printf("%d\n",get_ans(root,a,b));
    }

    return 0;
}

Compilation message

selling_rna.cpp: In function 'int main()':
selling_rna.cpp:134:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d\n",&n,&m);
     ~~~~~^~~~~~~~~~~~~~~~~
selling_rna.cpp:137:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%s\n",a);
         ~~~~~^~~~~~~~~~
selling_rna.cpp:146:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%s %s\n",a,b);
         ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 468 ms 34280 KB Output is correct
2 Correct 450 ms 34112 KB Output is correct
3 Correct 697 ms 154864 KB Output is correct
4 Correct 698 ms 148752 KB Output is correct
5 Correct 478 ms 109520 KB Output is correct
6 Correct 473 ms 110564 KB Output is correct
7 Correct 400 ms 33600 KB Output is correct
8 Correct 573 ms 78192 KB Output is correct
9 Correct 552 ms 70432 KB Output is correct
10 Correct 487 ms 70100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 2536 KB Output is correct
2 Correct 36 ms 2540 KB Output is correct
3 Correct 41 ms 2536 KB Output is correct
4 Correct 30 ms 2540 KB Output is correct
5 Correct 36 ms 2796 KB Output is correct
6 Correct 40 ms 2668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 468 ms 34280 KB Output is correct
9 Correct 450 ms 34112 KB Output is correct
10 Correct 697 ms 154864 KB Output is correct
11 Correct 698 ms 148752 KB Output is correct
12 Correct 478 ms 109520 KB Output is correct
13 Correct 473 ms 110564 KB Output is correct
14 Correct 400 ms 33600 KB Output is correct
15 Correct 573 ms 78192 KB Output is correct
16 Correct 552 ms 70432 KB Output is correct
17 Correct 487 ms 70100 KB Output is correct
18 Correct 46 ms 2536 KB Output is correct
19 Correct 36 ms 2540 KB Output is correct
20 Correct 41 ms 2536 KB Output is correct
21 Correct 30 ms 2540 KB Output is correct
22 Correct 36 ms 2796 KB Output is correct
23 Correct 40 ms 2668 KB Output is correct
24 Correct 442 ms 33928 KB Output is correct
25 Correct 483 ms 33980 KB Output is correct
26 Correct 446 ms 33852 KB Output is correct
27 Correct 719 ms 132284 KB Output is correct
28 Correct 419 ms 33720 KB Output is correct
29 Correct 186 ms 16848 KB Output is correct