-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path499-make-it-divisible-by-25.cpp
More file actions
117 lines (115 loc) · 2.56 KB
/
Copy path499-make-it-divisible-by-25.cpp
File metadata and controls
117 lines (115 loc) · 2.56 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Make it Divisible by 25 [1593B]
* Problem: https://codeforces.com/problemset/problem/1593/B
* Verdict: ACCEPTED Solved: 2021-10-13
* Language: C++17 (GCC 7-32)
* Runtime: 15 ms Memory: 0 KB
* Tags: dfs and similar, dp, greedy, math
* Author: BidoTeima
* Source: https://codeforces.com/contest/1593/submission/131766969
*/
/// isA AC
#include <bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename _Ty>
using ordered_set = tree<_Ty,null_type,less<_Ty>,rb_tree_tag,tree_order_statistics_node_update>;
using ll = long long;
void ACPLS()
{
#ifndef ONLINE_JUDGE
freopen("output.txt", "w", stdout);
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
#define tc \
int tttttt,subtask; \
cin >> tttttt /*>> subtask*/; \
while (tttttt--)
#define sumrange(l, r, arr) (l == 0 ? arr[r] : arr[r] - arr[l - 1])
#define all(v) v.begin(), v.end()
int main()
{
ACPLS();
tc{
string s;
cin>>s;
int ans = (int)s.size();
int cnt=0;
int zerocnt=0;
for(int i = (int)s.size()-1; i >= 0; i--){
if(zerocnt==2)break;
if(s[i]=='0')++zerocnt;
else ++cnt;
}
ans=min(ans,cnt);
cnt=0;
bool five=0,two=0;
for(int i = (int)s.size()-1; i >= 0; i--){
if(five&&two)
break;
if(s[i]=='5'){
if(five==0){
five=1;
}
else ++cnt;
}
else if(s[i]=='2'){
if(five==1){
two=1;
}
else ++cnt;
}
else ++cnt;
}
ans=min(ans,cnt);
cnt=0;
bool zero=0;
five=0;
for(int i = (int)s.size()-1; i >= 0; i--){
if(five&&zero)
break;
if(s[i]=='0'){
if(zero==0){
zero=1;
}
else ++cnt;
}
else if(s[i]=='5'){
if(zero==1){
five=1;
}
else ++cnt;
}
else ++cnt;
}
ans=min(ans,cnt);
cnt=0;
bool seven=0;
five=0;
for(int i = (int)s.size()-1; i >= 0; i--){
if(five&&seven)
break;
if(s[i]=='5'){
if(five==0){
five=1;
}
else ++cnt;
}
else if(s[i]=='7'){
if(five==1){
seven=1;
}
else ++cnt;
}
else ++cnt;
}
ans=min(ans,cnt);
cout<<ans<<'\n';
}
}