#include <bits/stdc++.h>
using namespace std;
// macros
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define FOR(a,b) for(auto& a : b)
#define all(a) a.begin(), a.end()
#define INF 1e18
#define EPS 1e-9
#define pb push_back
#define popb pop_back
#define fi first
#define se second
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// input
template<class T> void IN(T& x) {cin >> x;}
template<class H, class... T> void IN(H& h, T&... t) {IN(h); IN(t...); }
// output
template<class T1, class T2> void OUT(const pair<T1,T2>& x);
template<class T> void OUT(const vector<T>& x);
template<class T> void OUT(const T& x) {cout << x;}
template<class H, class... T> void OUT(const H& h, const T&... t) {OUT(h); OUT(t...); }
template<class T1, class T2> void OUT(const pair<T1,T2>& x) {OUT(x.fi,' ',x.se);}
template<class T> void OUT(const vector<T>& x) {RE(i,x.size()) OUT(i==0?"":" ",x[i]);}
template<class... T> void OUTL(const T&... t) {OUT(t..., "\n"); }
template<class H> void OUTLS(const H& h) {OUTL(h); }
template<class H, class... T> void OUTLS(const H& h, const T&... t) {OUT(h,' '); OUTLS(t...); }
// dp
template<class T> bool ckmin(T&a, T&b) { bool bl = a > b; a = min(a,b); return bl;}
template<class T> bool ckmax(T&a, T&b) { bool bl = a < b; a = max(a,b); return bl;}
void program();
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
program();
}
// mod library
ll MOD=1e9+7;
inline ll mod(ll x_) {
return (x_)%MOD;
}
ll modpow(ll x_, ll N_) {
if(N_ == 0) return 1;
ll a = modpow(x_,N_/2);
a = (a*a)%MOD;
if(N_%2) a = (a*x_)%MOD;
return a;
}
ll inv(ll x_) {
return modpow(x_, MOD-2);
}
class mi {
public:
mi(ll v=0) {value = v;}
mi operator+ (ll rs) {return mod(value+rs);}
mi operator- (ll rs) {return mod(value-rs+MOD);}
mi operator* (ll rs) {return mod(value*rs);}
mi operator/ (ll rs) {return mod(value*inv(rs));}
mi& operator+=(ll rs) {*this = (*this)+rs; return *this;}
mi& operator-=(ll rs) {*this = (*this)-rs; return *this;}
mi& operator*=(ll rs) {*this = (*this)*rs; return *this;}
mi& operator/=(ll rs) {*this = (*this)/rs; return *this;}
operator ll&() {return value;}
ll value;
};
typedef vector<mi> vmi;
//===================//
// begin program //
//===================//
const int MX = 2e5;
const int N = (1<<23);
// debug
void printString(string s) {
FOR(c,s) c+='a';
OUTL(s);
}
int n, m;
string s[MX], p[MX], q[MX];
// hashing
mi hsh[4];
mi hashString(string s) {
mi result = 0;
FOR(c,s)
result = result*mi(2) + hsh[c];
return result;
}
// tree
int nodes=1;
vi adj[N];
int w[N];
void createNode(int u) {
RE(i,4) adj[u].pb(nodes++);
}
// trie
string t;
int bg[MX];
int addTrie(int u, int i) {
if(t.size() == i)
return u;
if(adj[u].empty())
createNode(u);
return addTrie(adj[u][t[i]],i+1);
}
int getTrie(int u, int i) {
if(t.size() == i)
return u;
if(adj[u].empty())
return -1;
return getTrie(adj[u][t[i]],i+1);
}
// dfs pre order
int sgi[N], sge[N], nextSeg = 0;
void dfs(int u) {
sgi[u] = nextSeg++;
FOR(v,adj[u])
dfs(v);
sge[u] = nextSeg - 1;
}
// fenwick tree
int FT[N];
void addFT(int i, int value) {
for(i++; i<N; i+=i&-i) FT[i] += value;
}
int getFT(int i) {
int ans = 0;
for(i++; i; i-=i&-i) ans += FT[i];
return ans;
}
int getFT(int i, int j) {
return getFT(j) - getFT(i-1);
}
// answer
map<string,vi> mp;
int ans[N];
// dfs ans
string curStr;
vi inter;
void dfsAns(int i) {
if(inter.empty())
return;
FOR(j,inter)
addFT(sgi[bg[j]],-1);
RE(c,4) {
vi rem, newInter;
FOR(x,inter) {
if(s[x].size() == i || s[x][i] != c)
rem.pb(x);
else
newInter.pb(x);
}
inter = newInter;
newInter.clear(); newInter.shrink_to_fit();
FOR(j,inter)
addFT(sgi[bg[j]],1);
curStr += char(c);
// answer queries
vi queries = mp[curStr];
FOR(x,queries) {
t = q[x];
reverse(all(t));
int u = getTrie(0,0);
if(u == -1)
continue;
ans[x] = getFT(sgi[u], sge[u]);
}
dfsAns(i+1);
// return back to normal
curStr.pop_back();
FOR(j,inter)
addFT(sgi[bg[j]], -1);
FOR(j,rem)
inter.pb(j);
}
FOR(j,inter)
addFT(sgi[bg[j]],1);
}
void program() {
// pre computating
RE(i,4) hsh[i] = rng()%MOD;
// input
IN(n,m);
RE(i,n) IN(s[i]);
RE(i,m) IN(p[i],q[i]);
// compress strings
map<char,char> charToComp;
charToComp['A']=0, charToComp['U']=1, charToComp['G']=2, charToComp['C']=3;
RE(i,n) FOR(c,s[i]) c = charToComp[c];
RE(i,m) {
FOR(c,p[i]) c = charToComp[c];
FOR(c,q[i]) c = charToComp[c];
}
// create trie
createNode(0);
RE(i,n) {
t = s[i];
reverse(all(t));
bg[i] = addTrie(0,0);
}
dfs(0);
RE(i,n) addFT(sgi[bg[i]],1);
// fill map
RE(i,m) mp[p[i]].pb(i);
RE(i,n) inter.pb(i);
dfsAns(0);
RE(i,m) OUTL(ans[i]);
}
Compilation message
selling_rna.cpp: In function 'mi hashString(std::string)':
selling_rna.cpp:112:37: warning: array subscript has type 'char' [-Wchar-subscripts]
112 | result = result*mi(2) + hsh[c];
| ^
selling_rna.cpp: In function 'int addTrie(int, int)':
selling_rna.cpp:128:17: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
128 | if(t.size() == i)
| ~~~~~~~~~^~~~
selling_rna.cpp: In function 'int getTrie(int, int)':
selling_rna.cpp:135:17: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
135 | if(t.size() == i)
| ~~~~~~~~~^~~~
selling_rna.cpp: In function 'void dfsAns(int)':
selling_rna.cpp:180:28: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
180 | if(s[x].size() == i || s[x][i] != c)
| ~~~~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
122 ms |
216212 KB |
Output is correct |
2 |
Correct |
123 ms |
216132 KB |
Output is correct |
3 |
Correct |
119 ms |
216244 KB |
Output is correct |
4 |
Correct |
119 ms |
216308 KB |
Output is correct |
5 |
Correct |
120 ms |
216156 KB |
Output is correct |
6 |
Correct |
123 ms |
216220 KB |
Output is correct |
7 |
Correct |
122 ms |
216132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1598 ms |
423632 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
186 ms |
217748 KB |
Output is correct |
2 |
Correct |
192 ms |
217684 KB |
Output is correct |
3 |
Correct |
189 ms |
217792 KB |
Output is correct |
4 |
Correct |
174 ms |
217128 KB |
Output is correct |
5 |
Correct |
203 ms |
218548 KB |
Output is correct |
6 |
Correct |
207 ms |
218064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
122 ms |
216212 KB |
Output is correct |
2 |
Correct |
123 ms |
216132 KB |
Output is correct |
3 |
Correct |
119 ms |
216244 KB |
Output is correct |
4 |
Correct |
119 ms |
216308 KB |
Output is correct |
5 |
Correct |
120 ms |
216156 KB |
Output is correct |
6 |
Correct |
123 ms |
216220 KB |
Output is correct |
7 |
Correct |
122 ms |
216132 KB |
Output is correct |
8 |
Execution timed out |
1598 ms |
423632 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |