Advertisement
pb_jiang

CF2104E AC

Apr 28th, 2025
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. // Problem: E. Unpleasant Strings
  2. // Contest: Codeforces - Educational Codeforces Round 178 (Rated for Div. 2)
  3. // URL: https://codeforces.com/contest/2104/problem/E
  4. // Memory Limit: 512 MB
  5. // Time Limit: 2000 ms
  6. //
  7. // Powered by CP Editor (https://cpeditor.org)
  8.  
  9. #include <assert.h>
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12. #ifndef __DEBUG__
  13. #define dbg(...) 42
  14. #endif
  15. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  16.  
  17. namespace rngs = std::ranges;
  18. using ll = long long;
  19. using a2l = array<ll, 2>;
  20. using pll = pair<ll, ll>;
  21. using vl = vector<ll>;
  22.  
  23. ll n, k, q;
  24. string s;
  25. vector<vl> ids;
  26. vl cache;
  27.  
  28. void solve()
  29. {
  30.     string al;
  31.     cin >> al;
  32.  
  33.     ll st = -1;
  34.     for (auto c : al) {
  35.         auto id = c - 'a';
  36.         const auto &iv = ids[id];
  37.         auto nit = upper_bound(iv.begin(), iv.end(), st);
  38.         if (nit == iv.end()) {
  39.             cout << 0 << '\n';
  40.             return;
  41.         }
  42.         st = *nit;
  43.     }
  44.  
  45.     // cache_output(st);
  46.     cout << cache[st] + 1 << '\n';
  47. }
  48.  
  49. int main(int argc, char **argv)
  50. {
  51.     std::ios::sync_with_stdio(false);
  52.     std::cin.tie(nullptr);
  53.     cin >> n >> k >> s >> q;
  54.     ids.resize(k);
  55.  
  56.     for (ll i = 0; i < n; ++i) {
  57.         auto id = s[i] - 'a';
  58.         ids[id].push_back(i);
  59.     }
  60.  
  61.     cache = vl(n + 1, -1);
  62.     cache[n] = 0;
  63.     for (ll i = n - 1; i >= 0; --i) {
  64.         ll min_op = LLONG_MAX;
  65.         for (ll id = 0; id < k; ++id) {
  66.             const auto &iv = ids[id];
  67.             auto nid = upper_bound(iv.begin(), iv.end(), i);
  68.             if (nid == iv.end()) {
  69.                 min_op = 0;
  70.                 break;
  71.             }
  72.             min_op = min(min_op, cache[*nid] + 1);
  73.         }
  74.         cache[i] = min_op;
  75.     }
  76.  
  77.     for (ll i = 0; i < q; ++i)
  78.         solve();
  79.  
  80.     return 0;
  81. };
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement