QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#699015 | #6516. New but Nostalgic Problem | 0x3fffffff | RE | 0ms | 3576kb | C++23 | 2.4kb | 2024-11-01 23:46:13 | 2024-11-01 23:46:14 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 10;
struct Tire {
vector<array<int, 26>>ch;
vector<int>siz, cnt;
int idx;
Tire() {
idx = 0;
ch.assign(N, {});
cnt.resize(N);
siz.resize(N);
// pos.resize(N);
}
Tire(int n) {
idx = 0;
ch.assign(n + 1, {});
cnt.resize(n + 1);
siz.resize(n + 1);
// pos.resize(n + 1);
}
void insert(string& s, int now) {
int p = 0;
for (auto x : s) {
int c = x - 'a';
if (!ch[p][c])ch[p][c] = ++idx;
p = ch[p][c];
siz[p]++;
}
// pos[p] = now;
cnt[p]++;
}
int query(string& s) {
int p = 0;
for (auto x : s) {
int c = x - 'a';
if (!ch[p][c])return 0;
p = ch[p][c];
}
return cnt[p];
}
void solve(int k) {
int p = 0;
// auto dfs = [&](auto&& ff, int p)->void {
while (1) {
int sum = cnt[p];
for (int i = 0;i < 26;i++) {
if (siz[ch[p][i]])sum++;
}
if (sum >= k) {
if (p == 0) {
cout << "EMPTY\n";return;
}
return;
}
for (int i = 0;i < 26;i++) {
if (siz[ch[p][i]]) {
sum += siz[ch[p][i]] - 1;
if (sum >= k) {
k -= sum - siz[ch[p][i]];
cout << (char)(i + 'a');
p = ch[p][i];
break;
// ff(ff, ch[p][i]);
}
}
}
};
// dfs(dfs, 0);
// cout << "\n";
}
};
void solve() {
int n, k;cin >> n >> k;
vector<string>s(n + 1);
int mx = 0;
for (int i = 1;i <= n;i++) {
cin >> s[i];
mx = max(mx, (int)s[i].size());
}
Tire tr(mx + 10);
for (int i = 1;i <= n;i++) {
tr.insert(s[i], i);
}
tr.solve(k);
cout << "\n";
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
cin >> T;
while (T--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3576kb
input:
2 5 3 gdcpc gdcpcpcp suasua suas sususua 3 3 a b c
output:
gdcpc EMPTY
result:
ok 2 lines
Test #2:
score: -100
Runtime Error
input:
20000 5 3 apveofpr irdbqk rnionnjjrk wrpihlsw ofylfsriof 5 4 iiltghqg kybhogptqf jfnmqxzrdq rpztcluq tzmnrcjae 5 5 ysp vujmkkcutl ksawqeiiaf waukilaq wmlsutlued 5 3 pikybt yiqmqvtjeq tyrsoihf vnvjrxpus cuubpacubb 5 2 rihuvikhg twrsuoervz ukiekoohw eysqgndpf sxswpeqtaf 5 5 vxzhicbp nvtdbgqal jilppvpt...