[CF] C. K-Dominant Character - Educational Codeforces Round 32
Solutions Codeforces 杂题 1400 Easy
Lastmod: 2025-01-16 周四 00:21:44

https://codeforces.com/contest/888/problem/C

题目大意

给一个小写字母的字符串 $S \ (|S| \le 10^5)$。定义 $k-Dominant$ 为对于某字符 $c$,所有 $len \ge k$ 长的子序列都含有至少一个 $c$。问对于串 $S$ 最小的 $k$ 是多少。

简要题解

因为每类字符是独立的,因此我们分开考虑即可。对于字母 $c$,显然相邻的个钟最大的间隔将决定 $k$。注意还需要考虑首尾两段。结果取所有字母最好的即可。

复杂度

$T$:$O(n)$

$S$:$O(n)$

代码实现

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

int io_=[](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();

using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using PII = pair<int, int>;
using VI = vector<int>;
using MII = map<int, int>;

template<typename T> void cmin(T &x,const T &y) { if(y<x) x=y; }
template<typename T> void cmax(T &x,const T &y) { if(x<y) x=y; }
template<typename T> bool ckmin(T &x,const T &y) { 
    return y<x ? (x=y, true) : false; }
template<typename T> bool ckmax(T &x,const T &y) { 
    return x<y ? (x=y, true) : false; }
template<typename T> void cmin(T &x,T &y,const T &z) {// x<=y<=z 
    if(z<x) { y=x; x=z; } else if(z<y) y=z; }
template<typename T> void cmax(T &x,T &y,const T &z) {// x>=y>=z
    if(x<z) { y=x; x=z; } else if(y<z) y=z; }

// mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
// mt19937_64 rnd_64(chrono::system_clock::now().time_since_epoch().count());

/*
---------1---------2---------3---------4---------5---------6---------7---------
1234567890123456789012345678901234567890123456789012345678901234567890123456789
*/

void solve() {
  string s; cin >> s;
  int n = s.length();

  vector<int> lst(26, -1);
  vector<int> mx(26);

  for (int i = 0; i < n; i++) {
    int c = s[i] - 'a';
    cmax(mx[c], i - lst[c]);
    lst[c] = i;
  }
  int ans = n;
  for (int i = 0; i < 26; i++) {
    cmax(mx[i], n - lst[i]);
    cmin(ans, mx[i]);
  }
  cout << ans << '\n';
}

int main() {
  int t = 1; 
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Prev: [CF] B. Buggy Robot - Educational Codeforces Round 32
Next: [CF] D. Almost Identity Permutations - Educational Codeforces Round 32