Submission #1273879

#TimeUsernameProblemLanguageResultExecution timeMemory
1273879baotoan655Selling RNA Strands (JOI16_selling_rna)C++17
100 / 100
249 ms192316 KiB
#include <bits/stdc++.h>
#define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
using namespace std;

int con(char x) {
    if(x == 'A') return 0;
    if(x == 'G') return 1;
    if(x == 'C') return 2;
    return 3;
}

const int N = 1e5 + 5;
int n, q;
string str[N];
struct node1 {
    int mn, mx;
    int nxt[4];
    node1() {
        fill(nxt, nxt + 4, -1);
        mn = 1e9;
        mx = 0;
    }
};
struct node2 {
    int nxt[4];
    vector<int> ve;
    node2() {
        fill(nxt, nxt + 4, -1);
        ve.clear();
    }
};
vector<node1> nod1;
vector<node2> nod2;
void add1(const string &s, int id) {
    int cur = 0;
    for(char ch : s) {
        int c = con(ch);
        if(nod1[cur].nxt[c] == -1) nod1[cur].nxt[c] = nod1.size();
        nod1.emplace_back();
        cur = nod1[cur].nxt[c];
        nod1[cur].mn = min(nod1[cur].mn, id);
        nod1[cur].mx = max(nod1[cur].mx, id);
    }
}
void add2(const string& s, int id) {
    int cur = 0;
    for(char ch : s) {
        int c = con(ch);
        if(nod2[cur].nxt[c] == -1) nod2[cur].nxt[c]= nod2.size();
        nod2.emplace_back();
        cur = nod2[cur].nxt[c];
        nod2[cur].ve.emplace_back(id);
    }
}
pair<int, int> query(const string &s) {
    int cur = 0;
    for(char ch : s) {
        int c = con(ch);
        if(nod1[cur].nxt[c] == -1) return make_pair(0, -1);
        cur = nod1[cur].nxt[c];
    }
    return make_pair(nod1[cur].mn, nod1[cur].mx);
}
int get(const string &s, int l, int r) {
    int cur = 0;
    for(char ch : s) {
        int c = con(ch);
        if(nod2[cur].nxt[c] == -1) return 0;
        cur = nod2[cur].nxt[c];
    }
    return upper_bound(nod2[cur].ve.begin(), nod2[cur].ve.end(), r)  - lower_bound(nod2[cur].ve.begin(), nod2[cur].ve.end(), l);
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    file("A") else file("task");
    nod1.emplace_back();
    nod2.emplace_back();
    cin >> n >> q;
    for(int i = 1; i <= n; ++i) cin >> str[i];
    sort(str + 1, str + n + 1);
    for(int i = 1; i <= n; ++i) {
        add1(str[i], i);
        reverse(str[i].begin(), str[i].end());
        add2(str[i], i);
    }
    for(int i = 0; i < (int)nod2.size(); ++i) {
        sort(nod2[i].ve.begin(), nod2[i].ve.end());
    }
    while(q--) {
        string s, t;
        cin >> s >> t;
        auto [l, r] = query(s);
        if(l > r) {
            cout << 0 << '\n';
            continue;
        }
        reverse(t.begin(), t.end());
        cout << get(t, l, r) << '\n';
    }
    return 0;
}

Compilation message (stderr)

selling_rna.cpp: In function 'int main()':
selling_rna.cpp:2:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:76:5: note: in expansion of macro 'file'
   76 |     file("A") else file("task");
      |     ^~~~
selling_rna.cpp:2:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:76:5: note: in expansion of macro 'file'
   76 |     file("A") else file("task");
      |     ^~~~
selling_rna.cpp:2:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:76:20: note: in expansion of macro 'file'
   76 |     file("A") else file("task");
      |                    ^~~~
selling_rna.cpp:2:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:76:20: note: in expansion of macro 'file'
   76 |     file("A") else file("task");
      |                    ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...