QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#386077#8571. Palworldhos_lyricAC ✓673ms37980kbC++148.4kb2024-04-11 11:39:542024-04-11 11:39:56

Judging History

你现在查看的是最新测评结果

  • [2024-04-11 11:39:56]
  • 评测
  • 测评结果:AC
  • 用时:673ms
  • 内存:37980kb
  • [2024-04-11 11:39:54]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")

////////////////////////////////////////////////////////////////////////////////
// SA-IS
//   String: string, vector<int>, vector<long long>
//   if sigma <= n,  O(n) time, O(n) space
//   if sigma >  n,  O(n log n) time, O(n) space
template <class String> vector<int> suffixArrayRec(const String &as) {
  const int n = as.size();
  if (n == 0) return {};
  const auto minmaxA = minmax_element(as.begin(), as.end());
  const auto minA = *minmaxA.first, maxA = *minmaxA.second;
  if (static_cast<unsigned long long>(maxA) - minA >=
      static_cast<unsigned long long>(n)) {
    vector<int> us(n);
    for (int u = 0; u < n; ++u) us[u] = u;
    std::sort(us.begin(), us.end(), [&](int u, int v) -> bool {
      return (as[u] < as[v]);
    });
    int b = 0;
    vector<int> bs(n, 0);
    for (int i = 1; i < n; ++i) {
      if (as[us[i - 1]] != as[us[i]]) ++b;
      bs[us[i]] = b;
    }
    return suffixArrayRec(bs);
  }
  const int sigma = maxA - minA + 1;
  vector<int> pt(sigma + 1, 0), poss(sigma);
  for (int u = 0; u < n; ++u) ++pt[as[u] - minA + 1];
  for (int a = 0; a < sigma; ++a) pt[a + 1] += pt[a];
  // cmp[u] := (as[u, n) < as[u + 1, n))
  vector<bool> cmp(n);
  cmp[n - 1] = false;
  for (int u = n - 1; --u >= 0; ) {
    cmp[u] = (as[u] != as[u + 1]) ? (as[u] < as[u + 1]) : cmp[u + 1];
  }
  // ><,  nn - id (0 <= id < n)
  int nn = 0;
  vector<int> ids(n, 0);
  int last = n;
  vector<int> nxt(n, 0);
  // put ><, from the tail of each bucket
  vector<int> us(n, 0);
  memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
  for (int u = n - 1; --u >= 1; ) if (!cmp[u - 1] && cmp[u]) {
    ids[u] = ++nn;
    nxt[u] = last;
    last = u;
    us[--poss[as[u] - minA]] = u;
  }
  // follow > backwards, from the head of each bucket
  memcpy(poss.data(), pt.data(), sigma * sizeof(int));
  us[poss[as[n - 1] - minA]++] = n - 1;
  for (int i = 0; i < n; ++i) {
    const int u = us[i];
    if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1;
  }
  // follow < backwards, from the tail of each bucket
  memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
  for (int i = n; --i >= 0; ) {
    const int u = us[i];
    if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1;
  }
  if (nn) {
    int vsLen = 0;
    vector<int> vs(nn);
    for (const int u : us) if (ids[u]) vs[vsLen++] = u;
    int b = 0;
    vector<int> bs(nn, 0);
    for (int j = 1; j < nn; ++j) {
      // as[v1, w1] (< or =) as[v0, w0]
      int v1 = vs[j - 1], v0 = vs[j];
      const int w1 = nxt[v1], w0 = nxt[v0];
      if (w1 - v1 != w0 - v0) {
        ++b;
      } else {
        for (; ; ++v1, ++v0) {
          if (v1 == n) { ++b; break; }
          if (as[v1] != as[v0]) { ++b; break; }
          if (v1 == w1) break;
        }
      }
      bs[nn - ids[vs[j]]] = b;
    }
    for (int u = 0; u < n; ++u) if (ids[u]) vs[nn - ids[u]] = u;
    const auto sub = suffixArrayRec(bs);
    // put ><, from the tail of each bucket
    memset(us.data(), 0, n * sizeof(int));
    memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
    for (int j = nn; --j >= 0; ) {
      const int u = vs[sub[j]];
      us[--poss[as[u] - minA]] = u;
    }
    // follow > backwards, from the head of each bucket
    memcpy(poss.data(), pt.data(), sigma * sizeof(int));
    us[poss[as[n - 1] - minA]++] = n - 1;
    for (int i = 0; i < n; ++i) {
      const int u = us[i];
      if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1;
    }
    // follow < backwards, from the tail of each bucket
    memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
    for (int i = n; --i >= 0; ) {
      const int u = us[i];
      if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1;
    }
  }
  return us;
}

