QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#383378#8571. Palworlducup-team027WA 636ms38116kbC++233.6kb2024-04-09 11:46:582024-04-09 11:46:59

Judging History

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

  • [2024-04-09 11:46:59]
  • 评测
  • 测评结果:WA
  • 用时:636ms
  • 内存:38116kb
  • [2024-04-09 11:46:58]
  • 提交

answer

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

#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()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;

struct SuffixArray {
	vi sa, lcp;
	SuffixArray() {}
	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(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++);
	}
};

struct RMQ {
	vector<vector<int>> jmp;
	RMQ(const vector<int>& 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]);
		}
	}
	RMQ() {}
	int 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 PalRev {
	int n; string t;
	SuffixArray SA;
	RMQ rmq;
	vector<int> sa_id;

	PalRev(string s) {
		n = s.size();
		t = s;
		t += "#";
		reverse(s.begin(), s.end());
		t += s;
		SA = SuffixArray(t);
		rmq = RMQ(SA.lcp);

		sa_id.resize(SA.sa.size());
		for (int i = 0; i < sa_id.size(); i++) {
			sa_id[SA.sa[i]] = i;
		}
	}

	int getMxLen(int a, int b) { // if [a, b) were removed, what's the maximum palindrome centered at removal point
		// abcdef
		// fedcba
		// remove [2, 5)
		// lcp of [5] 1st string
		// vs [4] 2nd string
		int id1 = b;
		int id2 = n-a;

		int l = sa_id[id1];
		int r = sa_id[id2+n+1];
		if (l > r) swap(l, r);
		return rmq.query(l+1, r+1); 
	}
};

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;
}

void solve() {
	int n, k; cin >> n >> k;
	string s; cin >> s;

	auto pls = manacher(s);
	PalRev hrv(s);

	int ans = 0;
	for (int p = 0; p < 2; p++) {
		for (int i = 0; i < pls[p].size(); i++) {
			int pallen = pls[p][i]*2 + p;
			int lp = i - pls[p][i];
			int rp = lp + pallen;
			ans = max(ans, pallen + k);

			for (int offset = 0; offset <= k; offset++) {
				int lpal = lp-offset;
				int rpal = rp;

				if (lpal < 0) break;
				int len = hrv.getMxLen(lpal, rpal);
				ans = max(ans, pallen + offset*2 + len*2);
			}

			for (int offset = 0; offset <= k; offset++) {
				int lpal = lp;
				int rpal = rp+offset;

				if (rpal > n) break;
				int len = hrv.getMxLen(lpal, rpal);
				ans = max(ans, pallen + offset*2 + len*2);
			}
		}
	}
	cout << ans << '\n';
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);
	
	int t; 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: 3612kb

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: 430ms
memory: 38108kb

input:

1
200000 66
jsmwjmibgkjvdscqllsxpaxiycmpauhnzschbivtqbjfrxrqvhvfbqecozjewqqpwdfbeqppjkgxhbnniopkptkygspcdswhwadfwhnzovvpshgcdukrupeztkpxwhmctaquqbxtidzbbxsyuaeikuldaoeudletrsmqptaejibkymsjhmwykqsjdvvdaqwelrcpxwrwhuvodipjniowfofbjktkdezwqqbvwsppsmpilntmdmlxgkaxymnugmmcsljkjzjuudnllydwdwwanadynsoiolso...

output:

141

result:

ok 1 number(s): "141"

Test #3:

score: 0
Accepted
time: 636ms
memory: 38116kb

input:

1
200000 100
qhiaajzxinenucrnfffumuhnovpuwcnojbhsktztapgyivmfqrlntwazwnfetwqieckxcnkskpidltiydfoveaucckximydxxfbiwbdufmbhywqkflyqxbijakadqkzftlciccbpnldsqhtjxuxnvkusvizavuyfhdroiuominebadhfqzrpjnzpgyvkejfwmueiltyeqpvwrkanqknyacqganbszktocfuvvsfrboaennwpaabfdnaurvvurysrijnfaonesihbhrrvbvyhpbremsuhhbc...

output:

209

result:

ok 1 number(s): "209"

Test #4:

score: 0
Accepted
time: 51ms
memory: 3592kb

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: 242ms
memory: 37924kb

input:

1
200000 100
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

200100

result:

ok 1 number(s): "200100"

Test #6:

score: 0
Accepted
time: 539ms
memory: 37952kb

input:

1
200000 73
jgqahsycxmrrwvszqigkxmxxckratkgiyyqbxgjzaqkufvkgmepixhjdugpsntlsgtiedueukiytrytrcvlkymwibsfdhytbffmtaytnfczbavrrbdnpegwxubyeoopzopqhrwukohojtaizsureefffglwjfawvpqqnjteveuscyelgjiukskvymqejzwlnrjgvnpwnttzahoupruedryqeyahxgltnibxhbpxmhxxnmrebsgbjwsfpipezkekaqptnwtuanbfyfwyotwkrvlamnuvhunty...

output:

175682

result:

ok 1 number(s): "175682"

Test #7:

score: -100
Wrong Answer
time: 31ms
memory: 3592kb

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
22
9
5
14
28
8
35
4
14
4
6
11
11
14
5
5
21
16
13
11
2
22
13
3
28
5
23
15
21
16
16
21
3
3
11
23
13
13
8
14
4
22
5
5
13
3
18
27
9
3
24
32
28
3
13
3
30
18
3
7
8
3
15
20
7
13
19
15
3
7
3
15
10
4
7
29
5
3
11
36
23
5
26
22
11
8
12
30
7
9
25
10
3
3
9
12
15
22
10
2
16
15
21
6
31
32
7
35
18
31
11
10
10
...

result:

wrong answer 3rd numbers differ - expected: '23', found: '22'