fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int moves(string s, char d1, char d2) {
  5. long long n = s.length();
  6. int idx2 = -1, idx1 = -1;
  7.  
  8. for (int i = n - 1; i >= 0; i--) {
  9. if (s[i] == d2) {
  10. idx2 = i;
  11. break;
  12. }
  13. }
  14.  
  15. if (idx2 == -1) return 100000;
  16.  
  17. for (int i = idx2 - 1; i >= 0; i--) {
  18. if (s[i] == d1) {
  19. idx1 = i;
  20. break;
  21. }
  22. }
  23.  
  24. if (idx1 == -1) return 100000;
  25.  
  26.  
  27. return (int)(n - idx1 - 2);
  28. }
  29.  
  30. void solve() {
  31. string n;
  32. cin >> n;
  33.  
  34. int ans = 100000;
  35.  
  36. ans = min(ans, moves(n, '0', '0'));
  37. ans = min(ans, moves(n, '2', '5'));
  38. ans = min(ans, moves(n, '5', '0'));
  39. ans = min(ans, moves(n, '7', '5'));
  40.  
  41. cout << ans << endl;
  42. }
  43.  
  44. int main() {
  45.  
  46. int tc;
  47. cin >> tc;
  48.  
  49. while (tc--) {
  50. solve();
  51. }
  52.  
  53. return 0;
  54. }
Success #stdin #stdout 0s 5320KB
stdin
5
100
71345
3259
50555
2050047
stdout
0
3
1
3
2