#include <bits/stdc++.h>
using namespace std;
#define For(i, a, b) for (int i = a; i <= b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)
#define TASK ""
#define fi first
#define se second
#define iii pair<ii, int>
const int N = 1e6 + 7;
const int oo = 1e18;
typedef pair<int, int> ii;
const int maxn = 2e6 + 10;
int n, m, tim[3];
string s[100010];
struct data {
int child[27], in, ou;
};
vector<int> c[maxn];
data a[maxn], b[maxn];
void update(int w) {
string sa = s[w];
int u = 0;
for (int i = 0; i < sa.size(); i++) {
if (a[u].child[sa[i] - 'A' + 1] == 0) {
a[u].child[sa[i] - 'A' + 1] = ++tim[1];
a[tim[1]].in = w;
}
u = a[u].child[sa[i] - 'A' + 1];
a[u].ou = w;
}
u = 0;
for (int i = sa.size() - 1; i >= 0; i--) {
if (b[u].child[sa[i] - 'A' + 1] == 0) {
b[u].child[sa[i] - 'A' + 1] = ++tim[2];
}
u = b[u].child[sa[i] - 'A' + 1];
c[u].push_back(w);
}
}
void get(string sa, string sb) {
int u = 0, ua = 0;
for (int i = 0; i < sa.size(); i++) {
if (a[u].child[sa[i] - 'A' + 1]== 0) {
cout << "0\n";
return;
}
u = a[u].child[sa[i] - 'A' + 1];
}
for (int i = sb.size() - 1; i >= 0; i--) {
if (b[ua].child[sb[i] - 'A' + 1] == 0) {
cout << "0\n";
return;
}
ua = b[ua].child[sb[i] - 'A' + 1];
}
c[ua].push_back(1000000);
int w = lower_bound(c[ua].begin(), c[ua].end(), a[u].in) - c[ua].begin();
int wa = upper_bound(c[ua].begin(), c[ua].end(), a[u].ou) - c[ua].begin();
wa--;
if (w > wa) {
cout << "0\n";
return;
}
cout << wa - w + 1 << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
cin >> n >> m;
For(i, 1, n) {
cin >> s[i];
}
sort(s + 1, s + n + 1);
For(i, 1, n) {
update(i);
}
For(i, 1, m) {
cin >> s[1] >> s[2];
get(s[1], s[2]);
}
return 0;
}
Compilation message
selling_rna.cpp:11:16: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
11 | const int oo = 1e18;
| ^~~~
selling_rna.cpp: In function 'void update(int)':
selling_rna.cpp:26:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for (int i = 0; i < sa.size(); i++) {
| ~~^~~~~~~~~~~
selling_rna.cpp: In function 'void get(std::string, std::string)':
selling_rna.cpp:47:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int i = 0; i < sa.size(); i++) {
| ~~^~~~~~~~~~~
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:80:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:81:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
50776 KB |
Output is correct |
2 |
Correct |
11 ms |
50780 KB |
Output is correct |
3 |
Correct |
11 ms |
50780 KB |
Output is correct |
4 |
Correct |
11 ms |
50824 KB |
Output is correct |
5 |
Correct |
11 ms |
50780 KB |
Output is correct |
6 |
Correct |
11 ms |
50848 KB |
Output is correct |
7 |
Correct |
11 ms |
50780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
209 ms |
340840 KB |
Output is correct |
2 |
Correct |
209 ms |
326228 KB |
Output is correct |
3 |
Correct |
123 ms |
288084 KB |
Output is correct |
4 |
Correct |
118 ms |
277076 KB |
Output is correct |
5 |
Correct |
186 ms |
368732 KB |
Output is correct |
6 |
Correct |
209 ms |
373360 KB |
Output is correct |
7 |
Correct |
54 ms |
65808 KB |
Output is correct |
8 |
Correct |
190 ms |
249424 KB |
Output is correct |
9 |
Correct |
156 ms |
218964 KB |
Output is correct |
10 |
Correct |
118 ms |
211440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
52440 KB |
Output is correct |
2 |
Correct |
27 ms |
52560 KB |
Output is correct |
3 |
Correct |
31 ms |
52572 KB |
Output is correct |
4 |
Correct |
26 ms |
52060 KB |
Output is correct |
5 |
Correct |
28 ms |
52552 KB |
Output is correct |
6 |
Correct |
33 ms |
52560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
50776 KB |
Output is correct |
2 |
Correct |
11 ms |
50780 KB |
Output is correct |
3 |
Correct |
11 ms |
50780 KB |
Output is correct |
4 |
Correct |
11 ms |
50824 KB |
Output is correct |
5 |
Correct |
11 ms |
50780 KB |
Output is correct |
6 |
Correct |
11 ms |
50848 KB |
Output is correct |
7 |
Correct |
11 ms |
50780 KB |
Output is correct |
8 |
Correct |
209 ms |
340840 KB |
Output is correct |
9 |
Correct |
209 ms |
326228 KB |
Output is correct |
10 |
Correct |
123 ms |
288084 KB |
Output is correct |
11 |
Correct |
118 ms |
277076 KB |
Output is correct |
12 |
Correct |
186 ms |
368732 KB |
Output is correct |
13 |
Correct |
209 ms |
373360 KB |
Output is correct |
14 |
Correct |
54 ms |
65808 KB |
Output is correct |
15 |
Correct |
190 ms |
249424 KB |
Output is correct |
16 |
Correct |
156 ms |
218964 KB |
Output is correct |
17 |
Correct |
118 ms |
211440 KB |
Output is correct |
18 |
Correct |
31 ms |
52440 KB |
Output is correct |
19 |
Correct |
27 ms |
52560 KB |
Output is correct |
20 |
Correct |
31 ms |
52572 KB |
Output is correct |
21 |
Correct |
26 ms |
52060 KB |
Output is correct |
22 |
Correct |
28 ms |
52552 KB |
Output is correct |
23 |
Correct |
33 ms |
52560 KB |
Output is correct |
24 |
Correct |
217 ms |
291364 KB |
Output is correct |
25 |
Correct |
226 ms |
291804 KB |
Output is correct |
26 |
Correct |
203 ms |
288508 KB |
Output is correct |
27 |
Correct |
97 ms |
246976 KB |
Output is correct |
28 |
Correct |
147 ms |
96848 KB |
Output is correct |
29 |
Correct |
85 ms |
59576 KB |
Output is correct |