Lock and Key Sequence#12236

Lock and Key Sequence (Editorial)

Problem Author:

Imran AKKI

Prepared by:

Imran AKKI

Edited by:

Hasan Saleh (MathModel)

Solution We need two equal endpoints and pairwise-distinct characters strictly between them. Equivalently: find l < r with s_l=s_r such that the substring s[l+1;r-1]=s_{l+1}s_{l+2}...s_{r-1} contains at least distinct letters, and among all such pairs return the one minimizing r-l+1.

Notice that for a fixed left endpoint and fixed character c = s_l , if is moving to the right, the number of distinct letters inside (l,r) is non-decreasing as moves right. That monotonicity lets us binary-search the smallest value that satisfies the -distinct requirement for each .

We can count the occurrences of some character using using prefix frequency arrays over the 26 lowercase letters as , then for every letter that appears at least twice:

  1. For every occurrence index (left occurrence) binary-search among later occurrences \text{pos}[c][\text{mid}] for the smallest such that the middle substring s[i+1;r-1] contains distinct letters.

  2. Use to count distinct letters in that middle substring; as soon as distinct are found.

  3. Track the minimal substring length r- i + 1 and the corresponding endpoints.

// Author: Imran AKKI (imranakki)
#include <bits/stdc++.h>
#define ll long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define lp(i, a, n) for (ll i = a; i < n; i++)
#define MAXN 1000005
#define MOD (ll)(1e9 + 7)
using namespace std;

void solve()
{
    ll n, k; cin >> n >> k;
    string s; cin >> s;

    // 1-based indexing !
    vector<vector<ll>> pre(26, vector<ll>(n + 2, 0));
    for(ll d = 0; d < 26; d++) {
        for(ll i = 0; i < n; i++) {
            pre[d][i + 1] = pre[d][i] + ((s[i] - 'a') == d);
        }
    }

    vector<ll> pos[26];
    for(ll i = 0; i < n; i++){
        pos[s[i] - 'a'].push_back(i);
    }

    auto can = [&](ll i, ll l) {
        // i and l are 0-indexed
        if(l - i < k + 1) return false;
        ll distinct = 0;
        for(ll d = 0; d < 26; d++) {
            if(pre[d][l] - pre[d][i + 1] > 0) {
                distinct++;
                if(distinct >= k){
                    return true;
                }
            }
        }
        return false;
    };
    ll bestLen = INT_MAX;
    ll bestI = -1, bestJ = -1;
    for(ll c = 0; c < 26; c++) {
        auto &P = pos[c];
        ll m = P.size();
        if(m < 2) continue;

        for(ll x = 0; x < m - 1; x++) {
            ll i = P[x];
            ll l = x + 1, r = m - 1;
            ll best = -1;
            while(l <= r) {
                ll mid = (l + r) >> 1;
                if(can(i, P[mid])) {
                    best = mid;
                    r = mid - 1;
                }else {
                    l = mid + 1;
                }
            }
            if(best != -1  && can(i, P[best]) && P[best] - i + 1 < bestLen) {
                bestLen = min(bestLen, P[best] - i + 1);
                bestI = i;
                bestJ = P[best];
            }
        }
    }

    if(bestI != -1) {
        cout << "YES\n";
        cout << bestI + 1 << " " << bestJ + 1 << "\n";
    } else {
        cout << "NO\n";
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll t; cin >> t;
    while(t--) solve();
    return 0;
}

Complexity : \mathcal{O}(n \cdot 26 \cdot \log n)