D. Dense Subsequence - Intel Code Challenge Final Round
Solutions Codeforces 贪心 双指针
Lastmod: 2024-07-31 周三 22:09:38

https://codeforces.com/problemset/problem/724/D

题目大意

给定一个字符串 $S$ 串长 $\le 10^5$ 给定 $m \le |S|$。选择一些下标,使得所有长度为 $m$ 的子串都至少包含一个备选的下标。找出一组符合规定的下标,使其字符任意重排之后字典序最小,输出这个最小字典序的串。

简要题解

显然可以贪心的先从最小的字符选,如果这个字符的下标子集无法满足覆盖的要求,则需要全选(因为必然有更大的字符要选,则选更多的当前字符,可以使字典序更小)。如果某些子集能够覆盖所有 $m$ 长的子序列,则尽量少的当前字符更好。

实现上,我们可以按照字符分类把下标分类,依次处理。处理每一个字符时暴力扫已经选了字符的位置,用滑动窗口来检查是否覆盖,没覆盖时贪心选取区间中最右侧的字符,无字符可选时说明当前字符无法完成覆盖。

复杂度

$T$:$O(n \Sigma)$ 其中 $\Sigma$ 为字符集大小 $26$。

$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() {
  int m; cin >> m;
  string s; cin >> s;

  int n = s.length();

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

  string ans;
  vector<int> cover(n), tmp;
  for (int ii = 0; ii < 26; ii++) {
    int sz = pos[ii].size();
    if (!sz) continue;

    int j = 0;
    int cnt = 0;
    int add = 0;
    bool coverall = true;
    for (int i = 0; i < m - 1; i++) {
      cnt += cover[i];
    }
    for (int i = m - 1; i < n; i++) {
      cnt += cover[i];

      if (cnt == 0) {
        while (j < sz && pos[ii][j] <= i) j++;

        if (!j || pos[ii][j - 1] <= i - m) {
          coverall = false;
        } else {
          // cerr << "ii " << ii << " at " << i << " add " << pos[ii][j - 1] << '\n';
          cover[pos[ii][j - 1]]++;
          cnt++;
          add++;
        }
      }

      cnt -= cover[i - m + 1];
    }

    if (coverall) {
      for (int i = 0; i < add; i++) {
        ans += ((char)(ii + 'a'));
      }
      break;
    } else {
      for (int i = 0; i < sz; i++) {
        ans += ((char)(ii + 'a'));
      }
    }
  }

  cout << ans << '\n';
 }
 
 int main() {
   int t = 1; 
  //  cin >> t;
   while (t--) {
     solve();
   }
   return 0;
 }
 
Prev: C. Sonya and Problem Wihtout a Legend - Codeforces Round 371 (Div. 1)
Next: E. Decode - Codeforces Round 962 (Div. 3)