Mehulcoder

PhonePe2019D

Oct 9th, 2020 (edited)
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. /*
  2. Author: Mehul Chaturvedi
  3. */
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7.  
  8. using ll = long long;
  9. #define vll vector<long long>
  10. #define rep(i, n) for (int i = 0, _n = (n); i < _n; i++)
  11.  
  12. /*
  13. * ..AAABCBC...BC.. Now, k continuous A, and l continuous BC after those A
  14. * Will result in k*l operations. Also note that this will shift those k As
  15. * to the right of all BCs, so keep that buffer count in case you encounter
  16. * As again after the BCs
  17. */
  18. void solve() {
  19.     string s; cin >> s;
  20.     ll n = s.size();
  21.  
  22.     ll ans = 0ll;
  23.     ll i = 0ll;
  24.     ll prevA = 0;
  25.     while (i < n) {
  26.         if (s[i] == 'A') {
  27.             ll count = 0ll;
  28.             while (i < n && s[i] == 'A') {
  29.                 count++;
  30.                 i++;
  31.             }
  32.             count += prevA;
  33.             prevA = count;
  34.             ll count2 = 0ll;
  35.             while (i < n - 1 && s[i] == 'B' && s[i + 1] == 'C') {
  36.                 count2++;
  37.                 i++;
  38.                 i++;
  39.             }
  40.  
  41.             ans += count * count2;
  42.         } else {
  43.             prevA = 0;
  44.             i++;
  45.         }
  46.     }
  47.  
  48.     cout << ans << '\n';
  49.     return;
  50. }
  51.  
  52. int main(int argc , char ** argv) {
  53.     ios_base::sync_with_stdio(false) ;
  54.     cin.tie(NULL) ;
  55.  
  56.     ll t = 1;
  57.  
  58.     while (t--) {
  59.         solve();
  60.     }
  61.  
  62.     return 0 ;
  63. }
Add Comment
Please, Sign In to add comment