/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 532.0 KiB
#2 Accepted 2ms 536.0 KiB
#3 Accepted 2ms 484.0 KiB
#4 Accepted 2ms 584.0 KiB
#5 Accepted 6ms 956.0 KiB
#6 Accepted 34ms 3.516 MiB
#7 Accepted 75ms 8.398 MiB
#8 Accepted 72ms 8.395 MiB
#9 Accepted 259ms 34.461 MiB
#10 Accepted 658ms 96.66 MiB
#11 Accepted 658ms 96.656 MiB
#12 Accepted 569ms 96.652 MiB
#13 Accepted 655ms 96.66 MiB
#14 Accepted 665ms 96.656 MiB
#15 Accepted 646ms 96.656 MiB
#16 Accepted 627ms 96.656 MiB
#17 Accepted 616ms 96.656 MiB
#18 Accepted 2ms 580.0 KiB

Code

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

#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
using ll = long long;

const int INF = 100'000'000;
const int MX = 5005;
int dp[MX][MX];
int a[MX][3];
int n, k;
string s;

int solve(int ind, int cnt) {
  if (cnt == 0) return 0;
  if (ind >= n-2) return INF;

  int& curr = dp[ind][cnt];
  if (curr != -1) return curr;

  curr = solve(ind+1, cnt);
  curr = min(curr, a[ind][0] + a[ind+1][1] + a[ind+2][2] + solve(ind+3, cnt-1));

  return curr;
}

int main() {
  FAST;
  
  int tc = 1, ti;
  cin >> tc;

  for (ti = 1; ti <= tc; ++ti) {
    int i, j, x;
    cin >> n >> k;
    cin >> s;

    for (i = 0; i <= n; ++i) {
      for (j = 0; j <= n; ++j) dp[i][j] = -1;
    }

    for (i = 0; i < n; ++i) {
      for (j = 0; j < 3; ++j) {
        x = s[i]-'a';
        x = j-x;
        if (x < 0) x += 26;
        a[i][j] = x;
      }
    }

    for (i = n; i >= 1; --i) {
      if (solve(0, i) <= k) break;
    }

    cout << i << "\n";
  }

  return 0;
}

Information

Submit By
Type
Submission
Problem
P1100 Substring ABC
Contest
Brain Booster #6
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-03 17:34:09
Judged At
2024-12-17 11:32:40
Judged By
Score
100
Total Time
665ms
Peak Memory
96.66 MiB