QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#590255 | #5551. Selling RNA Strands | makrav | 35 | 27ms | 11924kb | C++20 | 4.5kb | 2024-09-25 22:57:57 | 2024-09-25 22:57:58 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ff first
#define sc second
struct trie {
vector<vector<int>> nxt;
vector<int> sub, leaf;
int siz = 1;
trie() {
nxt.assign(1, vector<int>(4, 0));
sub.assign(1, 0);
leaf.assign(1, 0);
}
void clear() {
nxt.clear();
}
void add(vector<int> &word) {
int v = 0;
for (int i = 0; i < sz(word); i++) {
sub[v]++;
if (nxt[v][word[i]] == 0) {
nxt[v][word[i]] = siz++;
nxt.pb(vector<int>(4, 0));
sub.pb(0);
leaf.pb(0);
}
v = nxt[v][word[i]];
}
sub[v]++;
leaf[v]++;
}
int find(vector<int>&word) {
int v = 0;
for (int i = 0; i < sz(word); i++) {
if (nxt[v][word[i]] == 0) return -1;
v = nxt[v][word[i]];
}
return v;
}
int get(vector<int>& word) {
int vt = find(word);
return (vt == -1 ? 0 : sub[vt]);
}
};
void solve() {
int n, m; cin >> n >> m;
trie T_pref;
vector<char> ch = {'A', 'U', 'G', 'C'};
auto conv = [&](string& s) {
vector<int> cword(s.size());
for (int j = 0; j < s.size(); j++) {
for (int k = 0; k < 4; k++) {
if (ch[k] == s[j]) cword[j] = k;
}
}
return cword;
};
for (int i = 0; i < n; i++) {
string s; cin >> s;
vector<int> cword = conv(s);
T_pref.add(cword);
}
vector<vector<pair<vector<int>, int>>> Q(T_pref.siz);
vector<int> ans(m);
for (int i = 0; i < m; i++) {
string pref, suff; cin >> pref >> suff;
vector<int> pr = conv(pref), sf = conv(suff);
reverse(all(sf));
int vt = T_pref.find(pr);
if (vt != -1) {
Q[vt].pb({sf, i});
}
}
vector<int> path;
auto dfs = [&](int v, auto&&self) -> pair<trie, vector<vector<int>>> {
int bs = -1;
vector<int> sons, lett;
for (int j = 0; j < 4; j++) {
if (T_pref.nxt[v][j] != 0) {
sons.pb(T_pref.nxt[v][j]);
lett.pb(j);
if (bs == -1 || T_pref.sub[bs] < T_pref.sub[sons.back()]) bs = sons.back();
}
}
if (bs == -1) {
trie Tcur;
assert(T_pref.leaf[v]);
vector<int> cw = path;
reverse(all(cw));
vector<vector<int>> sbtr;
for (int j = 0; j < T_pref.leaf[v]; j++) {
Tcur.add(cw);
sbtr.pb(cw);
}
for (auto [SF, ind] : Q[v]) {
ans[ind] = Tcur.get(SF);
}
return {Tcur, sbtr};
}
trie Tcur;
vector<vector<int>> sbtr;
int CIND = 0;
for (auto &u : sons) {
if (u == bs) {
path.pb(lett[CIND]);
auto result = self(u, self);
path.pop_back();
swap(Tcur,result.first);
swap(sbtr,result.second);
}
CIND++;
}
CIND = 0;
for (auto &u : sons) {
if (u != bs) {
path.pb(lett[CIND]);
auto result = self(u, self);
path.pop_back();
for (auto wrd : result.second) {
sbtr.pb(wrd);
Tcur.add(wrd);
}
result.first.clear(); result.second.clear();
}
CIND++;
}
if (T_pref.leaf[v]) {
vector<int> cw = path;
reverse(all(cw));
for (int j = 0; j < T_pref.leaf[v]; j++) {
Tcur.add(cw);
sbtr.pb(cw);
}
}
for (auto [SF, ind] : Q[v]) {
ans[ind] = Tcur.get(SF);
}
return {Tcur, sbtr};
};
dfs(0, dfs);
for (auto &u : ans) cout << u << '\n';
}
signed main() {
int tt = 1;
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
cin >> tt;
#else
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#endif
while (tt--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 1ms
memory: 3592kb
input:
100 100 A G AGC AUA C CCA CGGAG CCGAG GACA UCAAC CUUAU AC A CGUA UAUG UGCGA GCU GUUAU UAAU A UAA U CGCCC GCG U AUUC GACA CC AG GUCGU UUCA AGGC G CU UG CUUA CUAU AA A GUUG GGU UU C GCUG C GUGGA C UAU UAG GC GUU GC UCUCA U AA AG C GGU GC CCUG CCUU GG CAGCU UAGGU GGCUC CUACG UGC UU UAAUG UGGCA CAA UGAG...
output:
9 1 11 1 1 2 4 3 11 6 1 2 1 1 4 2 1 11 10 1 5 1 1 1 1 11 1 13 1 1 3 1 11 1 11 11 1 2 2 1 1 10 1 1 1 2 4 2 1 13 10 1 1 1 2 1 1 4 1 2 1 1 9 9 13 2 1 2 9 2 1 10 2 2 1 9 1 1 1 1 1 1 1 9 1 1 11 11 1 1 1 1 2 13 1 2 1 13 1 1
result:
ok 100 lines
Test #2:
score: 10
Accepted
time: 1ms
memory: 3684kb
input:
100 100 U CUAG AUCA UAACG U GG CUCCA G G UGCCA CGA ACAAU GUUA GUAGA AUCU C GC ACACG UUG CCC U UUGUC AUC GAA U CGGA U AGA GCU AAG CUA UCGG GGG UCG GGAU U CGA CCAUG ACG G C CUU GUCU UUAU UACU UCC UGAAG GCAAU CUCG GAG U ACC CUAU CU UU AAGUA AGGU CGAA U GCU AA CAA G UAGGG A UCU A AA G UAC GC CAC AGUGA U...
output:
1 5 1 8 14 1 5 1 12 1 2 2 12 1 1 6 8 1 1 4 5 2 1 1 3 1 1 1 2 2 1 1 2 1 1 14 1 1 1 1 2 1 2 1 1 14 1 1 2 8 1 14 2 12 1 1 1 7 1 2 1 1 12 5 1 1 1 2 12 1 1 14 2 8 1 1 1 6 3 1 2 1 5 12 1 1 1 1 3 1 1 12 14 1 14 1 4 1 2 14
result:
ok 100 lines
Test #3:
score: 10
Accepted
time: 1ms
memory: 3896kb
input:
100 100 GACC CACCU GGCC UGUA UCAG UUUGU GU AGUAC ACUG UAGAG UCAA GGCA G GGUU CGA A G UUUGG CGA UG AU GAC AA GGGA AGA AC AGCAG AGU A UUUAA G UGC ACGUC A AUAAA UAAAA CGU AUGAC GAUA UGUCC AACU AAUA CUAA C CG G GGC UGACC AUGU U UA GAAUA AG UA UGGAC AAG CCGC UA CGCAA A CCG UAAAA GAG CGG UA GAUG GGA UCAUU...
output:
2 4 9 9 1 3 9 1 6 11 1 1 1 1 1 11 2 1 2 9 4 5 1 2 1 11 3 1 4 2 1 1 9 1 11 1 11 4 11 1 2 9 1 1 1 1 1 1 2 1 4 1 1 4 9 9 3 11 1 1 1 1 3 5 6 1 2 1 4 1 2 2 6 9 1 1 5 1 4 3 2 1 1 4 10 1 2 1 2 2 11 2 1 1 2 3 1 1 1 1
result:
ok 100 lines
Test #4:
score: 10
Accepted
time: 1ms
memory: 3884kb
input:
100 100 CCG UAAUA AGUUU GAG GUAGC A AAUC UC ACCC A U UCGG AG G AG AC AAGAA GCGAU CGUC G AG UUAAU CGUGU GUCG A UA AU GAAAC AUG CGUG ACGG G C AUG GAUU ACGG GGA CU UG U AAA C UAUGU UAC CUA ACC CGAUG GCC UACG CCA UAAU UUAAA C UA GACA UUGUC AGCU UCCA AAAA CUUA GU UCCGA C UUU AG UC UAAU GGG CUCCC C UUU UG...
output:
1 1 2 4 9 1 2 2 2 12 1 1 10 2 10 4 12 5 2 1 1 10 8 4 2 5 8 1 1 4 1 3 9 1 1 1 1 2 4 9 2 4 2 2 1 1 1 8 1 1 1 9 1 7 4 1 4 8 1 3 8 10 4 8 8 2 2 4 2 9 2 2 1 1 1 1 1 1 1 2 2 2 1 4 2 9 2 2 8 9 1 2 2 8 6 4 4 12 2 5
result:
ok 100 lines
Test #5:
score: 10
Accepted
time: 1ms
memory: 3684kb
input:
100 100 CAGAA CU GAUCU AAAC AUGGG CCUAC G C A UC UUCAU GUCAA UUA GG UG GCU GGGCU UUUA CAG G G AAUU ACAU AUAC UA C UUCC CA CUC A UA GGUC UGG AGG GUUCU AA ACCC CAUG CGCA A AU GAGGU GG UCU AA AGUC AGC AACUA CUGAC GGACU CCGG AGGA GAG UAAG GA ACGA A UCUGG CAG GCAA GAG CC UAAUC UUGA AG U GGG AAACG UA ACGA...
output:
1 1 13 1 1 1 2 2 1 2 1 1 2 1 8 8 8 1 2 2 4 2 2 2 2 1 3 2 1 2 4 1 1 5 13 4 1 6 13 2 2 10 1 2 2 2 1 5 10 1 1 8 8 8 1 10 1 1 4 3 2 10 3 1 1 5 2 1 5 1 2 3 1 6 1 1 13 1 10 2 2 1 1 1 1 13 3 10 3 2 1 1 2 2 2 5 1 2 10 1
result:
ok 100 lines
Test #6:
score: 10
Accepted
time: 1ms
memory: 3612kb
input:
100 100 UG G AA GA G ACAC GGAAG GAGU UG UUAA UG U AGUUA CA C G CCCG U UAAG UCUG CGGU A CAC ACGA CA A UC GAAUC A GAGG GG GGG CCCUC GACGU GGUG GCG CCAG C AUAU A AG UGU UCGC UC AUA ACGCC A GCUA CCU G AGC GG GCAUG GGGUG C CCCC GG A AG AC UA CACG AU GAG G GG GACC ACAAC GCGUG C GAA C UGCU GC ACGGA AUG GCA...
output:
14 1 7 5 1 4 7 1 24 4 14 24 1 4 7 1 1 24 1 1 1 4 7 6 3 1 14 4 1 1 1 7 7 6 14 1 24 1 9 1 1 24 7 24 14 9 1 1 1 1 7 9 24 24 7 5 1 3 24 1 1 24 1 3 2 1 9 4 2 2 1 14 7 5 14 14 1 1 2 2 4 24 2 14 1 1 14 3 9 24 7 1 1 4 1 24 9 5 4 14
result:
ok 100 lines
Test #7:
score: 10
Accepted
time: 1ms
memory: 3600kb
input:
100 100 AG ACACC GAG GG C AC AGCA AAUGU AGU UUA UG UAC UU U G G CUAA CCG AAGU G CAAU CCCUA CCG G GACAA C CCCAA UUUCG UG C AAUGC GCA A C AAUCC UCGC UG GU GUAUG CUGG UUCA AC AACU UA GAAG CGGC ACU GUAAU GGGGA UGAUU GCA CUGAG CUA UUGGU GC CCA GCAU AC U GG UGCA CUACC GAUAC UACG CAUC GG GAG CAU AUAA UUA C...
output:
2 6 1 1 2 13 1 1 4 1 3 2 2 1 1 6 13 1 1 6 2 11 1 4 1 1 8 3 4 1 1 2 2 1 6 1 1 1 3 11 2 2 2 1 1 6 1 6 1 1 8 1 1 1 3 1 2 2 1 1 4 8 1 1 13 1 3 2 1 1 11 3 13 1 1 11 1 4 2 1 13 2 6 11 1 1 6 3 1 1 13 4 3 6 11 6 3 1 1 2
result:
ok 100 lines
Subtask #2:
score: 0
Time Limit Exceeded
Dependency #1:
100%
Accepted
Test #8:
score: 0
Time Limit Exceeded
input:
2000 2000 ACUGGAACAGAAUCGUUCGAAUCAACAUUUCCCUCGCUUUUGGCUCCCCUCAGUAAUCUUUGUAGUACCGGCCUUCGCGAUCUGCGGCCAUCCCCUCCCAGUUGUUCGAGUUAGGGCAUGCUCUGCAGCGUUGAUUCAGUC ACUGGAACAGAAUCGUUCGAAUCAACAUUUCCCUCGCUUUUGGCUCCCCUCAGUAAUCUUUGUAGUACCGGCCUUCGCGAUCUGCGGCCAUCCCCUCCCAGUUGUUCGAGUUAGGGCAUGCUCUGCAGCGUUGAUUCAGCAGAUCCUA...
output:
result:
Subtask #3:
score: 25
Accepted
Dependency #1:
100%
Accepted
Test #18:
score: 25
Accepted
time: 16ms
memory: 11924kb
input:
50000 50000 UA UU AU AU UA UA AU UU UU UU UU UU UU UA UU UU UA UA UU UU UA UU UU UU AU UA UA AU UU UU UU UA UU AU UU UU AU UA UU UA UU AU UU AU UU UU UA UU UA UU UA UA UU AU AU UA UU UU AU UU UA UU UU UU UA UU UU UU UU UU UA UA UA AU UU AU UU UA UA AU UA UU AU UU AU UA UU UA UU UU UU UU UA AU UA UU ...
output:
12440 12410 12410 12440 25150 25150 25150 12410 25150 25150 25150 25150 12440 12440 12440 12440 25150 12440 12440 12410 12410 12440 25150 12410 25150 25150 25150 12440 25150 25150 25150 12410 12440 12440 12410 12410 25150 12410 12440 25150 25150 12440 25150 12410 12410 25150 25150 25150 12410 25150 ...
result:
ok 50000 lines
Test #19:
score: 25
Accepted
time: 22ms
memory: 9260kb
input:
30000 30000 CAUA CAUUGUCCGGCUAG CAUUGUAU CAUUGUCCGUA CAUUGUCCGGCUCA CAUUGUCCGGCAC CAUUGUCCGGCUAUA CAUUGUCCGGCCU GC CAUUGUCC CAUUGUCCGGCUAAGC CAUUC CAUUGGA CAUUUU CAUUGUCCGGCUACA CAUUUC CAUUGC CAA CAUUGUGC CAU CACU UCC CAUUGUCCGGAGA CAUUGUCCAC CAUUGCC CAUUGUCCGGCUCAA CAUUGUCCUG CAUUGUCCC CAUUGC CCGC ...
output:
338 549 3133 422 1095 505 91 212 219 3205 364 2347 1001 197 217 173 175 1810 635 231 12 121 893 276 66 24 43 1444 302 25 259 555 329 125 181 121 33 791 231 487 578 16 2251 7317 2242 505 189 30 2596 549 555 541 26 132 1810 958 50 422 32 7317 789 189 102 1824 72 29 268 1059 338 41 386 2347 29 3202 728...
result:
ok 30000 lines
Test #20:
score: 25
Accepted
time: 21ms
memory: 10776kb
input:
40000 40000 U AGUA AGUGUC CUA AGCGU AGUGUCUAGAA AGUGUCUAAAU AGUGUCUGUG AGCUA AGUGUCUAAG GCG AGUGUCUACCUG AGUGUCCGG AGUGUCUACCAA A C AGUGUCGCU AGUGUA AGUGUCUACCG AGUGUCUU AGUGUCUACAGC AUC AGUGUCUACCAA AGUGUCUCAU AGCC UG AGUGUCUACCAU AG AGUGUCUC A AGUAA AGUGUCUACCAU AA AGUGGC AU AGUGCC AGUGUCUAAC UCU ...
output:
3325 47 15 58 147 123 2658 5 142 190 128 119 682 3284 9626 57 178 36 2638 244 1773 820 2113 241 320 470 1568 905 703 898 360 321 1072 21 1355 514 21 174 3325 345 920 116 119 3002 563 232 870 12 12 107 58 58 319 282 143 29 360 2658 75 230 67 920 360 455 59 330 124 171 2280 771 122 116 18 2088 2088 20...
result:
ok 40000 lines
Test #21:
score: 25
Accepted
time: 19ms
memory: 9012kb
input:
33322 33245 UAA AUU UUA AUA UAA AAU AUA UUA UAU AUU UAU AUU AAU AUA AUU AAU UAU AUU UUA UUA UAU AUU AUA AUA AUU UAU AAU UAA AUA UAA UAU AUU AAU UAU AAU AUA AAU AAU AAU AUU AUA UUA AAU UAA UUA UAU AUU UAA AUU AUU UAU UUA UUA UAA UAA UAA AUA AUU UAA UAU UAA UUA AUU UUA AUA UAU AAU UAA AUU AUU AAU UAU ...
output:
3685 7413 7492 7360 7492 3640 3732 3685 7360 7360 7413 3685 3640 7492 7360 3685 7413 3640 7492 3732 7492 7492 7413 7492 7413 7413 7492 7413 3732 7492 7360 7492 3640 7492 3732 7492 7492 7492 7360 7492 7360 7492 3685 7492 7360 7413 7360 7360 7492 7360 7492 7492 7413 3640 3640 7413 7413 3732 3640 7413 ...
result:
ok 33245 lines
Test #22:
score: 25
Accepted
time: 21ms
memory: 9756kb
input:
30000 30000 CCUAAGUUU GU AU GCUGGAGCAUAAGUUU AUU GGGU GUAAGUUU AAU GGGCAUAAGUUU AUAAGUUU AUUAGCAUAAGUUU UGUAAGUUU AAA U UGCAUAAGUUU UGAGAGCAUAAGUUU CCUGGAGCAUAAGUUU CCAGCAUAAGUUU AAUGUUU UAAGAGCAUAAGUUU GAGCAUAAGUUU GGUUU CUU GGGUUU AAGUUU GC UGUAGCAUAAGUUU CGGGAGCAUAAGUUU CGGUUU ACCCAUAAGUUU ACGAGC...
output:
160 96 78 1268 261 3199 379 19 834 1822 1039 414 267 1380 11 685 7328 183 827 56 173 462 115 96 319 2236 235 2166 99 1422 56 1039 151 196 2236 42 544 9 41 185 606 190 1039 3199 290 615 79 170 3153 204 264 624 35 183 573 11 203 3153 211 649 1268 199 591 924 67 205 11 597 2404 92 749 54 409 924 597 31...
result:
ok 30000 lines
Test #23:
score: 25
Accepted
time: 27ms
memory: 11032kb
input:
40000 40000 GAGCGGAG GUGGAG UGCCACCGGAG CCGGAG CAACCGGAG GAGACCGGAG GCUACCGGAG UG CGUACCGGAG UCGAG GACCACCGGAG CAG UCGGAG ACAG UUACCACCGGAG AGAACCGGAG GUACACCGGAG CG UCGGAG GAGCGGAG CUACCACCGGAG UGCCACCGGAG GCCCGGAG AGCG GCCCACCGGAG CA GACCACCGGAG AACCGGAG GGCCGGAG U AGGAG UGACCACCGGAG AGUAG UACCGGA...
output:
51 9588 767 267 270 127 444 2092 313 1552 20 300 186 418 1763 99 771 928 62 65 94 1552 290 444 733 254 466 43 444 56 254 29 91 25 864 341 2100 1108 51 567 1094 541 1549 1549 193 658 29 7 744 1094 3 137 1549 221 322 1243 541 1342 466 100 5 48 254 10 54 360 381 1185 44 1342 1763 11 221 439 5 207 251 3...
result:
ok 40000 lines
Subtask #4:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%