#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;
vector<int> gde[MAXL];
map<int,int> cudo;
lld hsh[MAXN], rez, key;
int f(char c) {
if(c == 'A') return 0;
if(c == 'G') return 1;
if(c == 'C') return 2;
if(c == 'U') return 3;
}
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) {
siz[node] = 1;
idx[node] = ++timer;
if(trie[node][0]) siz[node] += DFS(trie[node][0]);
if(trie[node][1]) siz[node] += DFS(trie[node][1]);
if(trie[node][2]) siz[node] += DFS(trie[node][2]);
if(trie[node][3]) siz[node] += DFS(trie[node][3]);
return siz[node];
}
void hashuj(string &s) {
fill(hsh, hsh+sz(s)+5, 0);
lld st = 5;
hsh[0] = f(s[0])+1;
for(int i = 1; i < sz(s); i++) {
hsh[i] = hsh[i-1] + st*(f(s[i])+1);
st *= 5;
if(hsh[i] >= MOD) hsh[i] %= MOD;
if(st >= MOD) st %= MOD;
}
for(int i = 0; i < sz(s); i++) {
if(cudo[hsh[i]] == 0) cudo[hsh[i]] = ++hshidx;
hsh[i] = cudo[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;
rez += upper_bound(all(gde[key]), idx[node]) - lower_bound(all(gde[key]), idx[node]);
return query(trie[node][c], s, idxx+1);
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);
//freopen("gdegod.txt", "w", stdout);
//freopen("in.txt", "r", stdin);
int N, M; cin >> N >> M;
for(int i = 1; i <= N; i++) {
string s, rev; cin >> s;
rev = s; reverse(all(rev));
hashuj(rev);
ubaci(0, s, 0);
}
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]));
}
// STIZE(3);
for(int q = 0; q < M; q++) {
rez = 0;
string P, Q; cin >> P >> Q;
reverse(all(Q)); hashuj(Q); key = hsh[sz(Q)-1];
int koji = query(0, P, 0);
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;
}
// STIZE(4);
return 0;
}
Compilation message
selling_rna.cpp: In function 'int f(char)':
selling_rna.cpp:30:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
45 ms |
47352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1559 ms |
119984 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
119984 KB |
Output is correct |
2 |
Incorrect |
91 ms |
119984 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
45 ms |
47352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |