tien_noob

AB - point 1, BA - point -1. Max point ?

May 22nd, 2021 (edited)
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define task "TESTCODE"
  3. using namespace std;
  4. const int N = 1e6;
  5. string s;
  6. int n, dp[N + 1][2];
  7. void read()
  8. {
  9.     cin >> n >> s;
  10.     s = ';' + s;
  11. }
  12. void solve()
  13. {
  14.     memset(dp, -1, sizeof(dp));
  15.     if (s[1] != 'a')
  16.     {
  17.         dp[1][1] = 0;
  18.     }
  19.     if (s[1] != 'b')
  20.     {
  21.         dp[1][0] = 0;
  22.     }
  23.     for (int i = 2; i <= n; ++ i)
  24.     {
  25.         if (s[i] != 'b')
  26.         {
  27.             dp[i][0] = max(dp[i-1][0], dp[i-1][1] - 1);
  28.         }
  29.         if (s[i] != 'a')
  30.         {
  31.             dp[i][1] = max(dp[i-1][1], dp[i-1][0] + 1);
  32.         }
  33.     }
  34.     cout << max(dp[n][0], dp[n][1]);
  35. }
  36. int main()
  37. {
  38.     ios_base::sync_with_stdio(false);
  39.     cin.tie(nullptr);
  40.     //freopen(task".INP", "r", stdin);
  41.     //freopen(task".OUT", "w", stdout);
  42.     int t = 1;
  43.     bool typetest = false;
  44.     if (typetest)
  45.     {
  46.         cin >> t;
  47.     }
  48.     for (int _ = 1; _ <= t; ++ _ )
  49.     {
  50.         read();
  51.         solve();
  52.     }
  53.     return 0;
  54. }
  55.  
Add Comment
Please, Sign In to add comment