/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 69ms 548.0 KiB
#3 Accepted 34ms 532.0 KiB
#4 Accepted 29ms 532.0 KiB
#5 Accepted 39ms 1.594 MiB
#6 Accepted 45ms 11.02 MiB
#7 Accepted 45ms 11.094 MiB
#8 Accepted 54ms 11.059 MiB
#9 Accepted 55ms 11.051 MiB
#10 Accepted 59ms 11.0 MiB
#11 Accepted 50ms 10.977 MiB
#12 Accepted 50ms 10.887 MiB
#13 Accepted 56ms 11.062 MiB
#14 Accepted 62ms 11.07 MiB
#15 Accepted 54ms 10.848 MiB
#16 Accepted 48ms 11.062 MiB
#17 Accepted 45ms 10.887 MiB
#18 Accepted 58ms 11.078 MiB
#19 Accepted 59ms 10.941 MiB
#20 Accepted 55ms 10.891 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-11-11 03:28:51
Judged By
Score
100
Total Time
69ms
Peak Memory
11.094 MiB