이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
using namespace std;
using ll = long long;
const int nx=1e5+5;
const int N=2e6+5;
int getid(char c)
{
if(c=='A')
return 0;
if(c=='C')
return 1;
if(c=='G')
return 2;
return 3;
}
int n, m;
string a[nx];
struct trie
{
struct dak
{
int child[4], l, r;
dak()
{
memset(child, -1, sizeof(child));
l=inf, r=0;
}
} nod[N<<2];
int cur;
trie() : cur(0){};
int new_node()
{
cur++;
return cur;
}
void add(string s, int id)
{
int pos=0;
for(int i = 0; i < s.size(); i++)
{
int c=getid(s[i]);
if(nod[pos].child[c]==-1)
nod[pos].child[c]=new_node();
pos=nod[pos].child[c];
nod[pos].l=min(nod[pos].l, id);
nod[pos].r=max(nod[pos].r, id);
}
}
ii get_range(string s)
{
int pos=0;
for(int i = 0; i < s.size(); i++)
{
int c=getid(s[i]);
if(nod[pos].child[c]==-1)
return ii(-1, -1);
pos=nod[pos].child[c];
}
return ii(nod[pos].l, nod[pos].r);
}
} T;
struct rev_trie
{
struct dak
{
int child[4];
vector<int> ids;
dak()
{
memset(child, -1, sizeof(child));
ids.clear();
}
} nod[N<<2];
int cur;
rev_trie() : cur(0){};
int new_node()
{
cur++;
return cur;
}
void add(string s, int id)
{
int pos=0;
for(int i = s.size()-1; i >= 0; i--)
{
int c=getid(s[i]);
if(nod[pos].child[c]==-1)
nod[pos].child[c]=new_node();
pos=nod[pos].child[c];
nod[pos].ids.emplace_back(id);
}
}
int query(string s, ii f)
{
int pos=0;
for(int i = s.size()-1; i >= 0; i--)
{
int c=getid(s[i]);
if(nod[pos].child[c]==-1)
return 0;
pos=nod[pos].child[c];
}
int l=lower_bound(nod[pos].ids.begin(), nod[pos].ids.end(), f.fi)-nod[pos].ids.begin();
int r=upper_bound(nod[pos].ids.begin(), nod[pos].ids.end(), f.se)-nod[pos].ids.begin();
return r-l;
}
} T1;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n>>m;
for(int i = 1; i <= n; i++)
cin>>a[i];
sort(a+1, a+n+1);
for(int i = 1; i <= n; i++)
{
T.add(a[i], i);
T1.add(a[i], i);
}
while(m--)
{
string x, y;
cin>>x>>y;
ii cur=T.get_range(x);
cout<<T1.query(y, cur)<<'\n';
}
}
컴파일 시 표준 에러 (stderr) 메시지
selling_rna.cpp: In member function 'void trie::add(std::string, int)':
selling_rna.cpp:43:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int i = 0; i < s.size(); i++)
| ~~^~~~~~~~~~
selling_rna.cpp: In member function 'std::pair<int, int> trie::get_range(std::string)':
selling_rna.cpp:56:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int i = 0; i < s.size(); i++)
| ~~^~~~~~~~~~
# | 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... |