This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
typedef long long ll;
typedef long double ld;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
typedef pair<ll,ll>pi; typedef pair<ll,pi>spi; typedef pair<pi,pi>dpi;
#define LLINF ((long long)1e18)
#define INF int(1e9+1e6)
#define MAXN (106)
ll n,m;
string A[MAXN];
struct fen {
vector<int> fw[MAXN*2];
fen() {
for(auto&i:fw)i.resize(MAXN*2);
}
void update(int x,int y,ll nval){
assert(x), assert(y);
ll oy=y;
for(;x<MAXN*2;x+=x&(-x)) {
y=oy;
for(;y<MAXN*2;y+=y&(-y)) fw[x][y] += nval;
}
}
ll sum(ll a,ll b,ll c,ll d){
assert(a>0&&b>0&&c>0&&d>0);
for(auto i:{a,b,c,d})assert(i<MAXN*2);
return sum(b, c, d) - sum(a-1, c, d);
}
ll sum(ll x,ll a,ll b){
ll res=0;
for(;x;x-=x&(-x)) {
res += sum(x,b) - sum(x,a-1);
}
return res;
}
ll sum(ll f,ll x){
ll res=0;
for(;x;x-=x&(-x)) res+=fw[f][x];
return res;
}
} fen;
ll ind(char ch){if(ch=='A')return 0;if(ch=='G')return 1;if(ch=='C')return 2;return 3;}
struct trie {
char ch='*';
ll c[4]={-1,-1,-1,-1};
void init(char x){
ch=x;
mmst(c,-1);
}
}t[2][MAXN];
int co[2]={1,1},at[2][MAXN],st[2][MAXN],en[2][MAXN],order;
int tins(int alt,int id,int i,int j){
if(t[alt][id].ch=='*'){
t[alt][id].init(A[i][j]);
}
if(A[i].size() == j+1) return id;
ll num=ind(A[i][j+1]);
if(t[alt][id].c[num] == -1) t[alt][id].c[num] = co[alt] ++;
return tins(alt,t[alt][id].c[num],i,j+1);
}
void dfs(ll alt,ll x){
st[alt][x]=order++;
for(auto i:t[alt][x].c) if(~i) {
dfs(alt,i);
}
en[alt][x]=order-1;
}
string p[2];
int find(int alt,int id,int i){
if(id == -1 || t[alt][id].ch=='*')return -1;
if(i+1==p[alt].size())return id;
int num=ind(p[alt][i+1]);
return find(alt,t[alt][id].c[num],i+1);
}
int main(){
FAST
cin>>n>>m;assert(n<=100),assert(m<=100);
FOR(i,1,n)cin>>A[i],assert(A[i].size()<=100);
t[0][0].init('!'),t[1][0].init('!');
FOR(i,1,n)reverse(all(A[i]));
FOR(i,1,n)at[1][i]=tins(1,0,i,-1);
FOR(i,1,n)reverse(all(A[i]));
FOR(i,1,n)at[0][i]=tins(0,0,i,-1);
dfs(0,0), order=0, dfs(1,0);
FOR(i,1,n)fen.update(st[0][at[0][i]],st[1][at[1][i]],1);
FOR(i,1,m){
cin>>p[0]>>p[1];assert(p[0].size()<=100),assert(p[1].size()<=100);
reverse(all(p[1]));
int ind[2] = {find(0,0,-1), find(1,0,-1)};
if((~ind[0])&&(~ind[1])) cout<<fen.sum(st[0][ind[0]],en[0][ind[0]],st[1][ind[1]],en[1][ind[1]])<<'\n';
else cout<<"0\n";
}
}
/*
1 1
AGA
AG GA
*/
Compilation message (stderr)
selling_rna.cpp: In function 'int tins(int, int, int, int)':
selling_rna.cpp:76:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(A[i].size() == j+1) return id;
~~~~~~~~~~~~^~~~~~
selling_rna.cpp: In function 'int find(int, int, int)':
selling_rna.cpp:91:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i+1==p[alt].size())return id;
~~~^~~~~~~~~~~~~~~
# | 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... |