#include <bits/stdc++.h>
#define ll long long
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define pi pair<int,int>
#define pii pair<int,pi>
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(a) (int) a.size()
#define endl '\n'
#define data "data"
using namespace std;
const ll linf = 1e18;
const int inf = 1e9;
const int MOD = 1e9 + 7, N = 1e5;
int n, m;
vector<string> Vs;
void inp(void)
{
cin >> n >> m;
FOR(i, 1, n)
{
string s; cin >> s;
Vs.pb(s);
}
sort(all(Vs));
}
struct Trie
{
Trie *child[26];
int l, r;
vector<int> idx;
Trie()
{
l = r = - 1;
idx = {};
FOR(i, 0, 25) child[i] = NULL;
}
};
Trie *root1;
Trie *root2;
void Insert(Trie *root, string s, int id, bool f)
{
Trie *p = root;
FOR(i, 0, sz(s) -1)
{
int c = s[i] - 'A';
if(p -> child[ c ] == NULL) p -> child[c] = new Trie();
p = p -> child[c];
if(f)
{
if(p -> l == -1) p -> l = id;
else p -> l = min(p -> l, id);
if(p -> r == -1) p -> r = id;
else p -> r = max(p -> r, id);
}
else
(p -> idx).pb(id);
}
}
pi Get1(string s)
{
Trie *p = root1;
FOR(i, 0, sz(s) -1)
{
int c = s[i] - 'A';
if(p -> child[c] == NULL) return {-1, -1};
p = p -> child[c];
}
return {p -> l, p -> r};
}
int Get2(string s, int L, int R)
{
Trie *p = root2;
FOR(i, 0, sz(s) -1)
{
int c = s[i] - 'A';
if(p -> child[c] == NULL)
{
return 0;
}
p = p -> child[c];
}
vector<int> :: iterator p1 = lower_bound(all(p -> idx), L);
vector<int> :: iterator p2 = upper_bound(all(p -> idx), R);
return p2 - p1;
}
void solve(void)
{
root1 = new Trie();
root2 = new Trie();
FOR(i, 0, sz(Vs) -1)
Insert(root1, Vs[i], i, 1);
FOR(i, 0, sz(Vs) -1)
{
string s = Vs[i];
reverse(all(s));
Insert(root2, s, i, 0);
}
FOR(i, 1, m)
{
string pre, suf; cin >> pre >> suf;
pi ret = Get1(pre);
int L = ret.fi, R = ret.se;
if(L == -1 && R == -1)
{
cout << "0\n";
}
else
{
reverse(all(suf));
cout << Get2(suf, L, R) << '\n';
}
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if(fopen(data".inp", "r"))
{
freopen(data".inp","r",stdin);
freopen(data".out","w",stdout);
}
inp();
solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:142:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
142 | freopen(data".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:143:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
143 | freopen(data".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... |