Submission #1073400

# Submission time Handle Problem Language Result Execution time Memory
1073400 2024-08-24T14:07:37 Z mimi Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
204 ms 216700 KB
#include <bits/stdc++.h>
//#ifndef ONLINE_JUDGE
//#include <debugging.h>
//#endif
#define pp push_back
#define all(x) (x).begin(),(x).end()
#define dbg(v)  cout << "Line(" << __LINE__ << ") -> " << #v << " = " << (v) << endl;
using namespace std;
const char el ='\n';
const char sp = ' ';
const int maxn = 2e6+5;
int n,m;
string s[maxn];

int binsearch(const string& a)
{
    int l = 1, r = n, mid;
    while(l<=r)
    {
        mid = (l+r)/2;
        int flag = 0;
        for(int i=0;i<a.size();++i){
            if(i>=s[mid].size() or s[mid][i]!=a[i]){
                if(a[i]>s[mid][i])  flag = 1;
                else                flag = 2;
                break;
            }
        }
        if(flag==1)    l = mid + 1;
        else           r = mid - 1;
    }
    return l;
}

int binsearch1(const string& a)
{
    int l = 1, r = n, mid;
    while(l<=r)
    {
        mid = (l+r)/2;
        int flag = 0;
        for(int i=0;i<a.size();++i){
            if(i>=s[mid].size() or s[mid][i]!=a[i]){
                if(a[i]>s[mid][i])  flag = 1;
                else                flag = 2;
                break;
            }
        }
        if(flag==1 or flag ==0)    l = mid + 1;
        else                       r = mid - 1;
    }
    return r;
}

struct Trie
{
    int trie[maxn][5],curnode = 0;
    vector <int> id[maxn];

    void add(const string& a, int id_)
    {
        int node = 0;
        for(char c:a)
        {
            if(!trie[node][c-'a'])  trie[node][c-'a'] = ++curnode;
            node = trie[node][c-'a'];
            id[node].pp(id_);
        }
    }

    int cal(const string& a, int l, int r)
    {
        int node = 0;
        for(char c:a)
        {
            if(!trie[node][c-'a'])  return 0;
            node = trie[node][c-'a'];
        }
        int le = lower_bound(all(id[node]),l) - id[node].begin();
        int ri = upper_bound(all(id[node]),r) - id[node].begin();
        return ri - le;
    }
}f;

char cal(char c)
{
    if(c=='A')  return 'a';
    if(c=='U')  return 'b';
    if(c=='G')  return 'c';
    if(c=='C')  return 'd';
}

void input()
{
    cin>>n>>m;
    for(int i=1;i<=n;++i)
    {
        cin>>s[i];
        for(int j=0;j<s[i].size();++j)  s[i][j] = cal(s[i][j]);
    }
    sort(s+1,s+n+1);
//    for(int i=1;i<=n;++i)   cout<<i<<sp<<s[i]<<endl;
    for(int i=1;i<=n;++i)
    {
        string tmp = s[i];
        reverse(all(tmp));
        f.add(tmp,i);
    }
    for(int i=0;i<=f.curnode;++i) sort(all(f.id[i]));
}

