#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;
int was;
trie(void) {
memset(t,0,sizeof(t));
was = 0;
}
};
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 Sr(tr rt) {
if (!rt)
return;
if (rt->was)
return;
rt->was = 1;
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];
});
}
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))];
}
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;
}
Sr(cnt);
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 |
86 ms |
97016 KB |
Output is correct |
2 |
Correct |
90 ms |
97128 KB |
Output is correct |
3 |
Correct |
93 ms |
97280 KB |
Output is correct |
4 |
Correct |
86 ms |
97280 KB |
Output is correct |
5 |
Correct |
108 ms |
97280 KB |
Output is correct |
6 |
Correct |
94 ms |
97280 KB |
Output is correct |
7 |
Correct |
89 ms |
97280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1555 ms |
287456 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
314 ms |
287456 KB |
Output is correct |
2 |
Correct |
435 ms |
287456 KB |
Output is correct |
3 |
Correct |
427 ms |
287456 KB |
Output is correct |
4 |
Correct |
267 ms |
287456 KB |
Output is correct |
5 |
Correct |
360 ms |
287456 KB |
Output is correct |
6 |
Correct |
342 ms |
287456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
97016 KB |
Output is correct |
2 |
Correct |
90 ms |
97128 KB |
Output is correct |
3 |
Correct |
93 ms |
97280 KB |
Output is correct |
4 |
Correct |
86 ms |
97280 KB |
Output is correct |
5 |
Correct |
108 ms |
97280 KB |
Output is correct |
6 |
Correct |
94 ms |
97280 KB |
Output is correct |
7 |
Correct |
89 ms |
97280 KB |
Output is correct |
8 |
Execution timed out |
1555 ms |
287456 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |