#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 2000001
#define MAXN 100010
#define MOD 29996224275833LL
int trie[MAXL][4], siz[MAXL], idx[MAXL], nodes, timer, hshidx, key, f[300];
bool sorted[MAXL];
vector<int> gde[MAXL], inNode[MAXL];
unordered_map<int,int> cudo, celi;
lld hsh[MAXN], hsh1[MAXN], st[MAXN];
string P, Q, S;
inline void ubaci(string &s) {
int node = 0;
for(int idx = 0; idx < sz(s); idx++) {
int c = f[s[idx]];
if(trie[node][c] == 0) trie[node][c] = ++nodes;
inNode[node].pb(hsh[sz(s)-1-idx]);
node = trie[node][c];
}
}
stack<int> dfsq;
inline int DFS(int node) {
dfsq.push(node);
while(!dfsq.empty()) {
int top = dfsq.top();
if(idx[top] == 0) {
idx[top] = ++timer;
for(auto x : inNode[top]) gde[x].pb(idx[top]);
for(int i = 0; i < 4; i++) if(trie[top][i]) dfsq.push(trie[top][i]);
} else {
dfsq.pop();
siz[top] = 1;
for(int i = 0; i < 4; i++) if(trie[top][i]) siz[top] += siz[trie[top][i]];
}
}
}
inline void hashuj(string &s, lld hsh[]) {
fill(hsh, hsh+sz(s)+5, 0);
hsh[0] = 0;
for(int i = sz(s)-1, ob = 0; i >= 0; i--, ob++) {
hsh[ob] += st[ob]*(f[s[i]]+1);
//hsh[ob+1] = 0;
hsh[ob+1] = hsh[ob];
if(hsh[ob] >= MOD) hsh[ob] %= MOD;
//hsh[ob+1] = hsh[ob];
if(i == 0) celi[hsh[ob]]++;
if(cudo[hsh[ob]] == 0) cudo[hsh[ob]] = ++hshidx;
hsh[ob] = cudo[hsh[ob]];
}
}
inline void hashuj1(string &s, lld hsh[]) {
fill(hsh, hsh+sz(s)+5, 0);
hsh[0] = 0;
for(int i = sz(s)-1, ob = 0; i >= 0; i--, ob++) {
hsh[ob] += st[ob]*(f[s[i]]+1);
//hsh[ob+1] = 0;
hsh[ob+1] = hsh[ob];
//PRINT(hsh[ob]);
if(hsh[ob] >= MOD) hsh[ob] %= MOD;
//hsh[ob+1] = hsh[ob];
}
}
inline int query(string &s) {
int node = 0;
for(int idxx = 0; idxx < sz(s); idxx++) {
int c = f[s[idxx]];
if(trie[node][c] == 0) return -1;
node = trie[node][c];
}
return node;
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.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++) {
cin >> S;
hashuj(S, hsh);
ubaci(S);
}
DFS(0);
for(int q = 0; q < M; q++) {
int rez = 0;
cin >> P >> Q;
int lenQ = sz(Q), lenP = sz(P);
hashuj1(Q, hsh); key = cudo[hsh[lenQ-1]];
int koji = query(P);
if(key == 0 || koji == -1) {
cout << 0 << endl;
continue;
}
hashuj1(P, hsh1);
for(int i = 0; i < lenQ; i++) {
if(hsh[lenQ-1] - hsh[lenQ-2-i] == hsh1[i]*st[lenQ-i-1]) {
lld cur = hsh1[lenP-1]*st[lenQ-i-1] + hsh[lenQ-2-i];
rez += celi[cur];
}
}
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(std::__cxx11::string&)':
selling_rna.cpp:30:25: warning: array subscript has type 'char' [-Wchar-subscripts]
int c = f[s[idx]];
^
selling_rna.cpp: In function 'int DFS(int)':
selling_rna.cpp:51:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
selling_rna.cpp: In function 'void hashuj(std::__cxx11::string&, long long int*)':
selling_rna.cpp:56:34: warning: array subscript has type 'char' [-Wchar-subscripts]
hsh[ob] += st[ob]*(f[s[i]]+1);
^
selling_rna.cpp: In function 'void hashuj1(std::__cxx11::string&, long long int*)':
selling_rna.cpp:70:34: warning: array subscript has type 'char' [-Wchar-subscripts]
hsh[ob] += st[ob]*(f[s[i]]+1);
^
selling_rna.cpp: In function 'int query(std::__cxx11::string&)':
selling_rna.cpp:81:26: warning: array subscript has type 'char' [-Wchar-subscripts]
int c = f[s[idxx]];
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
95096 KB |
Output is correct |
2 |
Correct |
80 ms |
95140 KB |
Output is correct |
3 |
Correct |
88 ms |
95104 KB |
Output is correct |
4 |
Correct |
80 ms |
95068 KB |
Output is correct |
5 |
Correct |
82 ms |
95100 KB |
Output is correct |
6 |
Correct |
80 ms |
95224 KB |
Output is correct |
7 |
Correct |
78 ms |
95096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1565 ms |
207400 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
96440 KB |
Output is correct |
2 |
Correct |
103 ms |
96604 KB |
Output is correct |
3 |
Correct |
101 ms |
96500 KB |
Output is correct |
4 |
Correct |
98 ms |
96244 KB |
Output is correct |
5 |
Correct |
99 ms |
96632 KB |
Output is correct |
6 |
Correct |
104 ms |
96628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
95096 KB |
Output is correct |
2 |
Correct |
80 ms |
95140 KB |
Output is correct |
3 |
Correct |
88 ms |
95104 KB |
Output is correct |
4 |
Correct |
80 ms |
95068 KB |
Output is correct |
5 |
Correct |
82 ms |
95100 KB |
Output is correct |
6 |
Correct |
80 ms |
95224 KB |
Output is correct |
7 |
Correct |
78 ms |
95096 KB |
Output is correct |
8 |
Execution timed out |
1565 ms |
207400 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |