• Source
    1. #include <bits/stdc++.h>
    2.  
    3. #define ll long long
    4.  
    5. using namespace std;
    6.  
    7. int main(void)
    8. {
    9. ios_base::sync_with_stdio(0);
    10. cin.tie(0);
    11. cout.tie(0);
    12. int t;
    13. cin >> t;
    14. while(t--)
    15. {
    16. int d, na = 0, np = 0, nr = 0;
    17. cin >> d;
    18. nr = 0.75*d;
    19. if(d%4 != 0) nr = nr + 1;
    20.  
    21. char *s = new char[d+1];
    22. for(int i = 1; i <= d; i++)
    23. {
    24. cin >> s[i];
    25. if(s[i] == 'P')
    26. np++;
    27. }
    28.  
    29. if(nr <= np)
    30. {
    31. cout << "0\n";
    32. }
    33. else
    34. {
    35. for(int i = 3; i <= d-2; i++)
    36. {
    37. if(s[i] == 'A')
    38. if(s[i-1] == 'P' || s[i-2] == 'P')
    39. if(s[i+1] == 'P' || s[i+2] == 'P')
    40. na++;
    41. }
    42. int test = na + np;
    43. if(nr <= test)
    44. {
    45. cout << nr - np << "\n";
    46. }
    47. else
    48. {
    49. cout << "-1\n";
    50. }
    51.  
    52. }
    53.  
    54. cout << endl;
    55.  
    56. }
    57. return 0;
    58. }