답안 #1057132

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1057132 2024-08-13T14:26:03 Z vjudge1 Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
168 ms 571988 KB
#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
using namespace std;
using ll = long long;
const int nx=1e5+5;
const int N=2e6+5;
int getid(char c)
{
    if(c=='A')
        return 0;
    if(c=='C')
        return 1;
    if(c=='G')
        return 2;
    return 3;
}
int n, m;
string a[nx];
struct trie
{
    struct dak
    {
        int child[4], l, r;
        dak()
        {
            memset(child, -1, sizeof(child));
            l=inf, r=0;
        }
    } nod[N<<2];
    int cur;
    trie() : cur(0){};
    int new_node()
    {
        cur++;
        return cur;
    }
    void add(string s, int id)
    {
        int pos=0;
        for(int i = 0; i < s.size(); i++)
        {
            int c=getid(s[i]);
            if(nod[pos].child[c]==-1)
                nod[pos].child[c]=new_node();
            pos=nod[pos].child[c];
            nod[pos].l=min(nod[pos].l, id);
            nod[pos].r=max(nod[pos].r, id);
        }
    }
    ii get_range(string s)
    {
        int pos=0;
        for(int i = 0; i < s.size(); i++)
        {
            int c=getid(s[i]);
            if(nod[pos].child[c]==-1)
                return ii(-1, -1);
            pos=nod[pos].child[c];
        }
        return ii(nod[pos].l, nod[pos].r);
    }
} T;
struct rev_trie
{
    struct dak
    {
        int child[4];
        vector<int> ids;
        dak()
        {
            memset(child, -1, sizeof(child));
            ids.clear();
        }
    } nod[N<<2];
    int cur;
    rev_trie() : cur(0){};
    int new_node()
    {
        cur++;
        return cur;
    }
    void add(string s, int id)
    {
        int pos=0;
        for(int i = s.size()-1; i >= 0; i--)
        {
            int c=getid(s[i]);
            if(nod[pos].child[c]==-1)
                nod[pos].child[c]=new_node();
            pos=nod[pos].child[c];
            nod[pos].ids.emplace_back(id);
        }
    }
    int query(string s, ii f)
    {
        int pos=0;
        for(int i = s.size()-1; i >= 0; i--)
        {
            int c=getid(s[i]);
            if(nod[pos].child[c]==-1)
                return 0;
            pos=nod[pos].child[c];
        }
        int l=lower_bound(nod[pos].ids.begin(), nod[pos].ids.end(), f.fi)-nod[pos].ids.begin();
        int r=upper_bound(nod[pos].ids.begin(), nod[pos].ids.end(), f.se)-nod[pos].ids.begin();
        return r-l;
    }
} T1;
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n>>m;
    for(int i = 1; i <= n; i++)
        cin>>a[i];
    sort(a+1, a+n+1);
    for(int i = 1; i <= n; i++)
    {
        T.add(a[i], i);
        T1.add(a[i], i);
    }
    while(m--)
    {
        string x, y;
        cin>>x>>y;
        ii cur=T.get_range(x);
        cout<<T1.query(y, cur)<<'\n';
    }
}

Compilation message

selling_rna.cpp: In member function 'void trie::add(std::string, int)':
selling_rna.cpp:43:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |         for(int i = 0; i < s.size(); i++)
      |                        ~~^~~~~~~~~~
selling_rna.cpp: In member function 'std::pair<int, int> trie::get_range(std::string)':
selling_rna.cpp:56:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |         for(int i = 0; i < s.size(); i++)
      |                        ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 504408 KB Output is correct
2 Correct 60 ms 504404 KB Output is correct
3 Correct 60 ms 504400 KB Output is correct
4 Correct 60 ms 504472 KB Output is correct
5 Correct 60 ms 504464 KB Output is correct
6 Correct 67 ms 504512 KB Output is correct
7 Correct 67 ms 504640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 163 ms 571988 KB Output is correct
2 Correct 150 ms 569168 KB Output is correct
3 Correct 103 ms 521612 KB Output is correct
4 Correct 88 ms 521496 KB Output is correct
5 Correct 123 ms 546388 KB Output is correct
6 Correct 125 ms 547152 KB Output is correct
7 Correct 105 ms 520020 KB Output is correct
8 Correct 135 ms 540496 KB Output is correct
9 Correct 126 ms 536656 KB Output is correct
10 Correct 110 ms 533708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 505940 KB Output is correct
2 Correct 80 ms 505404 KB Output is correct
3 Correct 80 ms 505488 KB Output is correct
4 Correct 70 ms 505472 KB Output is correct
5 Correct 70 ms 505512 KB Output is correct
6 Correct 76 ms 505680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 504408 KB Output is correct
2 Correct 60 ms 504404 KB Output is correct
3 Correct 60 ms 504400 KB Output is correct
4 Correct 60 ms 504472 KB Output is correct
5 Correct 60 ms 504464 KB Output is correct
6 Correct 67 ms 504512 KB Output is correct
7 Correct 67 ms 504640 KB Output is correct
8 Correct 163 ms 571988 KB Output is correct
9 Correct 150 ms 569168 KB Output is correct
10 Correct 103 ms 521612 KB Output is correct
11 Correct 88 ms 521496 KB Output is correct
12 Correct 123 ms 546388 KB Output is correct
13 Correct 125 ms 547152 KB Output is correct
14 Correct 105 ms 520020 KB Output is correct
15 Correct 135 ms 540496 KB Output is correct
16 Correct 126 ms 536656 KB Output is correct
17 Correct 110 ms 533708 KB Output is correct
18 Correct 74 ms 505940 KB Output is correct
19 Correct 80 ms 505404 KB Output is correct
20 Correct 80 ms 505488 KB Output is correct
21 Correct 70 ms 505472 KB Output is correct
22 Correct 70 ms 505512 KB Output is correct
23 Correct 76 ms 505680 KB Output is correct
24 Correct 151 ms 561236 KB Output is correct
25 Correct 168 ms 561348 KB Output is correct
26 Correct 150 ms 560464 KB Output is correct
27 Correct 97 ms 521100 KB Output is correct
28 Correct 146 ms 526720 KB Output is correct
29 Correct 100 ms 513096 KB Output is correct