// us[i]: i-th suffix
// su[u]: index of as[u, n)
// hs[i]: longest common prefix of as[us[i - 1], n) and as[us[i], n)
struct SuffixArray {
  int n;
  bool rmq;
  vector<int> us, su, hs;
  SuffixArray() : n(0), rmq(false) {}
  SuffixArray(const string &as, bool rmq_) : rmq(rmq_) { build(as); }
  SuffixArray(const vector<int> &as, bool rmq_) : rmq(rmq_) { build(as); }
  SuffixArray(const vector<long long> &as, bool rmq_) : rmq(rmq_) { build(as); }
  template <class String> void build(const String &as) {
    n = as.size();
    us = suffixArrayRec(as);
    su.resize(n + 1);
    for (int i = 0; i < n; ++i) su[us[i]] = i;
    su[n] = -1;
    hs.assign(n, 0);
    for (int h = 0, u = 0; u < n; ++u) if (su[u]) {
      for (int v = us[su[u] - 1]; v + h < n && as[v + h] == as[u + h]; ++h) {}
      hs[su[u]] = h;
      if (h) --h;
    }
    if (rmq) {
      const int logN = n ? (31 - __builtin_clz(n)) : 0;
      hs.resize((logN + 1) * n - (1 << logN) + 1);
      for (int e = 0; e < logN; ++e) {
        int *hes = hs.data() + e * n;
        for (int i = 0; i <= n - (1 << (e + 1)); ++i) {
          hes[n + i] = min(hes[i], hes[i + (1 << e)]);
        }
      }
    }
  }
  // Returns longest common prefix of as[u, n) and as[v, n).
  //   0 <= u, v <= n
  //   Assumes rmq.
  inline int lcp(int u, int v) const {
    if (u == v) return n - u;
    int i = su[u], j = su[v];
    if (i > j) swap(i, j);
    const int e = 31 - __builtin_clz(j - i);
    return min(hs[e * n + i + 1], hs[e * n + j + 1 - (1 << e)]);
  }
};
////////////////////////////////////////////////////////////////////////////////

// |as| = n ==> |rs| = 2 n + 1
// [i - rs[i], i + rs[i]] is palindrome for $ as[0] $ as[1] $ ... $ as[n-1] $
// as[i, j): palindrome <=> j - i <= rs[i + j]
template <class String> vector<int> manacher(const String &as) {
  const int n = as.size();
  vector<int> rs(2 * n + 1);
  for (int i = 0, j = 0, k; i <= 2 * n; i += k, j -= k) {
    for (; 0 < i - j && i + j < 2 * n &&
           (!((i + j + 1) & 1) || as[(i - j - 1) >> 1] == as[(i + j + 1) >> 1]);
         ++j) {}
    rs[i] = j;
    for (k = 1; k < j && k + rs[i - k] < j; ++k) rs[i + k] = rs[i - k];
  }
  return rs;
}


