#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1000 * 1000 * 1000;
const ll LINF = (ll)INF * INF;
#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define msub(a, b) ((mod + ((ll)(a) - (b)) % mod) % mod)
#define mdiv(a, b) ((ll)(a) * poww((b), mod - 2) % mod)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'
#define MAXN 100100
#define MAXL 2000100
int n, m;
vector<int> s[MAXN];
int sttime[MAXL], fntime[MAXL], timer;
int trie[MAXL][4];
int tn = 1;
int suftrie[MAXL][4];
int stn = 1;
int strend[MAXN];
vector<int> sts[MAXL];
inline void str2vec(const string &s, vector<int> &v)
{
for (char c : s)
{
if (c == 'A')
v.pb(0);
else if (c == 'G')
v.pb(1);
else if (c == 'C')
v.pb(2);
else // U
v.pb(3);
}
}
void add(const vector<int> &s, bool rev, int ind, int cld[MAXL][4])
{
int &cnt = (rev ? stn : tn);
int dir = (rev ? -1 : 1);
int beg = 0;
int end = s.size() - 1;
if (rev)
swap(beg, end);
int v = 1;
for (int i = beg; (rev && i >= end) || (!rev && i <= end); i += dir)
{
if (rev)
sts[v].pb(sttime[strend[ind]]);
int u = s[i];
if (!cld[v][u])
{
cnt++;
cld[v][u] = cnt;
}
v = cld[v][u];
}
if (rev)
sts[v].pb(sttime[strend[ind]]);
if (!rev)
strend[ind] = v;
}
void dfs(int v)
{
sttime[v] = timer++;
FOR(i, 0, 4)
{
if (!trie[v][i])
continue;
dfs(trie[v][i]);
}
fntime[v] = timer;
}
int _find(const vector<int> &s, int trie[MAXL][4])
{
int v = 1;
for (int u : s)
{
if (!trie[v][u])
{
return -1;
}
v = trie[v][u];
}
return v;
}
int32_t main(void)
{
fast_io;
cin >> n >> m;
FOR(i, 0, n)
{
string si;
cin >> si;
str2vec(si, s[i]);
add(s[i], false, i, trie);
}
dfs(1);
FOR(i, 0, n)
{
add(s[i], true, i, suftrie);
}
FOR(i, 1, stn + 1)
{
sort(all(sts[i]));
}
while (m--)
{
string p, q;
cin >> p >> q;
vector<int> pv, qv;
str2vec(p, pv);
str2vec(q, qv);
reverse(all(qv));
int lv = _find(pv, trie);
int rv = _find(qv, suftrie);
if (lv < 0 || rv < 0)
{
cout << 0 << endl;
continue;
}
auto it1 = lower_bound(all(sts[rv]), sttime[lv]);
auto it2 = lower_bound(all(sts[rv]), fntime[lv]);
int ans = it2 - it1;
cout << ans << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
49772 KB |
Output is correct |
2 |
Correct |
31 ms |
49644 KB |
Output is correct |
3 |
Correct |
29 ms |
49644 KB |
Output is correct |
4 |
Correct |
30 ms |
49644 KB |
Output is correct |
5 |
Correct |
29 ms |
49644 KB |
Output is correct |
6 |
Correct |
29 ms |
49644 KB |
Output is correct |
7 |
Correct |
29 ms |
49644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
272 ms |
153196 KB |
Output is correct |
2 |
Correct |
273 ms |
148844 KB |
Output is correct |
3 |
Correct |
293 ms |
118124 KB |
Output is correct |
4 |
Correct |
291 ms |
116076 KB |
Output is correct |
5 |
Correct |
261 ms |
142316 KB |
Output is correct |
6 |
Correct |
265 ms |
143596 KB |
Output is correct |
7 |
Correct |
130 ms |
66796 KB |
Output is correct |
8 |
Correct |
269 ms |
116076 KB |
Output is correct |
9 |
Correct |
264 ms |
107500 KB |
Output is correct |
10 |
Correct |
234 ms |
106476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
52608 KB |
Output is correct |
2 |
Correct |
61 ms |
52076 KB |
Output is correct |
3 |
Correct |
67 ms |
52328 KB |
Output is correct |
4 |
Correct |
60 ms |
51948 KB |
Output is correct |
5 |
Correct |
69 ms |
52076 KB |
Output is correct |
6 |
Correct |
68 ms |
52328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
49772 KB |
Output is correct |
2 |
Correct |
31 ms |
49644 KB |
Output is correct |
3 |
Correct |
29 ms |
49644 KB |
Output is correct |
4 |
Correct |
30 ms |
49644 KB |
Output is correct |
5 |
Correct |
29 ms |
49644 KB |
Output is correct |
6 |
Correct |
29 ms |
49644 KB |
Output is correct |
7 |
Correct |
29 ms |
49644 KB |
Output is correct |
8 |
Correct |
272 ms |
153196 KB |
Output is correct |
9 |
Correct |
273 ms |
148844 KB |
Output is correct |
10 |
Correct |
293 ms |
118124 KB |
Output is correct |
11 |
Correct |
291 ms |
116076 KB |
Output is correct |
12 |
Correct |
261 ms |
142316 KB |
Output is correct |
13 |
Correct |
265 ms |
143596 KB |
Output is correct |
14 |
Correct |
130 ms |
66796 KB |
Output is correct |
15 |
Correct |
269 ms |
116076 KB |
Output is correct |
16 |
Correct |
264 ms |
107500 KB |
Output is correct |
17 |
Correct |
234 ms |
106476 KB |
Output is correct |
18 |
Correct |
69 ms |
52608 KB |
Output is correct |
19 |
Correct |
61 ms |
52076 KB |
Output is correct |
20 |
Correct |
67 ms |
52328 KB |
Output is correct |
21 |
Correct |
60 ms |
51948 KB |
Output is correct |
22 |
Correct |
69 ms |
52076 KB |
Output is correct |
23 |
Correct |
68 ms |
52328 KB |
Output is correct |
24 |
Correct |
277 ms |
137068 KB |
Output is correct |
25 |
Correct |
298 ms |
137324 KB |
Output is correct |
26 |
Correct |
268 ms |
136300 KB |
Output is correct |
27 |
Correct |
313 ms |
109932 KB |
Output is correct |
28 |
Correct |
319 ms |
82276 KB |
Output is correct |
29 |
Correct |
196 ms |
63708 KB |
Output is correct |