Submission #866752

#TimeUsernameProblemLanguageResultExecution timeMemory
866752tradzSelling RNA Strands (JOI16_selling_rna)C++14
100 / 100
226 ms373360 KiB
#include <bits/stdc++.h>
using namespace std;

#define For(i, a, b) for (int i = a; i <= b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)
#define TASK ""
#define fi first
#define se second
#define iii pair<ii, int>
const int N = 1e6 + 7;
const int oo = 1e18;

typedef pair<int, int> ii;
const int maxn = 2e6 + 10;
int n, m, tim[3];
string s[100010];
struct data {
    int child[27], in, ou;
};
vector<int> c[maxn];
data a[maxn], b[maxn];

void update(int w) {
    string sa = s[w];
    int u = 0;
    for (int i = 0; i < sa.size(); i++) {
        if (a[u].child[sa[i] - 'A' + 1] == 0) {
            a[u].child[sa[i] - 'A' + 1] = ++tim[1];
            a[tim[1]].in = w;
        }
        u = a[u].child[sa[i] - 'A' + 1];
        a[u].ou = w;
    }
    u = 0;
    for (int i = sa.size() - 1; i >= 0; i--) {
        if (b[u].child[sa[i] - 'A' + 1] == 0) {
            b[u].child[sa[i] - 'A' + 1] = ++tim[2];
        }
        u = b[u].child[sa[i] - 'A' + 1];

        c[u].push_back(w);
    }
}

void get(string sa, string sb) {
    int u = 0, ua = 0;
    for (int i = 0; i < sa.size(); i++) {
        if (a[u].child[sa[i] - 'A' + 1]== 0) {
            cout << "0\n";
            return;
        }
        u = a[u].child[sa[i] - 'A' + 1];
    }

    for (int i = sb.size() - 1; i >= 0; i--) {
        if (b[ua].child[sb[i] - 'A' + 1] == 0) {
            cout << "0\n";
            return;
        }
        ua = b[ua].child[sb[i] - 'A' + 1];
    }

    c[ua].push_back(1000000);
    int w = lower_bound(c[ua].begin(), c[ua].end(), a[u].in) - c[ua].begin();
    int wa = upper_bound(c[ua].begin(), c[ua].end(), a[u].ou) - c[ua].begin();
    wa--;

    if (w > wa) {
        cout << "0\n";
        return;
    }
    cout << wa - w + 1 << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    if (fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }

    cin >> n >> m;

    For(i, 1, n) {
        cin >> s[i];
    }

    sort(s + 1, s + n + 1);

    For(i, 1, n) {
        update(i);
    }

    For(i, 1, m) {
        cin >> s[1] >> s[2];
        get(s[1], s[2]);
    }

    return 0;
}

Compilation message (stderr)

selling_rna.cpp:11:16: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
   11 | const int oo = 1e18;
      |                ^~~~
selling_rna.cpp: In function 'void update(int)':
selling_rna.cpp:26:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |     for (int i = 0; i < sa.size(); i++) {
      |                     ~~^~~~~~~~~~~
selling_rna.cpp: In function 'void get(std::string, std::string)':
selling_rna.cpp:47:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |     for (int i = 0; i < sa.size(); i++) {
      |                     ~~^~~~~~~~~~~
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:80:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:81:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...