fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. string s;
  6. int n,ans;
  7.  
  8. int main() {
  9. ios::sync_with_stdio(0),cin.tie(nullptr);
  10. cin >> s;
  11. n = s.size();
  12. queue<int> b,c;
  13. for (int i = 0; i < n; i++){
  14. if (s[i] == 'C')
  15. c.push(i);
  16. }
  17. for (int i = 0; i < n; i++){
  18. if (s[i] == 'B'){
  19. while (c.size() and c.front() < i) c.pop();
  20. if (c.size()) {
  21. ans++;
  22. c.pop();
  23. }
  24. else
  25. b.push(i);
  26. }
  27. }
  28. for (int i = 0; i < n; i++){
  29. if (s[i] == 'A'){
  30. while (b.size() and b.front() < i) b.pop();
  31. if (b.size()) {
  32. ans++;
  33. b.pop();
  34. }
  35. }
  36. }
  37. cout << ans;
  38. }
Success #stdin #stdout 0.01s 5516KB
stdin
ABCBBACBABB
stdout
5