void solve()
{
    string p,q;
    for(int i=1;i<=m;++i)
    {
        cin>>p>>q;
        for(int j=0;j<p.size();++j) p[j] = cal(p[j]);
        for(int j=0;j<q.size();++j) q[j] = cal(q[j]);
        reverse(all(q));
        int l = binsearch(p), r = binsearch1(p);
//        cout<<l<<sp<<r<<endl;
        cout<<f.cal(q,l,r)<<el;
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    input();
    solve();
    return 0;
}

Compilation message

selling_rna.cpp: In function 'int binsearch(const string&)':
selling_rna.cpp:22:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |         for(int i=0;i<a.size();++i){
      |                     ~^~~~~~~~~
selling_rna.cpp:23:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |             if(i>=s[mid].size() or s[mid][i]!=a[i]){
      |                ~^~~~~~~~~~~~~~~
selling_rna.cpp: In function 'int binsearch1(const string&)':
selling_rna.cpp:42:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |         for(int i=0;i<a.size();++i){
      |                     ~^~~~~~~~~
selling_rna.cpp:43:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |             if(i>=s[mid].size() or s[mid][i]!=a[i]){
      |                ~^~~~~~~~~~~~~~~
selling_rna.cpp: In function 'void input()':
selling_rna.cpp:99:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |         for(int j=0;j<s[i].size();++j)  s[i][j] = cal(s[i][j]);
      |                     ~^~~~~~~~~~~~
selling_rna.cpp: In function 'void solve()':
selling_rna.cpp:118:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |         for(int j=0;j<p.size();++j) p[j] = cal(p[j]);
      |                     ~^~~~~~~~~
selling_rna.cpp:119:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |         for(int j=0;j<q.size();++j) q[j] = cal(q[j]);
      |                     ~^~~~~~~~~
selling_rna.cpp: In function 'char cal(char)':
selling_rna.cpp:91:1: warning: control reaches end of non-void function [-Wreturn-type]
   91 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 26 ms 111960 KB Output is correct
2 Correct 26 ms 111964 KB Output is correct
3 Correct 25 ms 111980 KB Output is correct
4 Correct 27 ms 111964 KB Output is correct
5 Correct 26 ms 111952 KB Output is correct
6 Correct 25 ms 111964 KB Output is correct
7 Correct 25 ms 112088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 182 ms 216700 KB Output is correct
2 Correct 171 ms 212564 KB Output is correct
3 Correct 78 ms 129620 KB Output is correct
4 Correct 76 ms 129108 KB Output is correct
5 Correct 115 ms 178004 KB Output is correct
6 Correct 116 ms 178764 KB Output is correct
7 Correct 93 ms 127572 KB Output is correct
8 Correct 156 ms 161956 KB Output is correct
9 Correct 133 ms 155892 KB Output is correct
10 Correct 108 ms 152408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 113492 KB Output is correct
2 Correct 46 ms 113228 KB Output is correct
3 Correct 56 ms 113144 KB Output is correct
4 Correct 45 ms 112984 KB Output is correct
5 Correct 45 ms 112976 KB Output is correct
6 Correct 51 ms 113232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 111960 KB Output is correct
2 Correct 26 ms 111964 KB Output is correct
3 Correct 25 ms 111980 KB Output is correct
4 Correct 27 ms 111964 KB Output is correct
5 Correct 26 ms 111952 KB Output is correct
6 Correct 25 ms 111964 KB Output is correct
7 Correct 25 ms 112088 KB Output is correct
8 Correct 182 ms 216700 KB Output is correct
9 Correct 171 ms 212564 KB Output is correct
10 Correct 78 ms 129620 KB Output is correct
11 Correct 76 ms 129108 KB Output is correct
12 Correct 115 ms 178004 KB Output is correct
13 Correct 116 ms 178764 KB Output is correct
14 Correct 93 ms 127572 KB Output is correct
15 Correct 156 ms 161956 KB Output is correct
16 Correct 133 ms 155892 KB Output is correct
17 Correct 108 ms 152408 KB Output is correct
18 Correct 52 ms 113492 KB Output is correct
19 Correct 46 ms 113228 KB Output is correct
20 Correct 56 ms 113144 KB Output is correct
21 Correct 45 ms 112984 KB Output is correct
22 Correct 45 ms 112976 KB Output is correct
23 Correct 51 ms 113232 KB Output is correct
24 Correct 186 ms 200024 KB Output is correct
25 Correct 204 ms 200272 KB Output is correct
26 Correct 178 ms 198736 KB Output is correct
27 Correct 85 ms 128624 KB Output is correct
28 Correct 201 ms 138256 KB Output is correct
29 Correct 116 ms 120648 KB Output is correct