/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 165ms 96.285 MiB
#2 Accepted 821ms 96.312 MiB
#3 Time Exceeded ≥1090ms ≥96.305 MiB
#4 Time Exceeded ≥1093ms ≥96.316 MiB

Code

#include<bits/stdc++.h>
using namespace std;
const long long M=2e3+1,MOD=1000000000;
typedef long long ll;
//int dp[5001][5001];
int limit=5001;
int find_cost(char a, char b, char c){
    int total=0;
    while(a!='a'){
        total++;
        if(a=='z')a='a';
        else a++;
    }
    while(b!='b'){
        if(b=='z')b='a';
        else b++;
        total++;
    }
    while(c!='c'){
        if(c=='z')c='a';
        else c++;
        total++;
    }
    return total;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t=1;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        string s;
        vector<vector<int>>dp(limit,vector<int>(limit,k+1));
        cin>>s;
            for(int j=0;j<=n;j++)dp[j][0]=0;// base case
        for(int i=3;i<=n;i++){
            int cost=find_cost(s[i-3],s[i-2],s[i-1]);// cost for each substring
            int left_index=i-3;
            for(int j=n-2;j>=1;j--){
                dp[i][j]=min(dp[left_index][j],dp[left_index][j-1]+cost);
                dp[i][j]=min(dp[i][j],dp[i-1][j]);
            }
        }
        int res=0;
        for(int i=1;i<=n;i++)if(dp[n][i]<=k)res=i;
            cout<<res<<"\n";

       
    }


   return 0;
 
}

Information

Submit By
Type
Submission
Problem
P1100 Substring ABC
Language
C++20 (G++ 13.2.0)
Submit At
2024-09-16 12:11:30
Judged At
2024-12-17 11:27:22
Judged By
Score
9
Total Time
≥1093ms
Peak Memory
≥96.316 MiB