fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23. // LMCT
  24.  
  25. string a;
  26.  
  27. int consistency(int n) {
  28.  
  29.  
  30. int l = 0;
  31. int lc = 0;
  32. int lct = 0;
  33.  
  34. for(int i=0; i<n; i++){
  35. if(a[i] == 'L') l++;
  36. else if(a[i] == 'C') lc += l;
  37. else if(a[i] == 'T') lct += lc;
  38. }
  39.  
  40. int llct = lct + lc;
  41.  
  42. int t = 0;
  43. int ct = 0;
  44. for(int i=n-1; i>=0; i--){
  45. if(a[i] == 'T') t++;
  46. else if(a[i] == 'C') ct += t;
  47. }
  48.  
  49. int lctt = lct + ct;
  50.  
  51.  
  52. int x = 0;
  53. l = 0;
  54. for(int i=0; i<n; i++){
  55. if(a[i] == 'L') l++;
  56. else if(a[i] == 'T') t--;
  57.  
  58. x = max(x, l * t);
  59. }
  60.  
  61. int lcct = lct + x;
  62.  
  63. int ans = max({ lct, lctt, llct, lcct } );
  64.  
  65. return ans;
  66.  
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80. void solve() {
  81.  
  82. cin >> a;
  83. int n = a.size();
  84.  
  85. cout << consistency(n) << endl ;
  86.  
  87. }
  88.  
  89.  
  90.  
  91.  
  92.  
  93. int32_t main() {
  94. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  95.  
  96. int t = 1;
  97. while (t--) {
  98. solve();
  99. }
  100.  
  101. return 0;
  102. }
Success #stdin #stdout 0.01s 5320KB
stdin
LMCT
stdout
2