#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()
#pragma warning(disable : 4996)
const int P = 5, N = 1e5 + 5, L = 2e6 + 5, A = 4;
map<char, int> mp_ind = { {'A', 0}, {'U', 1}, {'G', 2}, {'C', 3} };
unordered_map<ll, vector<int>> mp_h[N];
int to[L][A], lb[L], rb[L];
vector<int> en[L];
string s[N];
int z = 1, timer = 0;
void dfs(int node) {
lb[node] = ++timer;
for (int i : en[node]) {
ll h = 0;
for (int j = 0; j < s[i].size(); j++) {
h = h * P + s[i][j];
mp_h[j + 1][h].push_back(lb[node]);
}
}
for (int i = 0; i < A; i++) {
if (to[node][i] != -1) {
dfs(to[node][i]);
rb[node] = rb[to[node][i]];
}
}
rb[node] = timer;
}
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
memset(to, -1, sizeof(to));
for (int i = 0; i < n; i++) {
cin >> s[i];
int v = 0;
for (int j = 0; j < s[i].size(); j++) {
s[i][j] = mp_ind[s[i][j]];
int c = s[i][j];
if (to[v][c] == -1) {
to[v][c] = z++;
}
v = to[v][c];
}
en[v].push_back(i);
reverse(all(s[i]));
}
dfs(0);
for (int i = 0; i < m; i++) {
string p, q;
cin >> p >> q;
reverse(all(q));
ll h = 0;
for (char c : q)
h = h * P + mp_ind[c];
int v = 0;
bool ok = true;
for (int j = 0; j < p.size(); j++) {
int c = mp_ind[p[j]];
if (to[v][c] == -1) {
ok = false;
break;
}
v = to[v][c];
}
int ans = 0;
if (ok) {
ans = upper_bound(all(mp_h[q.size()][h]), rb[v]) - lower_bound(all(mp_h[q.size()][h]), lb[v]);
}
cout << ans << "\n";
}
return 0;
}
Compilation message
selling_rna.cpp:41:0: warning: ignoring #pragma warning [-Wunknown-pragmas]
#pragma warning(disable : 4996)
selling_rna.cpp: In function 'void dfs(int)':
selling_rna.cpp:55:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < s[i].size(); j++) {
~~^~~~~~~~~~~~~
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:85:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < s[i].size(); j++) {
~~^~~~~~~~~~~~~
selling_rna.cpp:110:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < p.size(); j++) {
~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
87288 KB |
Output is correct |
2 |
Correct |
87 ms |
87288 KB |
Output is correct |
3 |
Correct |
77 ms |
87288 KB |
Output is correct |
4 |
Correct |
80 ms |
87416 KB |
Output is correct |
5 |
Correct |
76 ms |
87416 KB |
Output is correct |
6 |
Correct |
82 ms |
87176 KB |
Output is correct |
7 |
Correct |
76 ms |
87188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1573 ms |
237012 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
88308 KB |
Output is correct |
2 |
Correct |
102 ms |
88416 KB |
Output is correct |
3 |
Correct |
102 ms |
88384 KB |
Output is correct |
4 |
Correct |
95 ms |
88028 KB |
Output is correct |
5 |
Correct |
99 ms |
88188 KB |
Output is correct |
6 |
Correct |
104 ms |
88312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
87288 KB |
Output is correct |
2 |
Correct |
87 ms |
87288 KB |
Output is correct |
3 |
Correct |
77 ms |
87288 KB |
Output is correct |
4 |
Correct |
80 ms |
87416 KB |
Output is correct |
5 |
Correct |
76 ms |
87416 KB |
Output is correct |
6 |
Correct |
82 ms |
87176 KB |
Output is correct |
7 |
Correct |
76 ms |
87188 KB |
Output is correct |
8 |
Execution timed out |
1573 ms |
237012 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |