/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 580.0 KiB
#3 Accepted 2ms 532.0 KiB
#4 Accepted 5ms 532.0 KiB
#5 Accepted 6ms 788.0 KiB
#6 Accepted 29ms 3.27 MiB
#7 Accepted 67ms 8.352 MiB
#8 Accepted 65ms 8.27 MiB
#9 Accepted 233ms 34.293 MiB
#10 Accepted 601ms 96.523 MiB
#11 Accepted 582ms 96.52 MiB
#12 Accepted 511ms 96.52 MiB
#13 Accepted 607ms 96.652 MiB
#14 Accepted 589ms 96.578 MiB
#15 Accepted 583ms 96.625 MiB
#16 Accepted 563ms 96.633 MiB
#17 Accepted 543ms 96.656 MiB
#18 Accepted 2ms 532.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-10-03 17:34:35
Judged By
Score
100
Total Time
607ms
Peak Memory
96.656 MiB