답안 #64459

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
64459 2018-08-04T14:07:04 Z reality Selling RNA Strands (JOI16_selling_rna) C++17
35 / 100
1500 ms 286992 KB
#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
#define ull unsigned ll 
#define md ull
template < class T > ostream& operator<<(ostream& stream, const vector<T> v){ stream << "[ "; for (int i=0; i<(int)v.size(); i++) stream << v[i] << " "; stream << "]"; return stream;}
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
struct trie {
    trie * t[4];
    vi p;
    trie(void) {
        memset(t,0,sizeof(t));
    }
};
const int mod1 = 1e9 + 7;
const int mod2 = 1e9 + 9;
const int base = 5;
typedef trie * tr;
const int N = 2e5 + 5;
const int LG = 18;
string s[N];
int Log[N];
vector < md > h[N][LG + 1];
md H[LG + 1][N];
md pw[N];
tr root = new trie();
void dfs(tr rt,int dep = -1) {
    if (!rt)
        return;
    sort(all(rt->p),[&](int a,int b) {
            int sz1 = s[a].length();
            int sz2 = s[b].length();
            for (int t = Log[min(sz1,sz2)];t >= 0;--t)
                if (sz1 - (1 << t) >= 0 && sz2 - (1 << t) >= 0 && h[a][t][sz1 - (1 << t)] == h[b][t][sz2 - (1 << t)])
                    sz1 -= (1 << t),sz2 -= (1 << t);
            --sz1;--sz2;
            if (sz1 < 0 || sz2 < 0)
                return sz1 < sz2;
            return s[a][sz1] < s[b][sz2];
        });
    for (int i = 0;i < 4;++i)
        dfs(rt->t[i],dep + 1);
}
int main(void) {
    int n,m;
    cin>>n>>m;
    pw[0] = 1;
    for (int i = 1;i <= 1e5 + 5;++i)
        pw[i] = pw[i - 1] * base;
    for (int i = 2;i <= 1e5 + 5;++i)
        Log[i] = Log[i / 2] + 1;
    for (int i = 1;i <= n;++i) {
        cin>>s[i];
        for (auto & it : s[i]) {
            if (it == 'A')
                it = 1;
            else
            if (it == 'G')
                it = 2;
            else
            if (it == 'C')
                it = 3;
            else
                it = 4;
        }
        tr cnt = root;
        int sz = s[i].length();
        for (int j = 0;j < sz;++j) {
            int ch = s[i][j] - 1;
            if (!cnt->t[ch])
                cnt->t[ch] = new trie();
            cnt = cnt->t[ch];
            cnt->p.pb(i);
        }
        for (int k = 0;sz >> k;++k) {
            h[i][k].resize(sz);
        }
        for (int j = 0;j < sz;++j)
            h[i][0][j] = s[i][j];
        for (int k = 1;sz >> k;++k)
            for (int j = 0;j + (1 << k) <= sz;++j)
                h[i][k][j] = h[i][k - 1][j] + pw[1 << (k - 1)] * h[i][k - 1][j + (1 << (k - 1))];
    }
    dfs(root);
    while (m --) {
        string a,b;
        cin>>a>>b;
        tr cnt = root;
        for (auto &it : a) {
            if (it == 'A')
                it = 1;
            else
            if (it == 'G')
                it = 2;
            else
            if (it == 'C')
                it = 3;
            else 
                it = 4;
        }
        for (auto &it : b) {
            if (it == 'A')
                it = 1;
            else
            if (it == 'G')
                it = 2;
            else
            if (it == 'C')
                it = 3;
            else 
                it = 4;
        }
        for (auto it : a) {
            int ch = it - 1;
            cnt = cnt->t[ch];
            if (!cnt)
                break;
        }
        if (!cnt) {
            cout << "0\n";
            continue;
        }
        int sz = b.length();
        for (int i = 0;i < sz;++i)
            H[0][i] = b[i];
        for (int k = 1;sz >> k;++k)
            for (int i = 0;i + (1 << k) <= sz;++i)
                H[k][i] = H[k - 1][i] + pw[1 << (k - 1)] * H[k - 1][i + (1 << (k - 1))];
        int SZ = cnt->p.size();
        auto ls = [&](int index) {
            assert(0 <= index && index < SZ);
            index = cnt->p[index];
            int sz1 = s[index].length();
            int sz2 = sz;
            for (int t = Log[min(sz1,sz2)];t >= 0;--t)
                if (sz1 - (1 << t) >= 0 && sz2 - (1 << t) >= 0 && H[t][sz2 - (1 << t)] == h[index][t][sz1 - (1 << t)])
                    sz1 -= 1 << t,sz2 -= 1 << t;
            --sz1;--sz2;
            if (sz1 < 0 || sz2 < 0)
                return sz2 > sz1;
            return b[sz2] > s[index][sz1];
        };
        auto eq = [&](int index) {
            assert(0 <= index && index < SZ);
            index = cnt->p[index];
            int sz1 = s[index].length();
            int sz2 = sz;
            for (int t = Log[min(sz1,sz2)];t >= 0;--t)
                if (sz1 - (1 << t) >= 0 && sz2 - (1 << t) >= 0 && H[t][sz2 - (1 << t)] == h[index][t][sz1 - (1 << t)])
                    sz1 -= 1 << t,sz2 -= 1 << t;
            return !sz2;
        };
        int l = -1;
        for (int t = Log[SZ];t >= 0;--t)
            if (l + (1 << t) < SZ && ls(l + (1 << t)))
                l += (1 << t);
        int r = ++l;
        for (int t = Log[SZ];t >= 0;--t)
            if (r + (1 << t) <= SZ && eq(r + (1 << t) - 1))
                r += 1 << t;
        cout << (r - l) << '\n';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 97016 KB Output is correct
2 Correct 95 ms 97260 KB Output is correct
3 Correct 88 ms 97260 KB Output is correct
4 Correct 108 ms 97260 KB Output is correct
5 Correct 101 ms 97260 KB Output is correct
6 Correct 105 ms 97276 KB Output is correct
7 Correct 99 ms 97292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1600 ms 286992 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 391 ms 286992 KB Output is correct
2 Correct 510 ms 286992 KB Output is correct
3 Correct 488 ms 286992 KB Output is correct
4 Correct 255 ms 286992 KB Output is correct
5 Correct 354 ms 286992 KB Output is correct
6 Correct 319 ms 286992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 97016 KB Output is correct
2 Correct 95 ms 97260 KB Output is correct
3 Correct 88 ms 97260 KB Output is correct
4 Correct 108 ms 97260 KB Output is correct
5 Correct 101 ms 97260 KB Output is correct
6 Correct 105 ms 97276 KB Output is correct
7 Correct 99 ms 97292 KB Output is correct
8 Execution timed out 1600 ms 286992 KB Time limit exceeded
9 Halted 0 ms 0 KB -