#include <bits/stdc++.h>
#define task "TEST"
#define task2 "A"
#define pl pair<ll, ll>
#define pf push_front
#define pb push_back
#define pob pop_back
#define pof pop_front
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b, c) for (int i=a; i<=b; i+=c)
#define FORE(i, a, b, c) for (int i=a; i>=b; i+=c)
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int Mod = 1e9+7;
const int maxn = 2e6;
const ll Inf = 3e9;
int n, m;
string str[maxn+1];
struct TrieNode {
struct TrieNode* childNode[26];
int wordCount = 0;
vector<int> res = {};
TrieNode() {
for (int i = 0; i < 26; ++i) childNode[i] = NULL;
}
};
void insert_key(TrieNode* root, string& key, int num) {
TrieNode* currentNode = root;
for (auto c : key) {
if (currentNode->childNode[c - 'A'] == NULL) {
TrieNode* newNode = new TrieNode();
currentNode->childNode[c - 'A'] = newNode;
}
currentNode = currentNode->childNode[c - 'A'];
currentNode->res.pb(num);
}
currentNode->wordCount++;
}
vector<int> search_key(TrieNode* root, string& key) {
TrieNode* currentNode = root;
for (auto c : key) {
if (currentNode->childNode[c - 'A'] == NULL)
return vector<int>();
currentNode = currentNode->childNode[c - 'A'];
}
return currentNode->res;
}
TrieNode* trie = new TrieNode;
TrieNode* revtrie = new TrieNode;
void Read()
{
cin >> n >> m;
FOR(i, 1, n, 1) cin >> str[i];
sort(str + 1, str + n + 1);
FOR(i, 1, n, 1) {
insert_key(trie, str[i], i);
reverse(str[i].begin(), str[i].end());
insert_key(revtrie, str[i], i);
}
}
void Solve()
{
string left, right;
vector<int> L, R;
int l, r;
while (m--) {
cin >> left >> right;
reverse(right.begin(), right.end());
L = search_key(trie, left);
R = search_key(revtrie, right);
if (!L.size() or !R.size()) {
cout << 0 << '\n';
continue;
}
l = lower_bound(R.begin(), R.end(), L.front()) - R.begin();
r = upper_bound(R.begin(), R.end(), L.back()) - R.begin();
cout << r - l << '\n';
}
}
int main()
{
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t; //Setup();
for (t=1; t--;)
{
Read(); Solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:86:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | freopen (task".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:87:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | freopen (task".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |