fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define int long long
  5. string s;
  6. int n;
  7. const int N = 307;
  8. int mem[N][N];
  9. int k;
  10.  
  11. int dp(int i, int o) {
  12. if (i >= n)return o;
  13. int &res = mem[i][o];
  14. if (~res)return res;
  15. res = 1e9;
  16. if (s[i] == 'f' && o > 0) {
  17. for (int j = i + 1, c = 0; c <= k && j <= n; ++j, ++c)
  18. res = min(res, dp(j, o - 1));
  19. }
  20. if (s[i] == 'o') {
  21. res = min(res, dp(i + 1, o + 1));
  22. }
  23. res = min(res, o + 1 + dp(i + 1, 0));
  24. return res;
  25. }
  26.  
  27. int32_t main() {
  28. ios_base::sync_with_stdio(false);
  29. cin.tie(nullptr);
  30. cin >> s >> k;
  31. n = s.length();
  32. memset(mem, -1, sizeof mem);
  33. cout << dp(0, 0);
  34. }
Success #stdin #stdout 0.01s 5436KB
stdin
Standard input is empty
stdout
Standard output is empty