fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3. #include<string>
  4. #include<algorithm>
  5. #include<cmath>
  6.  
  7. using namespace std;
  8.  
  9. int nikita_string()
  10. {
  11. string st;cin>>st;
  12. int n=st.size();
  13. int ind_st,ind_en;
  14. int mx=0,cont=0;
  15. int cont_a_1=0,cont_b_1=0;
  16. int cont_a_2=0,cont_b_2=0;
  17. int ans=0;
  18. int cont_st=0;
  19. bool check=false;
  20. for(int i=0;i<n;i++)
  21. {
  22. if(st[i]=='b')
  23. {
  24. cont++;
  25. }
  26. else
  27. {
  28. if(mx<cont&&i-cont!=0)
  29. {
  30. mx=cont;
  31. ind_en=i;
  32. check=true;
  33. }
  34. else if(cont>cont_st)
  35. {
  36. cont_st=cont;
  37. }
  38. cont=0;
  39. }
  40. }
  41.  
  42. if(check==false&&cont==0&&cont_st>0||cont_st==0&&check==false&&cont>0)
  43. {
  44. return st.size();
  45. }
  46. else if(check==false&&cont>0)
  47. {
  48. if(cont>=cont_st)
  49. {
  50. mx=cont;
  51. ind_en=n;
  52. }
  53. else
  54. {
  55. mx=cont_st;
  56. ind_en=mx;
  57. }
  58. }
  59.  
  60. ind_st=ind_en-mx;
  61. // cout<<ind_en<<" "<<ind_st<<" "<<mx<<"\n";
  62. for(int i=0;i<ind_st;i++)
  63. {
  64. if(st[i]=='a')
  65. cont_a_1++;
  66. else
  67. cont_b_1++;
  68. }
  69. for(int i=n-1;i>=ind_en;i--)
  70. {
  71. if(st[i]=='a')
  72. cont_a_2++;
  73. else
  74. cont_b_2++;
  75. }
  76. ans=max(cont_a_1,cont_b_1)+max(cont_a_2,cont_b_2)+mx;
  77. //cout<<ans;
  78. return ans;
  79.  
  80.  
  81. }
  82.  
  83. int main()
  84. {
  85. ios::sync_with_stdio(false);
  86. cin.tie(nullptr);
  87. cout.tie(nullptr);
  88.  
  89. int val=nikita_string();
  90. cout<<val;
  91.  
  92. return 0;
  93. }
  94.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Standard output is empty