/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 2ms 2.477 MiB
#3 Accepted 2ms 796.0 KiB
#4 Accepted 2ms 488.0 KiB
#5 Accepted 9ms 4.582 MiB
#6 Accepted 46ms 18.926 MiB
#7 Accepted 118ms 39.25 MiB
#8 Accepted 106ms 38.859 MiB
#9 Accepted 333ms 96.008 MiB
#10 Accepted 702ms 192.223 MiB
#11 Accepted 694ms 192.219 MiB
#12 Accepted 605ms 192.23 MiB
#13 Accepted 715ms 192.234 MiB
#14 Accepted 706ms 192.23 MiB
#15 Accepted 716ms 192.223 MiB
#16 Accepted 692ms 192.23 MiB
#17 Accepted 668ms 192.227 MiB
#18 Accepted 2ms 580.0 KiB

Code

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define ff first
#define ss second
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define f(i,x,y) for(int i=x;i<y;i++)
#define f2(i,x,y) for(int i=x;i>=y;i--)
#define pii pair<int,int>
#define Fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const int MOD =1000000007;
const int INF = 1000000000;
const int N = 5005;
int dp[N][N],mn_cost[N][3];
int n,k;
string s;
// int fun(int ind,int cnt){
//     if(cnt==0)return 0;
//     if(ind+2>=n)return INF;
//     if(dp[ind][cnt]!=-1)return dp[ind][cnt];
//     int way1 = fun(ind+1,cnt);
//     int way2 = mn_cost[ind][0] + mn_cost[ind+1][1] + mn_cost[ind+2][2] + fun(ind+3,cnt-1);
//     return dp[ind][cnt] = min(way1,way2);
// }
int fun(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 = fun(ind+1, cnt);
  curr = min(curr, mn_cost[ind][0] + mn_cost[ind+1][1] + mn_cost[ind+2][2] + fun(ind+3, cnt-1));

  return curr;
}
void solve(int tc){
    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;
        mn_cost[i][j] = x;
      }
    }

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

    cout << i << "\n";

}
int32_t main(){

    Fast

    int t=1;

    cin >> t;

    for(int tc=1;tc<=t;tc++){

        solve(tc);
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1100 Substring ABC
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-03 22:05:40
Judged At
2024-10-03 22:05:40
Judged By
Score
100
Total Time
716ms
Peak Memory
192.234 MiB