/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 24ms 24.02 MiB
#2 Accepted 42ms 24.129 MiB
#3 Accepted 42ms 24.051 MiB
#4 Accepted 43ms 24.359 MiB
#5 Accepted 50ms 26.266 MiB
#6 Accepted 90ms 45.059 MiB
#7 Accepted 89ms 45.051 MiB
#8 Accepted 91ms 45.0 MiB
#9 Accepted 92ms 44.898 MiB
#10 Accepted 92ms 44.988 MiB
#11 Accepted 89ms 45.16 MiB
#12 Accepted 92ms 45.113 MiB
#13 Accepted 91ms 45.051 MiB
#14 Accepted 96ms 44.98 MiB
#15 Accepted 85ms 45.078 MiB
#16 Accepted 91ms 45.039 MiB
#17 Accepted 86ms 44.883 MiB
#18 Accepted 93ms 44.98 MiB
#19 Accepted 92ms 45.07 MiB
#20 Accepted 91ms 45.023 MiB

Code

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1000000007;
#define sz(x) (ll)(x).size()
#define rd ({ll x; cin >> x; x; })
#define dbg(x) cerr << "[" #x "]  " << (x) << "\n\n"
// #define errv(x)                     \
//     {                               \
//         cerr << "[" #x "]  [";      \
//         for (const auto& ___ : (x)) \
//             cerr << ___ << ", ";    \
//         cerr << "]\n";              \
//     }
// #define errvn(x, n) {cerr << "["#x"]  ["; for (auto ___ = 0; ___ < (n); ++___) cerr << (x)[___] << ", "; cerr << "]\n";}
// #define cerr if(0)cerr
#define xx first
#define yy second
mt19937 rnd(std::chrono::high_resolution_clock::now().time_since_epoch().count());
/*_________________________________________________________________________________________________________________________*/

vector<vector<ll>> dp(100001 + 1, vector<ll>(26, 0));
vector<ll> arr[26];
void Solve()
{
    ll n, k;
    cin >> n >> k;
    string a;
    cin >> a;
    for (int i = 0; i < 26; i++)
        arr[i].clear();
    for (int i = 0; i < n; i++) {
        for (char c = 'a'; c <= 'z'; c++) {
            if (c != a[i])
                dp[i][c - 'a'] = 1;
            else
                dp[i][c - 'a'] = 0;
        }
    }
    for (int m = 0; m < 26; m++) {
        ll i = m;
        for (int j = 0; j < n; j++) {
            arr[m].push_back(dp[j][i]);
            i = (i + 1) % 26;
        }
    }
    ll ans = 1;
    for (int m = 0; m < 26; m++) {
        ll i = 0, j = 0, sum = arr[m][0];
        while (j < n && i < n) {
            if (sum <= k) {
                ans = max(ans, j - i + 1);
                j++;
                if (j < n)
                    sum += arr[m][j];
            } else {
                sum -= arr[m][i];
                i++;
            }
        }
    }
    cout << ans << "\n";
}

int32_t main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    for (int i = 1; i <= t; i++) {
        // cout << "Case #" << i << ": "; // cout << "Case " << i << ": ";
        Solve();
    }
    return 0;
}
// Coded by Tahsin Arafat (@TahsinArafat)

Information

Submit By
Type
Submission
Problem
P1068 Alphabetical substring
Language
C++20 (G++ 13.2.0)
Submit At
2024-07-15 14:10:42
Judged At
2024-07-15 14:10:42
Judged By
Score
100
Total Time
96ms
Peak Memory
45.16 MiB