char buf[200'010];

int N, K;
string S;

string revS, cat;
SuffixArray sa;
vector<int> R;

int calc(int l, int r) {
// cerr<<"  calc "<<l<<" "<<r<<" = "<<sa.lcp(2 * N + 1 - l, r)<<endl;
  return sa.lcp(2 * N + 1 - l, r);
}

int main() {
  for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    scanf("%d%d", &N, &K);
    scanf("%s", buf);
    S = buf;
    
    revS = S;
    reverse(revS.begin(), revS.end());
    cat = S + "!" + revS;
    sa = SuffixArray(cat, true);
    R = manacher(S);
    
    int ans = 0;
    for (int i = 0; i <= N; ++i) {
      for (int k = 0; k <= K && k <= i  ; ++k) chmax(ans, k + K + 2 * calc(i - k, i));
      for (int k = 0; k <= K && k <= N-i; ++k) chmax(ans, k + K + 2 * calc(i, i + k));
    }
    for (int i = 0; i <= 2*N; ++i) {
      const int l = (i - R[i]) / 2;
      const int r = (i + R[i]) / 2;
// cerr<<l<<" "<<r<<" "<<ans<<endl;
      for (int k = 0; k <= K && k <= l  ; ++k) chmax(ans, (r - l) + 2 * k + 2 * calc(l - k, r));
      for (int k = 0; k <= K && k <= N-r; ++k) chmax(ans, (r - l) + 2 * k + 2 * calc(l, r + k));
    }
    printf("%d\n", ans);
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3820kb

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: 478ms
memory: 37756kb

input:

1
200000 66
jsmwjmibgkjvdscqllsxpaxiycmpauhnzschbivtqbjfrxrqvhvfbqecozjewqqpwdfbeqppjkgxhbnniopkptkygspcdswhwadfwhnzovvpshgcdukrupeztkpxwhmctaquqbxtidzbbxsyuaeikuldaoeudletrsmqptaejibkymsjhmwykqsjdvvdaqwelrcpxwrwhuvodipjniowfofbjktkdezwqqbvwsppsmpilntmdmlxgkaxymnugmmcsljkjzjuudnllydwdwwanadynsoiolso...

output:

141

result:

ok 1 number(s): "141"

Test #3:

score: 0
Accepted
time: 673ms
memory: 37680kb

input:

1
200000 100
qhiaajzxinenucrnfffumuhnovpuwcnojbhsktztapgyivmfqrlntwazwnfetwqieckxcnkskpidltiydfoveaucckximydxxfbiwbdufmbhywqkflyqxbijakadqkzftlciccbpnldsqhtjxuxnvkusvizavuyfhdroiuominebadhfqzrpjnzpgyvkejfwmueiltyeqpvwrkanqknyacqganbszktocfuvvsfrboaennwpaabfdnaurvvurysrijnfaonesihbhrrvbvyhpbremsuhhbc...

output:

209

result:

ok 1 number(s): "209"

Test #4:

score: 0
Accepted
time: 108ms
memory: 3852kb

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: 243ms
memory: 37628kb

input:

1
200000 100
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

200100

result:

ok 1 number(s): "200100"

Test #6:

score: 0
Accepted
time: 515ms
memory: 37980kb

input:

1
200000 73
jgqahsycxmrrwvszqigkxmxxckratkgiyyqbxgjzaqkufvkgmepixhjdugpsntlsgtiedueukiytrytrcvlkymwibsfdhytbffmtaytnfczbavrrbdnpegwxubyeoopzopqhrwukohojtaizsureefffglwjfawvpqqnjteveuscyelgjiukskvymqejzwlnrjgvnpwnttzahoupruedryqeyahxgltnibxhbpxmhxxnmrebsgbjwsfpipezkekaqptnwtuanbfyfwyotwkrvlamnuvhunty...

output:

175682

result:

ok 1 number(s): "175682"

Test #7:

score: 0
Accepted
time: 68ms
memory: 4180kb

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: 67ms
memory: 4156kb

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: 262ms
memory: 3996kb

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: 663ms
memory: 37572kb

input:

1
200000 100
cbdadabcbdaaacbedaccdeeeaabececdbcdebcedaeacddcdeaacaccedeadecacaabbdbdbbbbbeaecddbcaaeeaddcabcbebecebcddeccaeceacdebecaddcaddaaadaecabcbbbababedbbccbaceadaeccbbceaeacdccaaaabcbdaebcbcacaaeebebdddeeeeddcaaadbbbccacecacccebeeaabaecbdeccccbdbeedcddececabeaadeaadadcccbeebaecbacbccdbaceebdb...

output:

222

result:

ok 1 number(s): "222"

Test #11:

score: 0
Accepted
time: 655ms
memory: 37640kb

input:

1
200000 100
bcacdaadcdbcaeeebacabdaadebbceabaacdebddeacdebaabdccacdcacadbceceecdccebdebdbccdadbebbaebcabeddeabbdebdadcdedaadedbccbbdbcccdceddeadccdaadeecbebddcddccccbebccbbadaaadeeabaedecdacbeaabadcdeaeddadecdeddbeedeccaabaeebebdbecceaecbdcebceaadeebeaaceadebedcbdbdbcdbbecabddecabcdbccbbbecdaceabdd...

output:

220

result:

ok 1 number(s): "220"

Extra Test:

score: 0
Extra Test Passed