/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Accepted 73ms 568.0 KiB
#3 Accepted 35ms 320.0 KiB
#4 Accepted 29ms 580.0 KiB
#5 Accepted 38ms 1.59 MiB
#6 Accepted 45ms 10.863 MiB
#7 Accepted 45ms 10.859 MiB
#8 Accepted 55ms 10.941 MiB
#9 Accepted 53ms 10.898 MiB
#10 Accepted 59ms 11.047 MiB
#11 Accepted 51ms 10.875 MiB
#12 Accepted 52ms 10.922 MiB
#13 Accepted 57ms 11.059 MiB
#14 Accepted 60ms 11.023 MiB
#15 Accepted 55ms 10.992 MiB
#16 Accepted 50ms 10.871 MiB
#17 Accepted 47ms 10.938 MiB
#18 Accepted 57ms 11.07 MiB
#19 Accepted 58ms 10.91 MiB
#20 Accepted 56ms 11.023 MiB

Code

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

#define fr freopen ("input19.txt","r",stdin)
//ofstream file("output19.txt");

bool check(vector<vector<int>> &dp,int n,int k,int mid)
{
    for(int i=0;i<26;i++)
    {
        if(dp[i][mid-1] <= k) return true;
        for(int j=mid;j<n;j++)
        {
            if(dp[i][j] - dp[i][j-mid] <= k) return true;
        }
    } 
    return false;
}

void solve()
{
    int n,k; cin>>n>>k;
    string s; cin>>s;
    // hashing the characters
    int ch[26];
    for(int i=0;i<26;i++) ch[i] = i;
    vector<vector<int>> dp(26,vector<int>(n));

    // mapping the next character
    map<int,int> mp;
    for(int i=0;i<25;i++) mp[i]=i+1;
    mp[25] = 0;

    // first row of dp
    for(int i=0;i<26;i++) dp[i][0] = ((s[0]-'a') != i);
    
    for(int i=1;i<n;i++)
    {
        for(int j=0;j<26;j++)
        {
            // shifting characters to the next
            ch[j] = mp[ch[j]];
            dp[j][i] = dp[j][i-1] + (s[i]-'a' != ch[j]);
        }
    }

    /*for(int i=0;i<26;i++)
    {
        for(int j=0;j<n;j++) cout<<dp[i][j]<<" "; cout<<endl;
    }*/
    
    int ans = 0,lo = 0, hi = n, mid;
    while(lo<=hi)
    {
        mid = (lo+hi)/2;
        if(check(dp,n,k,mid))
        {
            ans = max(ans,mid);
            lo = mid + 1;
        }
        else hi = mid - 1;
    }
    
    cout<<ans<<endl;
    //file<<ans<<endl;
}

int main()
{
    //fr;
    int t; cin>>t; while(t--) solve();
}

Information

Submit By
Type
Submission
Problem
P1068 Alphabetical substring
Language
C++20 (G++ 13.2.0)
Submit At
2024-07-08 21:48:33
Judged At
2024-10-03 13:43:43
Judged By
Score
100
Total Time
73ms
Peak Memory
11.07 MiB