-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1593b.cpp
More file actions
58 lines (46 loc) · 1.34 KB
/
1593b.cpp
File metadata and controls
58 lines (46 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(false); cin.tie(NULL)
int main() {
fastio;
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
string end_nums[4] = {"00","25","50","75"};
int n = s.length();
int ans = 999;
for(string target : end_nums){
// Last two position
int pos2 = -1;
int pos1 = -1;
// finding the last character of target from end
for(int i = n - 1;i >= 0;i--){
if(s[i] == target[1]){
pos2 = i;
break;
}
}
if(pos2 == -1)continue;
// find first character of target before pos2
for (int i = pos2 - 1; i >= 0; i--) {
if (s[i] == target[0]) {
pos1 = i;
break;
}
}
if(pos1 == -1)continue;
// n-pos2 -1 == deletion of number after pos2
// pos2-pos1-1 == deltion of number between pos1 and pos2
int deletions = (n - pos2 - 1) + (pos2 - pos1 - 1);
ans = min(ans,deletions);
}
if(ans == 999){
cout << -1 << "\n";
}else{
cout << ans << "\n";
}
}
return 0;
}