QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#471571#8571. Palworlducup-team2307#WA 803ms42552kbC++203.5kb2024-07-10 22:18:432024-07-10 22:18:43

Judging History

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

  • [2024-07-10 22:18:43]
  • 评测
  • 测评结果:WA
  • 用时:803ms
  • 内存:42552kb
  • [2024-07-10 22:18:43]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
//#define int ll
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pii = pair<int, int>;
using vi = vector<int>;
#define fi first
#define se second
#define pb push_back

array<vi, 2> manacher(const string& s) {
    int n = sz(s);
    array<vi,2> p = {vi(n+1), vi(n)};
    rep(z,0,2) for (int i=0,l=0,r=0; i < n; i++) {
            int t = r-i+!z;
            if (i<r) p[z][i] = min(t, p[z][l+t]);
            int L = i-p[z][i], R = i+p[z][i]-!z;
            while (L>=1 && R+1<n && s[L-1] == s[R+1])
                p[z][i]++, L--, R++;
            if (R>r) l=L, r=R;
        }
    return p;
}

template<class T>
struct RMQ {
    vector<vector<T>> jmp;
    RMQ(){}
    RMQ(const vector<T>& V) : jmp(1, V) {
        for (int pw = 1, k = 1; pw * 2 <= sz(V); pw *= 2, ++k) {
            jmp.emplace_back(sz(V) - pw * 2 + 1);
            rep(j,0,sz(jmp[k]))
                jmp[k][j] = min(jmp[k - 1][j], jmp[k - 1][j + pw]);
        }
    }
    T query(int a, int b) {
        assert(a < b); // or return inf if a == b
        int dep = 31 - __builtin_clz(b - a);
        return min(jmp[dep][a], jmp[dep][b - (1 << dep)]);
    }
};

struct SuffixArray {
    vi sa, lcp;
    vi rank;
    RMQ<int> rmq;
    SuffixArray(string& s, int lim=256) { // or basic_string<int>
        int n = sz(s) + 1, k = 0, a, b;
        vi x(all(s)+1), y(n), ws(max(n, lim));
        rank.resize(n);
        sa = lcp = y, iota(all(sa), 0);
        for (int j = 0, p = 0; p < n; j = max(1, j * 2), lim = p) {
            p = j, iota(all(y), n - j);
            rep(i,0,n) if (sa[i] >= j) y[p++] = sa[i] - j;
            fill(all(ws), 0);
            rep(i,0,n) ws[x[i]]++;
            rep(i,1,lim) ws[i] += ws[i - 1];
            for (int i = n; i--;) sa[--ws[x[y[i]]]] = y[i];
            swap(x, y), p = 1, x[sa[0]] = 0;
            rep(i,1,n) a = sa[i - 1], b = sa[i], x[b] =
                        (y[a] == y[b] && y[a + j] == y[b + j]) ? p - 1 : p++;
        }
        rep(i,1,n) rank[sa[i]] = i;
        for (int i = 0, j; i < n - 1; lcp[rank[i++]] = k)
            for (k && k--, j = sa[rank[i] - 1];
                    s[i + k] == s[j + k]; k++);

        rmq=RMQ(lcp);
    }
    int query(int i,int j)
    {
        i=rank[i],j=rank[j];
        if(i>j)
            swap(i,j);
        return rmq.query(i,j);
    }
};

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        string s;
        cin >> s;
        auto [even, odd] = manacher(s);
        string ss = s;
        reverse(all(ss));
        ss += "#" + s;
        auto suf = SuffixArray(ss);
        auto starts = [&](int i) { return n + 1 + i; };
        auto ends = [&](int i) { return n - 1 - i; };
        int ans = 0;
        for (int i = 0; i < n; i++) {
            ans = max(ans, 2 * even[i] + k);
            ans = max(ans, 2 * odd[i] + 1 + k);
            for (auto [l, r]: {pii(i - even[i], i + even[i] - 1), pii(i - odd[i], i + odd[i])})
                for (int m = 0; m <= k; m++)
                    for (auto [L, R]: {pii(l - m, r), pii(l, m + r)})
                        if (L >= 0 && R < n)
                            ans = max(ans, suf.query(starts(R + 1), ends(L - 1)) * 2 + R - L + 1 + m);
        }
        cout<<ans<<"\n";
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 538ms
memory: 42552kb

input:

1
200000 66
jsmwjmibgkjvdscqllsxpaxiycmpauhnzschbivtqbjfrxrqvhvfbqecozjewqqpwdfbeqppjkgxhbnniopkptkygspcdswhwadfwhnzovvpshgcdukrupeztkpxwhmctaquqbxtidzbbxsyuaeikuldaoeudletrsmqptaejibkymsjhmwykqsjdvvdaqwelrcpxwrwhuvodipjniowfofbjktkdezwqqbvwsppsmpilntmdmlxgkaxymnugmmcsljkjzjuudnllydwdwwanadynsoiolso...

output:

141

result:

ok 1 number(s): "141"

Test #3:

score: 0
Accepted
time: 803ms
memory: 42440kb

input:

1
200000 100
qhiaajzxinenucrnfffumuhnovpuwcnojbhsktztapgyivmfqrlntwazwnfetwqieckxcnkskpidltiydfoveaucckximydxxfbiwbdufmbhywqkflyqxbijakadqkzftlciccbpnldsqhtjxuxnvkusvizavuyfhdroiuominebadhfqzrpjnzpgyvkejfwmueiltyeqpvwrkanqknyacqganbszktocfuvvsfrboaennwpaabfdnaurvvurysrijnfaonesihbhrrvbvyhpbremsuhhbc...

output:

209

result:

ok 1 number(s): "209"

Test #4:

score: -100
Wrong Answer
time: 60ms
memory: 3496kb

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:

19
21
17
9
12
10
8
18
9
13
6
9
23
9
24
21
5
23
7
16
9
23
25
11
17
13
14
5
20
5
21
11
9
11
16
16
21
21
19
14
13
21
21
9
11
9
20
15
6
24
11
7
21
12
11
8
9
19
25
7
10
10
13
22
17
14
8
15
7
20
16
13
13
15
5
11
23
19
17
21
15
8
20
17
24
15
24
10
15
22
9
22
7
23
21
13
18
9
13
10
11
11
9
13
7
21
15
19
9
26...

result:

wrong answer 1st numbers differ - expected: '17', found: '19'