Skip to content

Commit a7f1036

Browse files
Merge pull request #4564 from manas-2003/problem1566B
Java Solution Added to cf-1566B
2 parents aa39b30 + 81d3b9b commit a7f1036

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

solutions/orphaned/cf-1566B.mdx

+38-8
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ author: Ryan Chou
77

88
[Official Editorial](https://codeforces.com/blog/entry/94803)
99

10-
11-
# Explanation
10+
## Explanation
1211

1312
Since the MEX can be at most 2 when there is a substring that contains a 0 and 1, there's only 3 possible cases. If there are no zeros, then the MEX is 0, since all substrings would be missing a 0. If all zeros are adjacent to one another, we can cut the entire sequence out, leaving a MEX of 1. The other groups, all comprised of ones would have a MEX of 0. Leaving the answer to be $max(0, 1) = 1$. Lastly, if there are zeros and they're not adjacent to one another, then a substring like ${0, 1}$ must exist, resulting in a MEX of 2.
1413

@@ -33,36 +32,67 @@ int main() {
3332
// find the number of zeros
3433
int zeros = count(s.begin(), s.end(), '0');
3534

36-
// no zeros, the answer is 0
3735
if (zeros == 0) {
38-
cout << 0 << endl;
36+
cout << 0 << endl; // no zeros, the answer is 0
3937
} else {
4038
// first and last occurences of zero
4139
int l = s.find('0');
4240
int r = s.rfind('0');
43-
44-
// if they're all adjacent to one another.
41+
// if they're all adjacent to one another
4542
cout << ((r - l + 1 == zeros) ? 1 : 2) << endl;
4643
}
4744
}
4845
}
4946
```
47+
5048
</CPPSection>
49+
<JavaSection>
50+
51+
```java
52+
import java.io.*;
53+
54+
public class MinMexCut {
55+
public static void main(String[] args) throws IOException {
56+
BufferedReader read =
57+
new BufferedReader(new InputStreamReader(System.in));
58+
int t = Integer.parseInt(read.readLine());
59+
for (int i = 0; i < t; i++) {
60+
String s = read.readLine();
61+
// find the number of zeros
62+
int zeros = s.length() - s.replace("0", "").length();
63+
64+
if (zeros == 0) {
65+
System.out.println(0); // no zeros, the answer is 0
66+
} else {
67+
// first and last occurences of zero
68+
int l = s.indexOf('0');
69+
int r = s.lastIndexOf('0');
70+
// if they're all adjacent to one another
71+
System.out.println((r - l + 1 == zeros) ? 1 : 2);
72+
}
73+
}
74+
}
75+
}
76+
```
5177

78+
</JavaSection>
5279
<PySection>
5380

5481
```py
5582
for _ in range(int(input())):
5683
s = input()
84+
# find the number of zeros
5785
zeros = s.count("0")
5886

5987
if zeros == 0:
60-
print(0)
88+
print(0) # no zeros, the answer is 0
6189
else:
90+
# first and last occurences of zero
6291
l = s.find("0")
6392
r = s.rfind("0")
64-
93+
# if they're all adjacent to one another
6594
print(1 if r - l + 1 == zeros else 2)
6695
```
96+
6797
</PySection>
6898
</LanguageSection>

0 commit comments

Comments
 (0)