Advertisement
Protyay77

Question 3 Protyay

Apr 13th, 2025
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. long getMaximumPrizes(string inputString) {
  5.     long countA = 0, countAM = 0, countAMZ = 0;
  6.     long maxPrizes = 0;
  7.  
  8.     // Traverse the string to count "A", "AM", and "AMZ" subsequences
  9.     for (char c : inputString) {
  10.         if (c == 'Z') {
  11.             countAMZ += countAM;  // Every "AM" can form an "AMZ" when followed by "Z"
  12.         }
  13.         if (c == 'M') {
  14.             countAM += countA;  // Every "A" can form an "AM" when followed by "M"
  15.         }
  16.         if (c == 'A') {
  17.             countA++;  // Count of "A"s found
  18.         }
  19.     }
  20.  
  21.     // Initially, the count of "AMZ" subsequences without adding any letter
  22.     maxPrizes = countAMZ;
  23.  
  24.     // Try adding each letter ('A', 'M', 'Z') and calculate the maximum result
  25.     // Add 'A' - This will start new "AMZ" subsequences from "AMZ" found earlier
  26.     maxPrizes = max(maxPrizes, countAMZ + countA);
  27.  
  28.     // Add 'M' - This will complete "A"s into "AM" subsequences
  29.     maxPrizes = max(maxPrizes, countAMZ + countA);
  30.  
  31.     // Add 'Z' - This will complete "AM"s into "AMZ" subsequences
  32.     maxPrizes = max(maxPrizes, countAMZ + countAM);
  33.  
  34.     return maxPrizes;
  35. }
  36.  
  37. // int main() {
  38. //     // Sample Test Cases
  39. //     string inputString = "AKMMZ";
  40. //     cout << getMaximumPrizes(inputString) << endl;  // Expected output: 4
  41.    
  42. //     string inputString2 = "AZ";
  43. //     cout << getMaximumPrizes(inputString2) << endl;  // Expected output: 1
  44.  
  45. //     return 0;
  46. // }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement