/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 580.0 KiB
#2 Accepted 28ms 776.0 KiB
#3 Accepted 12ms 580.0 KiB
#4 Accepted 11ms 828.0 KiB
#5 Accepted 14ms 1.566 MiB
#6 Accepted 15ms 10.824 MiB
#7 Accepted 15ms 10.816 MiB
#8 Accepted 24ms 10.75 MiB
#9 Accepted 24ms 10.891 MiB
#10 Accepted 28ms 10.691 MiB
#11 Accepted 21ms 10.664 MiB
#12 Accepted 20ms 10.887 MiB
#13 Accepted 25ms 10.77 MiB
#14 Accepted 32ms 10.883 MiB
#15 Accepted 25ms 10.633 MiB
#16 Accepted 19ms 10.691 MiB
#17 Accepted 16ms 10.82 MiB
#18 Accepted 30ms 10.742 MiB
#19 Accepted 30ms 10.879 MiB
#20 Accepted 25ms 10.695 MiB

Code

#include <bits/stdc++.h>
#define ll long long
#define endll '\n';
#define pb push_back
#define all(v) v.begin(), v.end()
using namespace std;

const int mod = 1e9 + 7, N = 1e6;
int ar[26][N + 3];

void solve()
{
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int starting = 0;
    for (int i = 0; i <= 25; i++)
    {

        starting = i;
        for (int j = 1; j <= n; j++)
        {
            // cout << char(starting + 'a') << "  = " <<  ar[i][j] <<", ";
            if (s[j - 1] - 'a' == starting)
            {
                ar[i][j] = 0;
            }
            else
                ar[i][j] = 1;

            starting++;
            if (starting > 25)
                starting = 0;
        }

        // cout << endll;
    }

    //
    // prefix
    for (int i = 0; i <= 25; i++)
    {

        for (int j = 1; j <= n; j++)
        {

                ar[i][j] += ar[i][j - 1];
        }
        // cout << endll;
    }



       for (int i = 0; i <= 25; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            // cout << ar[i][j] << " ";
        }
        // cout << endll;
    }

    int ans = 0;
    int left = 1, right = n;

    for (int i = 0; i <= 25; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            // cout << ar[i][j] << " ";
        }
        // cout << endll;
    }
    // cout << endll;

    while (left <= right)
    {
        int flg = 0;
        int mid = (left + right) / 2;
        for (int i = 0; i <= 25; i++)
        {
            for (int j = mid; j <= n; j++)
            {

                int cst = ar[i][j] - ar[i][j - mid];
                // cout << cst << " " << mid  << " <  " << j<< endll;



                if (cst <= k)
                {
                    flg = 1;
                    // left = mid + 1;
                    // cout  << " found = " << mid << endll;
                    break;
                }
            }
            if(flg) break;
        }


        if(flg) {
            ans = max (ans, mid);
            left = mid + 1;
        }
        else right = mid - 1;

        
    }

    cout << ans << endl;
}

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1068 Alphabetical substring
Language
C++20 (G++ 13.2.0)
Submit At
2024-07-15 09:59:55
Judged At
2024-11-11 03:21:44
Judged By
Score
100
Total Time
32ms
Peak Memory
10.891 MiB