/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 2ms 532.0 KiB
#4 Accepted 2ms 532.0 KiB
#5 Accepted 2ms 532.0 KiB
#6 Accepted 3ms 532.0 KiB
#7 Accepted 4ms 532.0 KiB
#8 Accepted 5ms 536.0 KiB
#9 Accepted 14ms 532.0 KiB
#10 Accepted 24ms 580.0 KiB
#11 Accepted 14ms 532.0 KiB
#12 Accepted 14ms 532.0 KiB
#13 Accepted 13ms 532.0 KiB
#14 Accepted 13ms 580.0 KiB
#15 Accepted 13ms 364.0 KiB
#16 Accepted 14ms 536.0 KiB
#17 Accepted 13ms 532.0 KiB
#18 Accepted 2ms 352.0 KiB

Code

#include <bits/stdc++.h>

using namespace std;

int get(char a, char b, char c) {
  int ret = 0;
  if (a != 'a') ret += 'z' - a + 1;
  if (b <= 'b') ret += 'b' - b;
  else ret += 'z' - b + 2;
  if (c <= 'c') ret += 'c' - c;
  else ret += 'z' - c + 3;
  return ret;
}

void solve(int cs) {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<vector<int>> dp(4, vector<int>(n / 3 + 5, 1e9));
  dp[0][0] = dp[1][0] = dp[2][0] = dp[3][0] = 0;
  int ans = 0;
  for (int i = 2; i < n; i++) {
    for (int j = 0; j <= i / 3 + 1; j++) {
      int ops = get(s[i - 2], s[i - 1], s[i]);
      dp[i % 4][j] = min(dp[i % 4][j], dp[(i - 1 + 4) % 4][j]);
      dp[i % 4][j + 1] = min(dp[i % 4][j + 1], dp[(i - 3 + 4) % 4][j] + ops);
      if (dp[i % 4][j] <= k) {
        ans = max(ans, j);
      }
    }
  }
  cout << ans << "\n";
}

int32_t main() {
  cin.tie(0) -> sync_with_stdio(0);
  int t = 1;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    solve(i);
  }
  return 0;
}

Information

Submit By
Type
Submission
Problem
P1100 Substring ABC
Language
C++17 (G++ 13.2.0)
Submit At
2025-06-19 20:52:48
Judged At
2025-06-19 20:52:48
Judged By
Score
100
Total Time
24ms
Peak Memory
580.0 KiB