QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#379135 | #8571. Palworld | ucup-team180# | AC ✓ | 648ms | 45616kb | C++14 | 5.8kb | 2024-04-06 16:19:11 | 2024-04-06 16:19:12 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
vector<int> suffix_array(const vector<int> &A, int mx){
int N = A.size();
vector<int> sum(mx + 1, 0);
for (int i = 0; i < N; i++){
sum[A[i] + 1]++;
}
for (int i = 0; i < mx; i++){
sum[i + 1] += sum[i];
}
vector<bool> is_s(N);
is_s[N - 1] = false;
for (int i = N - 2; i >= 0; i--){
is_s[i] = A[i] < A[i + 1] || A[i] == A[i + 1] && is_s[i + 1];
}
vector<int> id(N, -1);
vector<int> pos;
int M = 0;
for (int i = 1; i < N; i++){
if (is_s[i] && !is_s[i - 1]){
id[i] = M;
pos.push_back(i);
M++;
}
}
vector<int> sa(N);
auto induce = [&](vector<int>& lms){
sa = vector<int>(N, -1);
vector<bool> used(N, false);
vector<int> p(mx);
vector<int> p2(mx);
for (int i = 0; i < mx; i++){
p[i] = sum[i + 1] - 1;
p2[i] = sum[i];
}
for (int i = M - 1; i >= 0; i--){
sa[p[A[lms[i]]]] = lms[i];
p[A[lms[i]]]--;
used[lms[i]] = true;
}
sa[p2[A[N - 1]]] = N - 1;
p2[A[N - 1]]++;
used[N - 1] = true;
for (int i = 0; i < N; i++){
if (sa[i] > 0){
if (!is_s[sa[i] - 1] && !used[sa[i] - 1]){
sa[p2[A[sa[i] - 1]]] = sa[i] - 1;
p2[A[sa[i] - 1]]++;
used[sa[i] - 1] = true;
}
}
}
for (int i = 0; i < N; i++){
if (sa[i] != -1){
if (id[sa[i]] != -1){
used[sa[i]] = false;
sa[i] = -1;
}
}
}
for (int i = 0; i < mx; i++){
p[i] = sum[i + 1] - 1;
}
for (int i = N - 1; i >= 0; i--){
if (sa[i] > 0){
if (is_s[sa[i] - 1] && !used[sa[i] - 1]){
sa[p[A[sa[i] - 1]]] = sa[i] - 1;
p[A[sa[i] - 1]]--;
used[sa[i] - 1] = true;
}
}
}
};
induce(pos);
if (M == 0){
return sa;
}
vector<int> lms;
for (int i = 0; i < N; i++){
if (id[sa[i]] != -1){
lms.push_back(sa[i]);
}
}
vector<int> c(M);
c[0] = 0;
for (int i = 0; i < M - 1; i++){
c[i + 1] = c[i];
int x = lms[i];
int y = lms[i + 1];
bool ok = true;
while (x < N && y < N){
if (A[x] != A[y]){
ok = false;
break;
}
x++;
y++;
if (x == N || y == N){
break;
}
if (id[x] != -1){
if (id[y] == -1){
ok = false;
}
break;
}
}
if (x == N || y == N){
ok = false;
}
if (!ok){
c[i + 1]++;
}
}
vector<int> rec(M);
for (int i = 0; i < M; i++){
rec[id[lms[i]]] = c[i];
}
vector<int> sa2 = suffix_array(rec, c[M - 1] + 1);
vector<int> pos2(M);
for (int i = 0; i < M; i++){
pos2[i] = pos[sa2[i]];
}
induce(pos2);
return sa;
}
vector<int> suffix_array(const string &S){
int N = S.size();
vector<int> A(N);
for (int i = 0; i < N; i++){
A[i] = S[i];
}
return suffix_array(A, 256);
}
template <typename T>
vector<int> lcp_array(const T &A, vector<int> &SA){
int N = A.size();
vector<int> rank(N);
for (int i = 0; i < N; i++){
rank[SA[i]] = i;
}
vector<int> lcp(N - 1, 0);
int h = 0;
for (int i = 0; i < N; i++){
if (rank[i] > 0){
int prev = SA[rank[i] - 1];
if (h > 0){
h--;
}
while (i + h < N && prev + h < N){
if (A[i + h] != A[prev + h]){
break;
}
h++;
}
lcp[rank[i] - 1] = h;
}
}
return lcp;
}
template <typename T>
vector<int> manacher(const T &A){
int N = A.size();
vector<int> ans(N, 0);
int i = 0, j = 0;
while (i < N){
while (i >= j && i + j < N && A[i - j] == A[i + j]){
j++;
}
ans[i] = j;
int k = 1;
while (i >= k && i + k < N && k + ans[i - k] < j){
ans[i + k] = ans[i - k];
k++;
}
i += k;
j -= k;
}
return ans;
}
template <typename T>
struct sparse_table{
vector<vector<T>> ST;
sparse_table(){
}
sparse_table(vector<T> &A){
int N = A.size();
int LOG = 32 - __builtin_clz(N);
ST = vector<vector<T>>(LOG, vector<T>(N));
for (int i = 0; i < N; i++){
ST[0][i] = A[i];
}
for (int i = 0; i < LOG - 1; i++){
for (int j = 0; j < N - (1 << i); j++){
ST[i + 1][j] = min(ST[i][j], ST[i][j + (1 << i)]);
}
}
}
int query(int L, int R){
int d = 31 - __builtin_clz(R - L);
return min(ST[d][L], ST[d][R - (1 << d)]);
}
};
int main(){
int t;
cin >> t;
for (int i = 0; i < t; i++){
int n, k;
cin >> n >> k;
string S;
cin >> S;
string S1;
for (int j = 0; j < n; j++){
S1 += S[j];
if (j < n - 1){
S1 += "$";
}
}
string S2 = S;
for (int j = 0; j < n; j++){
S2 += S[n - 1 - j];
}
vector<int> M = manacher(S1);
vector<int> SA = suffix_array(S2);
vector<int> LCP = lcp_array(S2, SA);
vector<int> rank(n * 2);
for (int j = 0; j < n * 2; j++){
rank[SA[j]] = j;
}
sparse_table<int> ST(LCP);
auto get = [&](int l, int r){
if (l == 0){
return 0;
}
int L = rank[r], R = rank[n * 2 - l];
return min(ST.query(min(L, R), max(L, R)), n - r);
};
int ans = 0;
for (int j = 0; j < n; j++){
for (int l = j; l <= min(j + k, n); l++){
ans = max(ans, l - j + get(j, l) + k);
}
}
for (int j = 0; j < n * 2 - 1; j++){
int l = (j - M[j] + 2) / 2, r = (j + M[j] + 1) / 2;
for (int m = r; m <= min(r + k, n); m++){
ans = max(ans, get(l, m) * 2 + (r - l) + (m - r) * 2);
}
for (int m = l; m >= max(l - k, 0); m--){
ans = max(ans, get(m, r) * 2 + (r - l) + (l - m) * 2);
}
}
cout << ans << endl;
}
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3592kb
input:
4 1 3 a 4 1 icpc 4 2 icpc 8 4 icecream
output:
4 5 5 11
result:
ok 4 number(s): "4 5 5 11"
Test #2:
score: 0
Accepted
time: 467ms
memory: 45616kb
input:
1 200000 66 jsmwjmibgkjvdscqllsxpaxiycmpauhnzschbivtqbjfrxrqvhvfbqecozjewqqpwdfbeqppjkgxhbnniopkptkygspcdswhwadfwhnzovvpshgcdukrupeztkpxwhmctaquqbxtidzbbxsyuaeikuldaoeudletrsmqptaejibkymsjhmwykqsjdvvdaqwelrcpxwrwhuvodipjniowfofbjktkdezwqqbvwsppsmpilntmdmlxgkaxymnugmmcsljkjzjuudnllydwdwwanadynsoiolso...
output:
141
result:
ok 1 number(s): "141"
Test #3:
score: 0
Accepted
time: 648ms
memory: 44996kb
input:
1 200000 100 qhiaajzxinenucrnfffumuhnovpuwcnojbhsktztapgyivmfqrlntwazwnfetwqieckxcnkskpidltiydfoveaucckximydxxfbiwbdufmbhywqkflyqxbijakadqkzftlciccbpnldsqhtjxuxnvkusvizavuyfhdroiuominebadhfqzrpjnzpgyvkejfwmueiltyeqpvwrkanqknyacqganbszktocfuvvsfrboaennwpaabfdnaurvvurysrijnfaonesihbhrrvbvyhpbremsuhhbc...
output:
209
result:
ok 1 number(s): "209"
Test #4:
score: 0
Accepted
time: 107ms
memory: 3600kb
input:
10000 21 7 fhcjhdjcfbfbdeeheibhg 18 8 hccgjfaadefhjhcijc 15 7 ahefiiheaigjiid 15 3 fgjjebidbfgbdij 15 5 jccebbgjbhifjhb 20 2 hcbddhibgfccjjcfebcc 19 1 ehbdgiicchijebidbcd 20 8 gbcfghefhbjggecdhcbj 17 2 jjaeaccjbcfiehfdd 23 4 iafjedfbebbhcfgfjbehdaf 22 1 jgiiiaacehcbaiehfggcfi 17 2 jefbbfigfhbhfcici ...
output:
17 21 17 11 13 9 6 18 9 11 6 9 22 8 21 23 5 23 7 16 9 23 25 11 17 12 9 5 20 5 23 12 7 13 16 17 21 21 21 12 16 21 21 8 9 11 21 15 5 24 13 7 21 13 11 8 7 19 25 7 8 12 13 22 15 14 7 15 7 20 15 13 11 15 5 11 23 17 17 19 15 7 19 17 25 13 23 10 17 21 9 21 7 23 21 13 16 7 14 10 13 10 9 11 7 21 15 19 11 23 ...
result:
ok 10000 numbers
Test #5:
score: 0
Accepted
time: 135ms
memory: 41668kb
input:
1 200000 100 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
200100
result:
ok 1 number(s): "200100"
Test #6:
score: 0
Accepted
time: 541ms
memory: 45488kb
input:
1 200000 73 jgqahsycxmrrwvszqigkxmxxckratkgiyyqbxgjzaqkufvkgmepixhjdugpsntlsgtiedueukiytrytrcvlkymwibsfdhytbffmtaytnfczbavrrbdnpegwxubyeoopzopqhrwukohojtaizsureefffglwjfawvpqqnjteveuscyelgjiukskvymqejzwlnrjgvnpwnttzahoupruedryqeyahxgltnibxhbpxmhxxnmrebsgbjwsfpipezkekaqptnwtuanbfyfwyotwkrvlamnuvhunty...
output:
175682
result:
ok 1 number(s): "175682"
Test #7:
score: 0
Accepted
time: 75ms
memory: 3580kb
input:
10000 12 1 jelwdrimpvtq 19 8 qcogumialfqhaahfhtb 11 12 tbnfsiqbibv 5 4 hdehb 4 1 ivxi 7 8 nonrwqa 14 16 obvpfqjekvmnyq 4 5 fzhi 18 17 gploorpjnrrhcrgxlp 2 3 pf 7 9 uiliuyr 1 3 v 3 3 bgj 9 4 tektrwtmk 10 4 vvkvbzpbye 20 5 jqzqpqiwkssksbilayvj 2 4 kl 5 2 vptsk 20 9 kuiwsxhfmirnrhzuysqd 8 8 aairrgjl 15...
output:
3 20 23 9 5 15 30 9 35 5 16 4 6 11 11 14 6 5 21 16 13 11 2 23 13 3 30 5 23 15 21 16 18 21 3 3 11 23 13 13 8 14 4 22 6 5 13 3 18 27 9 3 24 32 28 3 13 3 31 18 3 7 8 3 15 20 7 13 19 15 3 7 3 15 12 4 7 29 5 3 11 36 23 5 26 22 11 8 12 30 7 9 25 11 3 3 9 12 15 22 10 2 17 15 21 7 31 33 7 35 18 31 11 10 10 ...
result:
ok 10000 numbers
Test #8:
score: 0
Accepted
time: 78ms
memory: 3824kb
input:
20480 1 1 d 1 2 d 1 1 z 1 2 z 2 1 pp 2 2 pp 2 3 pp 2 1 zp 2 2 zp 2 3 zp 2 1 pz 2 2 pz 2 3 pz 2 1 zz 2 2 zz 2 3 zz 3 1 www 3 2 www 3 3 www 3 4 www 3 1 mww 3 2 mww 3 3 mww 3 4 mww 3 1 wmw 3 2 wmw 3 3 wmw 3 4 wmw 3 1 mmw 3 2 mmw 3 3 mmw 3 4 mmw 3 1 wwm 3 2 wwm 3 3 wwm 3 4 wwm 3 1 mwm 3 2 mwm 3 3 mwm 3 ...
output:
2 3 2 3 3 4 5 3 4 5 3 4 5 3 4 5 4 5 6 7 4 5 6 7 4 5 6 7 4 5 6 7 4 5 6 7 4 5 6 7 4 5 6 7 4 5 6 7 5 6 7 8 9 5 6 7 8 9 5 6 7 8 9 4 6 7 8 9 5 6 7 8 9 5 5 7 8 9 5 6 7 8 9 5 6 7 8 9 5 6 7 8 9 5 6 7 8 9 5 5 7 8 9 5 6 7 8 9 4 6 7 8 9 5 6 7 8 9 5 6 7 8 9 5 6 7 8 9 6 7 8 9 10 11 6 7 8 9 10 11 6 7 8 9 10 11 5 ...
result:
ok 20480 numbers
Test #9:
score: 0
Accepted
time: 196ms
memory: 3792kb
input:
2000 88 14 hbwgbmjtqmgdpbgsdkjndhwjrhluountepcjpecnaogiamrzmnomjcgzzvdrzsfjspyxpooxhjtzlqtzvpeydwap 104 88 osirzoiablktpnobqvhckmttlpbgaeakzhemoxgteqbkmfzaerhqfwkoqpsiqqymmbmufxhvfhovkhdduzybzzhqnvbmswqepkqzrkox 82 25 rumtzeygumtvcmyiowwmxkjekajxpubgazyxabibdirnbsmzsjawtaksrydmuroohkrrajlqqyxogtvuul...
output:
31 177 53 71 113 9 153 107 97 51 149 153 121 193 111 38 129 175 99 183 91 183 115 182 203 41 193 155 159 11 149 145 111 19 76 152 67 107 79 15 49 175 149 63 115 59 45 39 138 37 102 14 23 157 75 175 31 33 65 95 171 61 59 34 73 107 195 185 113 11 65 16 128 184 59 139 23 131 115 135 143 166 42 34 49 29...
result:
ok 2000 numbers
Test #10:
score: 0
Accepted
time: 631ms
memory: 44904kb
input:
1 200000 100 cbdadabcbdaaacbedaccdeeeaabececdbcdebcedaeacddcdeaacaccedeadecacaabbdbdbbbbbeaecddbcaaeeaddcabcbebecebcddeccaeceacdebecaddcaddaaadaecabcbbbababedbbccbaceadaeccbbceaeacdccaaaabcbdaebcbcacaaeebebdddeeeeddcaaadbbbccacecacccebeeaabaecbdeccccbdbeedcddececabeaadeaadadcccbeebaecbacbccdbaceebdb...
output:
222
result:
ok 1 number(s): "222"
Test #11:
score: 0
Accepted
time: 648ms
memory: 45128kb
input:
1 200000 100 bcacdaadcdbcaeeebacabdaadebbceabaacdebddeacdebaabdccacdcacadbceceecdccebdebdbccdadbebbaebcabeddeabbdebdadcdedaadedbccbbdbcccdceddeadccdaadeecbebddcddccccbebccbbadaaadeeabaedecdacbeaabadcdeaeddadecdeddbeedeccaabaeebebdbecceaecbdcebceaadeebeaaceadebedcbdbdbcdbbecabddecabcdbccbbbecdaceabdd...
output:
220
result:
ok 1 number(s): "220"
Extra Test:
score: 0
Extra Test Passed