#include <bits/stdc++.h>
#define endl '\n'
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int N=1e5+5;
string s[N];
int valchild[255];
struct trie
{
trie *child[4];
int l,r,cn;
trie()
{
for(int i=0;i<4;i++) child[i]=NULL;
l=N,r=-N,cn=0;
}
} *root;
void ins(string &s,int idx)
{
trie *p=root;
for(int i=0;i<(int)s.size();i++)
{
int c=valchild[(int)s[i]];
if(p->child[c]==NULL) p->child[c]=new trie();
p=p->child[c];
p->l=min(p->l,idx);
p->r=max(p->r,idx);
}
p->cn++;
}
string tempdfs="";
int idxdfs=0;
void dfs(trie *p)
{
for(int i=0;i<(int)p->cn;i++) s[++idxdfs]=tempdfs;
for(int i=0;i<4;i++) if(p->child[i]!=NULL)
{
if(i==0) tempdfs.push_back('A');
else if(i==1) tempdfs.push_back('C');
else if(i==2) tempdfs.push_back('G');
else tempdfs.push_back('U');
dfs(p->child[i]);
tempdfs.pop_back();
}
delete p;
}
pair<int,int> get(string &s)
{
trie *p=root;
for(int i=0;i<(int)s.size();i++)
{
int c=valchild[(int)s[i]];
if(p->child[c]==NULL) return {-1,-1};
p=p->child[c];
}
return {p->l,p->r};
}
struct trie2
{
trie2 *child[4];
vector<int> v;
trie2()
{
for(int i=0;i<4;i++) child[i]=NULL;
v.clear();
}
} *root2;
void ins2(string &s,int idx)
{
trie2 *p=root2;
for(int i=0;i<(int)s.size();i++)
{
int c=valchild[(int)s[i]];
if(p->child[c]==NULL) p->child[c]=new trie2();
p=p->child[c];
p->v.push_back(idx);
}
}
int get2(pair<int,int> range,string &s)
{
trie2 *p=root2;
for(int i=0;i<(int)s.size();i++)
{
int c=valchild[(int)s[i]];
if(p->child[c]==NULL) return 0;
p=p->child[c];
}
int r=upper_bound(p->v.begin(),p->v.end(),range.se)-p->v.begin(),l=lower_bound(p->v.begin(),p->v.end(),range.fi)-p->v.begin();
return r-l;
}
void solve()
{
int n,q;
cin >> n >> q;
valchild['A']=0;valchild['C']=1;valchild['G']=2;valchild['U']=3;
root=new trie();
for(int i=1;i<=n;i++)
{
cin >> s[i];
ins(s[i],-1);
}
dfs(root);
root=new trie();
root2=new trie2();
for(int i=1;i<=n;i++)
{
ins(s[i],i);
reverse(s[i].begin(),s[i].end());
ins2(s[i],i);
}
while(q--)
{
string s,t;
cin >> s >> t;
reverse(t.begin(),t.end());
pair<int,int> range=get(s);
if(range.fi==range.se&&range.fi==-1) cout << 0 << endl;
else cout << get2(range,t) << endl;
}
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//freopen("hanhhhh.inp","r",stdin);
//freopen("hanhhhh.out","w",stdout);
int t=1;
//cin >> t;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3420 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
2 ms |
3676 KB |
Output is correct |
4 |
Correct |
1 ms |
3420 KB |
Output is correct |
5 |
Correct |
2 ms |
3416 KB |
Output is correct |
6 |
Correct |
2 ms |
3416 KB |
Output is correct |
7 |
Correct |
1 ms |
3592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
195408 KB |
Output is correct |
2 |
Correct |
198 ms |
185172 KB |
Output is correct |
3 |
Correct |
184 ms |
143356 KB |
Output is correct |
4 |
Correct |
196 ms |
137040 KB |
Output is correct |
5 |
Correct |
206 ms |
198232 KB |
Output is correct |
6 |
Correct |
199 ms |
201204 KB |
Output is correct |
7 |
Correct |
65 ms |
18472 KB |
Output is correct |
8 |
Correct |
202 ms |
127696 KB |
Output is correct |
9 |
Correct |
146 ms |
109908 KB |
Output is correct |
10 |
Correct |
123 ms |
105136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
4824 KB |
Output is correct |
2 |
Correct |
11 ms |
4956 KB |
Output is correct |
3 |
Correct |
13 ms |
4792 KB |
Output is correct |
4 |
Correct |
10 ms |
4700 KB |
Output is correct |
5 |
Correct |
11 ms |
4716 KB |
Output is correct |
6 |
Correct |
13 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3420 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
2 ms |
3676 KB |
Output is correct |
4 |
Correct |
1 ms |
3420 KB |
Output is correct |
5 |
Correct |
2 ms |
3416 KB |
Output is correct |
6 |
Correct |
2 ms |
3416 KB |
Output is correct |
7 |
Correct |
1 ms |
3592 KB |
Output is correct |
8 |
Correct |
163 ms |
195408 KB |
Output is correct |
9 |
Correct |
198 ms |
185172 KB |
Output is correct |
10 |
Correct |
184 ms |
143356 KB |
Output is correct |
11 |
Correct |
196 ms |
137040 KB |
Output is correct |
12 |
Correct |
206 ms |
198232 KB |
Output is correct |
13 |
Correct |
199 ms |
201204 KB |
Output is correct |
14 |
Correct |
65 ms |
18472 KB |
Output is correct |
15 |
Correct |
202 ms |
127696 KB |
Output is correct |
16 |
Correct |
146 ms |
109908 KB |
Output is correct |
17 |
Correct |
123 ms |
105136 KB |
Output is correct |
18 |
Correct |
13 ms |
4824 KB |
Output is correct |
19 |
Correct |
11 ms |
4956 KB |
Output is correct |
20 |
Correct |
13 ms |
4792 KB |
Output is correct |
21 |
Correct |
10 ms |
4700 KB |
Output is correct |
22 |
Correct |
11 ms |
4716 KB |
Output is correct |
23 |
Correct |
13 ms |
4700 KB |
Output is correct |
24 |
Correct |
149 ms |
161496 KB |
Output is correct |
25 |
Correct |
158 ms |
161640 KB |
Output is correct |
26 |
Correct |
147 ms |
159480 KB |
Output is correct |
27 |
Correct |
181 ms |
120180 KB |
Output is correct |
28 |
Correct |
97 ms |
40020 KB |
Output is correct |
29 |
Correct |
38 ms |
11592 KB |
Output is correct |