fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int countLongestAtoZLength(const string& S) {
  5. int n = S.size();
  6. int first_A = -1, last_Z = -1;
  7.  
  8. for (int i = 0; i < n; i++) {
  9. if (S[i] == 'A') {
  10. first_A = i;
  11. break;
  12. }
  13. }
  14.  
  15. for (int i = n - 1; i >= 0; i--) {
  16. if (S[i] == 'Z') {
  17. last_Z = i;
  18. break;
  19. }
  20. }
  21.  
  22. if (first_A != -1 && last_Z != -1 && first_A < last_Z) {
  23. return last_Z - first_A + 1;
  24. }
  25.  
  26. return 0;
  27. }
  28.  
  29. int main() {
  30. string S;
  31. getline(cin, S);
  32.  
  33. int ans = countLongestAtoZLength(S);
  34. cout << ans << endl;
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
0