Submission #78394

# Submission time Handle Problem Language Result Execution time Memory
78394 2018-10-04T18:35:36 Z MladenP Selling RNA Strands (JOI16_selling_rna) C++17
35 / 100
1500 ms 128748 KB
#include<bits/stdc++.h>
#define STIZE(x) fprintf(stderr, "STIZE%d\n", x);
#define PRINT(x) fprintf(stderr, "%s = %d\n", #x, x);
#define NL(x) printf("%c", " \n"[(x)]);
#define lld long long
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define mid (l+r)/2
#define endl '\n'
#define all(a) begin(a),end(a)
#define sz(a) int((a).size())
#define LINF 1000000000000000LL
#define INF 1000000000
#define EPS 1e-9
using namespace std;
#define MAXL 2000000
#define MAXN 100010
#define MOD 1000000007
int trie[MAXL][4], siz[MAXL], idx[MAXL], nodes, timer, hshidx, key, f[300];
vector<int> gde[MAXL], celi;
map<int,int> cudo;
lld hsh[MAXN], hsh1[MAXN], st[MAXN];
void ubaci(int node, string &s, int idx) {
    if(idx == sz(s)) return;
    int c = f[s[idx]];
    if(trie[node][c] == 0) trie[node][c] = ++nodes;
    gde[hsh[sz(s)-1-idx]].pb(node); ///zeznuta linija
    ubaci(trie[node][c], s, idx+1);
}
int DFS(int node) {
    idx[node] = ++timer;
    for(int i = 0; i < 4; i++) if(trie[node][i]) siz[node] += DFS(trie[node][i]);
    return ++siz[node];
}
void hashuj(string &s, lld hsh[]) {
    fill(hsh, hsh+sz(s)+5, 0);
    for(int i = 0; i < sz(s); i++) {
        hsh[i] += st[i]*(f[s[i]]+1);
        if(hsh[i] >= MOD) hsh[i] %= MOD;
        hsh[i+1] += hsh[i];
        if(cudo[hsh[i]] == 0) cudo[hsh[i]] = ++hshidx;
        hsh[i] = cudo[hsh[i]];
    }
}
void hashuj1(string &s, lld hsh[]) {
    fill(hsh, hsh+sz(s)+5, 0);
    for(int i = 0; i < sz(s); i++) {
        hsh[i] += st[i]*(f[s[i]]+1);
        if(hsh[i] >= MOD) hsh[i] %= MOD;
        hsh[i+1] += hsh[i];
    }
}
int query(int node, string &s, int idxx) {
    if(idxx == sz(s)) return node;
    int c = f[s[idxx]];
    if(trie[node][c] == 0) return -1;
    return query(trie[node][c], s, idxx+1);
}
int main() {
    ios::sync_with_stdio(false);cin.tie(0);
    int N, M; cin >> N >> M;
    f['A'] = 0, f['G'] = 1, f['C'] = 2, f['U'] = 3, st[0] = 1;
    for(int i = 1; i < MAXN; i++) st[i] = (st[i-1]*5 >= MOD ? (st[i-1]*5)%MOD : st[i-1]*5);
    for(int i = 1; i <= N; i++) {
        string s, rev; cin >> s;
        rev = s; reverse(all(rev));
        hashuj(rev, hsh);
        ubaci(0, s, 0);
        hashuj1(rev, hsh);
        celi.pb(hsh[sz(s)-1]);
    }
    sort(all(celi));
    //for(auto x : celi) PRINT(x);
    DFS(0);
    for(int i = 1; i <= hshidx; i++) {
        for(int j = 0; j < sz(gde[i]); j++) {
            gde[i][j] = idx[gde[i][j]];
        }
        sort(all(gde[i]));
    }
    for(int q = 0; q < M; q++) {
        //PRINT(q);
        int rez = 0;
        string P, Q; cin >> P >> Q;
        reverse(all(Q));
        hashuj(Q, hsh); key = hsh[sz(Q)-1];
        int koji = query(0, P, 0);
        reverse(all(P));
        hashuj1(P, hsh1); hashuj1(Q, hsh);
        for(int i = 0; i < sz(Q); i++) {
            if(hsh[sz(Q)-1] - hsh[sz(Q)-2-i] == hsh1[i]*st[sz(Q)-i-1]) {
                lld cur = hsh1[sz(P)-1]*st[sz(Q)-i-1] + hsh[sz(Q)-2-i];
                //PRINT(cur);
                rez += upper_bound(all(celi), cur) - lower_bound(all(celi), cur);
            }
        }
        if(koji == -1) rez = 0;
        if(koji != -1) rez += upper_bound(all(gde[key]), idx[koji]+siz[koji]-1) - lower_bound(all(gde[key]), idx[koji]);
        cout << rez << endl;
    }
    return 0;
}

Compilation message

selling_rna.cpp: In function 'void ubaci(int, std::__cxx11::string&, int)':
selling_rna.cpp:27:21: warning: array subscript has type 'char' [-Wchar-subscripts]
     int c = f[s[idx]];
                     ^
selling_rna.cpp: In function 'void hashuj(std::__cxx11::string&, long long int*)':
selling_rna.cpp:40:32: warning: array subscript has type 'char' [-Wchar-subscripts]
         hsh[i] += st[i]*(f[s[i]]+1);
                                ^
selling_rna.cpp: In function 'void hashuj1(std::__cxx11::string&, long long int*)':
selling_rna.cpp:50:32: warning: array subscript has type 'char' [-Wchar-subscripts]
         hsh[i] += st[i]*(f[s[i]]+1);
                                ^
selling_rna.cpp: In function 'int query(int, std::__cxx11::string&, int)':
selling_rna.cpp:57:22: warning: array subscript has type 'char' [-Wchar-subscripts]
     int c = f[s[idxx]];
                      ^
# Verdict Execution time Memory Grader output
1 Correct 45 ms 48120 KB Output is correct
2 Correct 52 ms 48252 KB Output is correct
3 Correct 66 ms 48332 KB Output is correct
4 Correct 46 ms 48332 KB Output is correct
5 Correct 46 ms 48332 KB Output is correct
6 Correct 45 ms 48344 KB Output is correct
7 Correct 45 ms 48344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1578 ms 128748 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 86 ms 128748 KB Output is correct
2 Correct 91 ms 128748 KB Output is correct
3 Correct 87 ms 128748 KB Output is correct
4 Correct 77 ms 128748 KB Output is correct
5 Correct 82 ms 128748 KB Output is correct
6 Correct 87 ms 128748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 48120 KB Output is correct
2 Correct 52 ms 48252 KB Output is correct
3 Correct 66 ms 48332 KB Output is correct
4 Correct 46 ms 48332 KB Output is correct
5 Correct 46 ms 48332 KB Output is correct
6 Correct 45 ms 48344 KB Output is correct
7 Correct 45 ms 48344 KB Output is correct
8 Execution timed out 1578 ms 128748 KB Time limit exceeded
9 Halted 0 ms 0 KB -