/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 171ms 96.199 MiB
#2 Accepted 845ms 96.324 MiB
#3 Time Exceeded ≥1075ms ≥96.328 MiB
#4 Time Exceeded ≥1099ms ≥96.324 MiB
#5 Time Exceeded ≥1070ms ≥96.32 MiB
#6 Accepted 838ms 96.328 MiB
#7 Accepted 455ms 96.332 MiB
#8 Accepted 423ms 96.332 MiB
#9 Accepted 201ms 96.332 MiB
#10 Accepted 141ms 96.336 MiB
#11 Accepted 164ms 96.332 MiB
#12 Accepted 139ms 96.328 MiB
#13 Accepted 138ms 96.328 MiB
#14 Accepted 136ms 96.332 MiB
#15 Accepted 136ms 96.332 MiB
#16 Accepted 143ms 96.16 MiB
#17 Accepted 143ms 96.332 MiB
#18 Time Exceeded ≥1014ms ≥96.332 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-10-03 12:56:26
Judged By
Score
78
Total Time
≥1099ms
Peak Memory
≥96.336 MiB