fork download
  1. #include <cstdio>
  2. #include <string.h>
  3. using namespace std;
  4. char s[100005];
  5. int j=0;
  6.  
  7. int calc(int a){
  8. int count1=0, temp=0;
  9. for(int i=0; i<j; i++){
  10. if(s[i]=='P'){
  11. count1++;
  12. }
  13. if(count1==a){
  14. count1=0;
  15. temp=i;
  16. break;
  17. }
  18. }
  19. for(int i=temp+1; i<j;i++){
  20. if(s[i]=='A'){
  21. count1++;
  22. }
  23. if(count1==a){
  24. count1=0;
  25. temp=i;
  26. break;
  27. }
  28. }
  29. for(int i=temp+1; i<j;i++){
  30. if(s[i]=='N'){
  31. count1++;
  32. }
  33. if(count1==a){
  34. count1=0;
  35. temp=i;
  36. break;
  37. }
  38. }
  39. for(int i=temp+1; i<j;i++){
  40. if(s[i]=='D'){
  41. count1++;
  42. }
  43. if(count1==a){
  44. count1=0;
  45. temp=i;
  46. break;
  47. }
  48. }
  49. for(int i=temp+1; i<j;i++){
  50. if(s[i]=='A'){
  51. count1++;
  52. }
  53. if(count1==a){
  54. return 1;
  55. }
  56. }
  57. return 0;
  58. }
  59. int main(){
  60.  
  61. int count=0;
  62. // while (true){
  63. // scanf("%c", &s[j]);
  64. // if(s[j]=='P') count++;
  65. // if(s[j]=='\n' || s[j]==EOF) break;
  66. // j++;
  67. // }
  68. // j++;
  69. scanf("%s", s);
  70. j=strlen(s);
  71. for(int i=0; i<j; i++){
  72. if(s[i] == 'P') {
  73. count++;
  74. }
  75. }
  76. int bawah=0;
  77. int atas = count+1;
  78. while(atas-1>bawah){
  79. int mid = (atas+bawah)/2;
  80. if(calc(mid)==1){
  81. bawah = mid;
  82. }
  83. else{
  84. atas = mid;
  85. }
  86. }
  87. printf("%d\n", bawah);
  88. }
Success #stdin #stdout 0s 5540KB
stdin
PADPAPAAANDNDNPANDADDDAAADA
stdout
3