Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 |
n=int( input() )
s=str( input() )
ret=3
for i in range(1,n):
if s[i] != s[i-1]:
ret=ret+1
if ret < n :
print (ret)
else :
print (n)
| PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
int n, f[maxn][3];
string s;
void test() {
puts("[test]");
for (int i = 0; i < n; i++) {
printf("%d: %6d%6d%6d\n", i, f[i][0], f[i][1], f[i][2]);
}
}
int main() {
cin >> n >> s;
f[0][0] = f[0][1] = f[0][2] = 1;
for (int i = 1; i < n; i++) {
f[i][0] = f[i - 1][0] + (s[i] != s[i - 1]);
f[i][1] =
max(f[i - 1][0] + (s[i] == s[i - 1]), f[i - 1][1] + (s[i] != s[i - 1]));
f[i][2] =
max(f[i - 1][1] + (s[i] == s[i - 1]), f[i - 1][2] + (s[i] != s[i - 1]));
}
cout << max(f[n - 1][0], max(f[n - 1][1], f[n - 1][2])) << endl;
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
char a[100003];
int n, i, l, l1, l2;
int main() {
scanf("%d\n", &n);
gets(a);
l = strlen(a);
l1 = 1;
for (i = 0; i + 1 <= l - 1; i++) {
if (a[i] != a[i + 1]) l1++;
}
if (l == l1 || l1 == l - 1)
printf("%d", l);
else
printf("%d", l1 + 2);
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
uint64_t las(std::vector<int8_t> bits) {
uint64_t n = bits.size();
uint64_t length = 1;
uint64_t new_length = 1;
for (uint64_t i = 1; i < n; i++) {
if (bits.at(i - 1) != bits.at(i))
new_length++;
else {
length = new_length > length ? new_length : length;
}
}
length = new_length > length ? new_length : length;
return length;
}
int main() {
uint64_t n;
std::cin >> n;
std::vector<int8_t> bits(n);
for (uint64_t i = 0; i < n; i++) std::cin >> bits.at(i);
uint64_t seq_len = las(bits);
std::cout << std::min(seq_len + 2, n) << std::endl;
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.io.*;
import java.util.*;
public class Main {
FastScanner in;
PrintWriter out;
static final String FILE = "";
public void solve() {
int n = in.nextInt();
String s = in.next();
int cnt = 1;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) != s.charAt(i - 1))
cnt++;
}
out.print(Math.min(n, cnt + 2));
}
public void run() {
if (FILE.equals("")) {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
} else {
try {
in = new FastScanner(new FileInputStream(FILE + ".in"));
out = new PrintWriter(new FileOutputStream(FILE + ".out"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
solve();
out.close();
}
public static void main(String[] args) {
(new Main()).run();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
public ArrayList<Integer> readIntList(int n) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(in.nextInt());
return list;
}
public ArrayList<String> readStringList(int n) {
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(in.next());
return list;
}
public abstract class Tuple implements Comparable<Tuple> {
private Object[] valueArray;
private List<Object> valueList;
protected Tuple(Object... values) {
super();
this.valueArray = values;
this.valueList = Arrays.asList(values);
}
public Object getValue(int pos) {
return this.valueArray[pos];
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Tuple other = (Tuple) obj;
return valueList.equals(other.valueList);
}
public int compareTo(Tuple o) {
for (int i = 0; i < valueArray.length; i++) {
Comparable tElement = (Comparable) valueArray[i];
Comparable oElement = (Comparable) o.valueArray[i];
int comparison = tElement.compareTo(oElement);
if (comparison != 0) {
return comparison;
}
}
return 0;
}
}
public class Pair<A, B> extends Tuple {
public A a;
public B b;
public Pair(A a, B b) {
super(a, b);
this.a = a;
this.b = b;
}
}
public class Triplet<A, B, C> extends Tuple {
public A a;
public B b;
public C c;
public Triplet(A a, B b, C c) {
super(a, b, c);
this.a = a;
this.b = b;
this.c = c;
}
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const long double pi = 3.1415926535897932384626433;
const int mod = 1e9 + 7;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n;
cin >> n;
string s;
cin >> s;
long long ans = 1;
for (long long i = 1; i < n; i++) {
ans += (s[i] != s[i - 1]);
}
cout << min(ans + 2, n);
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
vector<int> vp;
int N;
int u = 0;
char str[100010];
int cnt = 0;
inline void ReadInput(void) {
scanf("%d", &N);
cin >> str;
int last;
if (str[0] == '1') {
vp.push_back(1);
last = 1;
cnt = 1;
} else {
vp.push_back(0);
last = 0;
cnt = 1;
}
for (int i = 1; i < N; i++) {
if (str[i] == '0') {
if (last == 1) {
vp.push_back(0);
cnt = 1;
last = 0;
} else {
if (cnt == 1) {
cnt++;
u++;
} else if (cnt == 2) {
cnt++;
u += 2;
} else {
cnt++;
}
}
} else {
if (last == 0) {
vp.push_back(1);
cnt = 1;
last = 1;
} else {
if (cnt == 1) {
cnt++;
u++;
} else if (cnt == 2) {
cnt++;
u += 2;
} else {
cnt++;
}
}
}
}
}
inline void solve(void) {
int ans = vp.size();
if (u >= 2)
ans += 2;
else if (u == 1)
ans += 1;
cout << ans << endl;
}
inline void Refresh(void) {}
int main() {
ReadInput();
solve();
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int inf = 1e5 + 10;
char s[inf];
int dp[inf][3][2];
int main() {
int n;
scanf("%d%s", &n, s);
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 2; k++) {
dp[i + 1][j][k] = dp[i][j][k];
}
}
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 2; k++) {
int c = s[i] - '0';
if (k != c) {
dp[i + 1][j][c] = max(dp[i + 1][j][c], dp[i][j][k] + 1);
} else {
if (j < 2) {
dp[i + 1][j + 1][c] = max(dp[i + 1][j + 1][c], dp[i][j][c] + 1);
}
}
}
}
}
int ans = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
ans = max(ans, dp[n][i][j]);
}
}
printf("%d\n", ans);
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int tmp1 = 0, tmp2 = 0;
cin >> n;
string s;
cin >> s;
for (int i = 1; i <= s.length(); i++) {
if (s[i] != s[i - 1]) {
tmp1++;
} else {
tmp2++;
}
}
cout << tmp1 + min(tmp2, 2) << endl;
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
char c(char w) {
if (w == '1') return '0';
return '1';
}
int main() {
long int n;
scanf("%ld\n", &n);
char s[n + 1];
scanf("%s", &s);
bool b = 0;
for (long int i = 1; i < n; i++) {
if ((s[i] == s[i - 1])) {
s[i] = c(s[i]);
b = 1;
} else if (b)
break;
}
long int sum[n];
for (long int i = 0; i < n; i++) sum[i] = 0;
long int nula = 0, kec = 0;
long int ans = 0;
for (long int i = 0; i < n; i++) {
if (s[i] == '1') {
sum[i] = 1 + nula;
kec = max(sum[i], kec);
} else {
sum[i] = 1 + kec;
nula = max(nula, sum[i]);
}
ans = max(ans, sum[i]);
}
printf("%ld", ans);
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n, ans = 1, sum, cnt = 1;
string test;
string s;
int main() {
cin >> n;
cin >> s;
if (n <= 3) {
cout << n;
return 0;
}
test[0] = s[0];
for (int i = 1; i < n; i++) {
if (s[i] != test[0]) {
test = s[i];
ans++;
if (cnt < 3) {
sum++;
} else if (cnt >= 3) {
sum += 2;
}
cnt = 1;
} else if (s[i] == s[i - 1]) {
cnt++;
}
}
if (cnt > 1) {
if (cnt >= 3) {
sum += 2;
}
if (cnt < 3) {
sum++;
}
}
if (sum > 2) {
sum = 2;
}
ans += sum;
if (ans <= 3) {
ans = 3;
}
if (ans > n) {
ans = n;
}
cout << ans;
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.io.*;
public class CF604C {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
char[] cc = br.readLine().toCharArray();
// a?[i][j] is length of longest subsequence of cc[0,i]
// containing at most j non-alternates and ending with ?
int[][] a0 = new int[n][3];
int[][] a1 = new int[n][3];
for (int j = 0; j < 3; j++) {
a0[0][j] = cc[0] == '0' ? 1 : 0;
a1[0][j] = cc[0] == '1' ? 1 : 0;
}
for (int i = 1; i < n; i++)
if (cc[i] == '0')
for (int j = 0; j < 3; j++) {
a0[i][j] = Math.max(a0[i - 1][j], a1[i - 1][j] + 1);
if (j > 0)
a0[i][j] = Math.max(a0[i][j], a0[i - 1][j - 1] + 1);
a1[i][j] = a1[i - 1][j];
}
else
for (int j = 0; j < 3; j++) {
a1[i][j] = Math.max(a1[i - 1][j], a0[i - 1][j] + 1);
if (j > 0)
a1[i][j] = Math.max(a1[i][j], a1[i - 1][j - 1] + 1);
a0[i][j] = a0[i - 1][j];
}
System.out.println(Math.max(a0[n - 1][2], a1[n - 1][2]));
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import sys
def solve(n, x):
ant = x[0]
cont = 1
maximo = 1
maxi = 1
dos = 0
for i in range(1, n):
if(ant != x[i]):
cont += 1
ant = x[i]
if(maximo >= 2):
dos += 1
maximo = 1
else:
maximo += 1
maxi = max(maxi, maximo)
if(maximo >= 2):
dos += 1
# print(maxi, cont)
if(maxi >= 3 or dos >= 2):
return cont + 2
if(dos == 1):
return cont+1
return cont
n = int(input())
x = input()
print(solve(n, x)) | PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 100;
int n;
bool inp[MAXN];
int dp[MAXN][7];
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n; i++) {
char t;
cin >> t;
inp[i] = t - '0';
}
if (inp[0]) {
dp[0][0] = 0;
dp[0][1] = 1;
dp[0][2] = 1;
dp[0][3] = 0;
dp[0][4] = 1;
dp[0][5] = 0;
dp[0][6] = 1;
} else {
dp[0][0] = 1;
dp[0][1] = 0;
dp[0][2] = 0;
dp[0][3] = 1;
dp[0][4] = 0;
dp[0][5] = 1;
dp[0][6] = 1;
}
for (int i = 1; i < n; i++) {
if (inp[i]) {
dp[i][1] = max(dp[i - 1][0] + 1, dp[i - 1][1]);
dp[i][0] = dp[i - 1][0];
dp[i][2] = max(max(dp[i - 1][3] + 1, dp[i - 1][1] + 1), dp[i - 1][2]);
dp[i][3] = dp[i - 1][3];
dp[i][4] = max(dp[i][2], dp[i - 1][4]);
dp[i][5] = max(dp[i - 1][4] + 1, dp[i - 1][5]);
for (int j = 0; j < 6; j++) dp[i][6] = max(dp[i][6], dp[i][j]);
} else {
dp[i][0] = max(dp[i - 1][1] + 1, dp[i - 1][0]);
dp[i][1] = dp[i - 1][1];
dp[i][2] = dp[i - 1][2];
dp[i][3] = max(max(dp[i - 1][2] + 1, dp[i - 1][0] + 1), dp[i - 1][3]);
dp[i][4] = max(dp[i - 1][5] + 1, dp[i - 1][4]);
dp[i][5] = max(dp[i][3], dp[i - 1][5]);
for (int j = 0; j < 6; j++) dp[i][6] = max(dp[i][6], dp[i][j]);
}
}
cout << dp[n - 1][6] << endl;
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author I_love_PloadyFree
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
char[] s = in.next().toCharArray();
out.print(countLen(s) + magic(s));
}
private int magic(char[] s) {
for (int i = 2; i < s.length; i++)
if (s[i] == s[i - 1] && s[i - 1] == s[i - 2])
return 2;
List<Integer> ends = new ArrayList<>();
for (int i = 1; i < s.length; i++)
if (s[i] == s[i - 1])
ends.add(i);
for (int i = 1; i < ends.size(); i++) {
int dist = ends.get(i) - ends.get(i - 1);
if (dist >= 2) return 2;
}
if (s.length >= 2) {
if (s[0] == s[1] || s[s.length - 1] == s[s.length - 2]) {
return 1;
}
}
for (int i = 1; i < s.length; i++)
if (s[i] == s[i - 1])
return 1;
return 0;
}
private int countLen(char[] s) {
int count = 0;
int prev = -1;
for (char c : s) {
if (c != prev) {
prev = c;
count++;
}
}
return count;
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c))
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void print(int i) {
writer.print(i);
}
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 * 1000 + 1000;
int arr[MAXN];
int main() {
int n;
cin >> n;
string s;
cin >> s;
int cnt = 0;
for (int i = 0; i < n - 1; i++)
if (s[i] != s[i + 1]) cnt++;
cout << min(n, cnt + 3) << endl;
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
int Min(int a, int b) {
if (a > b)
return b;
else
return a;
}
int main() {
int n, ans = 0, counter = 0;
char s[100001], tmp, ch = 'd';
scanf("%d", &n);
getchar();
for (int i = 0; i < n; i++) {
scanf("%c", &s[i]);
if (i > 0 && tmp == s[i]) {
if (ch != s[i]) {
ans++;
ch = tmp;
} else {
ans = 2;
}
} else {
counter++;
ch = 'd';
}
tmp = s[i];
}
printf("%d\n", Min(ans, 2) + counter);
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
long long a[100006], b1[100005], b2[100005], b3[100005], n, k, first, ans, mx;
long long sum, l, r, ans1, ans2, ans3;
bool f;
string s, st;
char ch;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
cin >> ch;
a[1] = 0;
if (ch == '1') a[1] = 1;
a[0] = 1000000007;
for (int i = 2; i <= n; ++i) {
cin >> ch;
a[i] = 0;
if (ch == '1') a[i] = 1;
if (!f && a[i] == a[i - 1]) {
f = true;
l = i;
}
}
if (!f) l = n + 1;
f = false;
for (int i = n - 1; i > 0; --i)
if (!f && a[i] == a[i + 1]) {
f = true;
r = i;
break;
}
for (int i = 1; i <= n; ++i) {
if (i < l)
b1[i] = a[i];
else
b1[i] = (a[i] ^ 1);
if (i > r)
b2[i] = a[i];
else
b2[i] = (a[i] ^ 1);
if (i < l || i > r)
b3[i] = a[i];
else
b3[i] = (a[i] ^ 1);
if (i != 1) {
if (b1[i] != b1[i - 1]) ++ans1;
if (b2[i] != b2[i - 1]) ++ans2;
if (b3[i] != b3[i - 1]) ++ans3;
}
}
cout << max(ans1, max(ans2, ans3)) + 1;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.util.*;
public class C334 {
public static void main(String[] args) {
Scanner qwe = new Scanner(System.in);
int n = qwe.nextInt();
int[] bits = new int[n];
String str = qwe.next();
for (int i = 0; i < bits.length; i++) {
bits[i] = str.charAt(i)-'0';
}
int[][] ps = new int[2][n];
int[][] ss= new int[2][n];
for (int i = 0; i < bits.length-1; i++) {
ps[0][i+1] = Math.max(ps[0][i], bits[i] == 0 ? ps[1][i] +1 : 0);
ps[1][i+1] = Math.max(ps[1][i], bits[i] == 1 ? ps[0][i] + 1 : 0);
}
for(int i = bits.length-1; i >= 1; i--){
ss[0][i-1] = Math.max(ss[0][i], bits[i] == 0 ? ss[1][i] +1 : 0);
ss[1][i-1] = Math.max(ss[1][i], bits[i] == 1 ? ss[0][i] + 1 : 0);
}
System.out.println(Math.max(getans(bits,ps,ss,0), getans(bits,ps,ss,1)));
qwe.close();
}
static int getans(int[] bits, int[][] ps, int[][] ss, int start){
int max = 1;
for (int i = 0; i < bits.length; i++) {
if(bits[i] == start){
int look = 1-start;
int runstart = i;
i++;
for(; i < bits.length; i++){
if(bits[i] != look){
i--;
break;
}
look = 1-look;
}
if(i == bits.length) i--;
int runend = i;
//System.out.println("s: " + runstart + " e: " + runend + " val: " + (ps[bits[runstart]][runstart]+ss[bits[runend]][runend] + runend-runstart+1));
max = Math.max(max, ps[bits[runstart]][runstart]+ss[bits[runend]][runend] + runend-runstart+1);
}
}
return max;
}
} | JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class C {
static char[] c;
static int[][][] dp;
public static void main(String[] args) throws IOException {
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
int n = Integer.parseInt(buf.readLine());
c = buf.readLine().toCharArray();
dp = new int[c.length][3][3];
for (int i = 0; i < dp.length; i++)
for (int j = 0; j < dp[0].length; j++)
Arrays.fill(dp[i][j], -1);
System.out.println(go(0, 0, 0));
}
static int go(int index, int last, int flip) {
if (index == c.length)
return 0;
else if (dp[index][last][flip] != -1)
return dp[index][last][flip];
else {
int current = c[index] - '0' + 1;
int max = 0;
if (last == 0) {
max = Math.max(1 + go(index + 1, current, flip),
go(index + 1, last, flip));
} else if (flip == 0) {
max = go(index, last, 1);
if (last != current)
max = Math.max(max, 1 + go(index + 1, current, flip));
else
max = Math.max(max, go(index + 1, last, flip));
} else if (flip == 2) {
if (last != current)
max = 1 + go(index + 1, current, flip);
else
max = go(index + 1, last, flip);
} else if (flip == 1) {
max = go(index, last, 2);
if (current == 1)
current = 2;
else
current = 1;
if (last != current)
max = Math.max(max, 1 + go(index + 1, current, flip));
else
max = Math.max(max, go(index + 1, last, flip));
}
return dp[index][last][flip] = max;
}
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.util.Scanner;
public class Main {
private static int[][] dp1;
private static int[][] dp2;
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
int i , j , n = scan.nextInt() , ans = 0;
String input = scan.next();
dp1 = new int[n][2];
dp2 = new int[n][2];
for (i = 0;i < n;i ++) {
int value = input.charAt(i) - '0';
if (i == 0) {
dp1[0][value] ++;
} else {
dp1[i][0] = dp1[i - 1][0];
dp1[i][1] = dp1[i - 1][1];
if (value == 0) {
int temp = dp1[i - 1][1] + 1;
if (temp > dp1[i][0]) {
dp1[i][0] = temp;
}
} else {
int temp = dp1[i - 1][0] + 1;
if (temp > dp1[i][1]) {
dp1[i][1] = temp;
}
}
}
}
for (i = n - 1;i >= 0;i --) {
int value = input.charAt(i) - '0';
if (i == n - 1) {
dp2[i][value] ++;
} else {
dp2[i][0] = dp2[i + 1][0];
dp2[i][1] = dp2[i + 1][1];
if (value == 0) {
int temp = dp2[i + 1][1] + 1;
if (temp > dp2[i][0]) {
dp2[i][0] = temp;
}
} else {
int temp = dp2[i + 1][0] + 1;
if (temp > dp2[i][1]) {
dp2[i][1] = temp;
}
}
}
}
for (i = 0;i < n;i ++) {
int start = i , end = i;
i ++;
while (i < n && input.charAt(i) != input.charAt(i - 1)) {
end = i;
i ++;
}
i --;
int temp = end - start + 1;
if (start > 0) {
int value = input.charAt(start) - '0';
temp += dp1[start - 1][value];
}
if (end + 1 < n) {
int value = input.charAt(end) - '0';
temp += dp2[end + 1][value];
}
if (temp > ans) {
ans = temp;
}
}
System.out.println(ans);
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
const int mx[8] = {-1, -2, -2, -1, 1, 2, 2, 1};
const int my[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
const double eps = 1e-6;
const double PI = acos(-1.0);
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
int n, ans;
char str[maxn];
int main() {
while (~scanf("%d", &n)) {
scanf("%s", str), ans = 1;
for (int i = 1; i < n; ++i) ans += (str[i - 1] ^ str[i]);
if (ans >= n - 1)
ans = n;
else
ans += 2;
printf("%d\n", ans);
}
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author ilyakor
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
String s = in.nextToken();
int[] a = new int[n];
for (int i = 0; i < n; ++i) {
a[i] = s.charAt(i) == '0' ? 0 : 1;
}
int[][][] d = new int[3][2][n];
d[0][a[0]][0] = 1;
d[1][1 - a[0]][0] = 1;
for (int i = 1; i < n; ++i) {
for (int state = 0; state < 3; ++state)
for (int last = 0; last < 2; ++last) {
d[state][last][i] = Math.max(d[state][last][i], d[state][last][i - 1]);
int val = d[state][last][i - 1];
if (val <= 0) continue;
int x = a[i];
if (state == 1)
x = 1 - a[i];
if (last != x)
d[state][x][i] = Math.max(d[state][x][i], val + 1);
if (last != 1 - x && state < 2) {
d[state + 1][1 - x][i] = Math.max(d[state + 1][1 - x][i], val + 1);
}
}
}
int res = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 2; ++j)
res = Math.max(res, d[i][j][n - 1]);
out.printLine(res);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
static class InputReader {
private InputStream stream;
private byte[] buffer = new byte[10000];
private int cur;
private int count;
public InputReader(InputStream stream) {
this.stream = stream;
}
public static boolean isSpace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int read() {
if (count == -1) {
throw new InputMismatchException();
}
try {
if (cur >= count) {
cur = 0;
count = stream.read(buffer);
if (count <= 0)
return -1;
}
} catch (IOException e) {
throw new InputMismatchException();
}
return buffer[cur++];
}
public int readSkipSpace() {
int c;
do {
c = read();
} while (isSpace(c));
return c;
}
public String nextToken() {
int c = readSkipSpace();
StringBuilder sb = new StringBuilder();
while (!isSpace(c)) {
sb.append((char) c);
c = read();
}
return sb.toString();
}
public int nextInt() {
int sgn = 1;
int c = readSkipSpace();
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res = res * 10 + c - '0';
c = read();
} while (!isSpace(c));
res *= sgn;
return res;
}
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | n = map(int, input())
s = input()
l = []
cnt = 1
for i in range( 1, len(s)):
if s[i] != s[i-1]:
l.append(cnt)
cnt = 1
else:
cnt += 1
l.append(cnt)
result = len(l)
max_pair = 0
max_count = 0
for i in range(len(l)):
if l[i] == 2:
max_pair += 1
max_count = max( max_count, l[i])
if max_pair >= 2 or max_count >= 3:
add = 2
elif max_pair == 1:
add = 1
else:
add = 0
print(result + add)
| PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, dp[N][2], su[N][2];
string s;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> s;
dp[1][s[0] - '0'] = 1;
for (int i = 2; i <= n; i++) {
int cur = s[i - 1] - '0';
dp[i][cur] = max(dp[i - 1][cur], dp[i - 1][!cur] + 1);
dp[i][!cur] = dp[i - 1][!cur];
}
su[n][s[n - 1] - '0'] = 1;
for (int i = n - 1; i >= 1; i--) {
int cur = s[i - 1] - '0';
su[i][cur] = max(su[i + 1][cur], su[i + 1][!cur] + 1);
su[i][!cur] = su[i + 1][!cur];
}
int ans = max(dp[n][0], dp[n][1]);
for (int i = 1; i <= n; i++) {
int cur = s[i - 1] - '0';
ans = max(ans, dp[i - 1][cur] + 1 + su[i + 1][cur]);
}
int i = 1, j = 1;
int len = 1;
while (i <= j && j <= n - 1) {
if (s[j] != s[j - 1]) {
j++;
len++;
} else {
int le = s[i - 1] - '0';
int ri = s[j - 1] - '0';
ans = max(ans, dp[i - 1][le] + len + su[j + 1][ri]);
i = j + 1;
j = i;
len = 1;
}
}
if (len != 1) {
int le = s[i - 1] - '0';
int ri = s[j - 1] - '0';
ans = max(ans, dp[i - 1][le] + len + su[j + 1][ri]);
i = j + 1;
j = i;
len = 1;
}
cout << ans << '\n';
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import sys
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
input = sys.stdin.readline
t = 1
while t:
t -= 1
n = int(input())
s = input()
res = 1
for i in range(1, n):
res += (1 if s[i] != s[i-1] else 0)
print(min(n, res+2))
| PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | n=int(input())
a=input()
f=a[0]
if(n==1 or n==2):
print(n)
else:
i=1
t1=1
while(i<n):
if(a[i]!=f):
f=a[i]
t1+=1
i+=1
t3=0
t2=1
i=1
f1=0
f=a[0]
q=0
while(i<n):
if(a[i]==f):
t2+=1
if(t2==2):
q=1
t3+=1
if(t3==2):
f1=1
break
if(t2>2):
f1=1
break
else:
t2=1
f=a[i]
i+=1
if(q==1):
if(f1==1):
print(t1+2)
else:
print(t1+1)
else:
print(t1)
| PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | //package codeforces;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* Created by nitin.s on 14/03/16.
*/
public class AlternativeThinking {
static class CustomPair<Character, Integer> {
private Character first; //first member of pair
private Integer second; //second member of pair
public CustomPair(Character first, Integer second) {
this.first = first;
this.second = second;
}
public void setFirst(Character first) {
this.first = first;
}
public void setSecond(Integer second) {
this.second = second;
}
public Character getFirst() {
return first;
}
public Integer getSecond() {
return second;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
String s = in.nextLine();
List<CustomPair> list = new ArrayList<CustomPair>();
list.add(new CustomPair(s.charAt(0), 1));
for(int i = 1; i < s.length(); ++i) {
if(s.charAt(i) == list.get(list.size() - 1).getFirst()) {
CustomPair customPair = list.get(list.size() - 1);
customPair.setSecond((Integer)customPair.getSecond() + 1);
list.set(list.size() - 1, customPair);
} else {
list.add(new CustomPair(s.charAt(i), 1));
}
}
boolean ok = false;
int cnt = 0;
for(int i = 0; i < list.size(); ++i) {
if((Integer)list.get(i).second == 2) {
++cnt;
ok = true;
if(cnt >= 2) {
break;
}
} else {
if((Integer)list.get(i).second > 2) {
System.out.println(list.size() + 2);
return;
}
}
}
if(ok) {
System.out.println(list.size() + cnt);
} else {
System.out.println(list.size());
}
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, m;
int dp[maxn][5];
char ss[maxn];
int main() {
scanf("%d", &n);
getchar();
scanf("%s", ss + 1);
for (int i = 1; i <= n; i++) {
if (ss[i] == '0') {
dp[i][0] = dp[i - 1][1] + 1;
dp[i][1] = dp[i - 1][1];
} else if (ss[i] == '1') {
dp[i][0] = dp[i - 1][0];
dp[i][1] = dp[i - 1][0] + 1;
}
}
int ans = max(1, max(dp[n][0], dp[n][1]));
int cnt1, cnt2, l1, l2;
cnt1 = cnt2 = 0;
for (int i = 1; i <= n; i++) {
if (ss[i] == '0') {
int tt = 0;
for (int j = i; j <= n; j++) {
if (ss[j] == '0') {
tt++;
if (j == n) {
if (cnt1 < tt) {
cnt1 = tt;
}
}
i = i + tt;
} else {
if (cnt1 < tt) {
cnt1 = tt;
}
i = j - 1;
break;
}
}
} else if (ss[i] == '1') {
int tt = 0;
for (int j = i; j <= n; j++) {
if (ss[j] == '1') {
tt++;
if (j == n) {
if (cnt2 < tt) {
cnt2 = tt;
}
}
i = i + tt;
} else {
if (cnt2 < tt) {
cnt2 = tt;
}
i = j - 1;
break;
}
}
}
}
int maxx = max(cnt1, cnt2);
if (maxx >= 2) {
printf("%d\n", min(n, ans + 2));
} else
printf("%d\n", ans);
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | def main():
n, t, a = int(input()), 0, '*'
for b in input():
if a == b:
t += 1
else:
a = b
print(n - max(t - 2, 0))
if __name__ == '__main__':
main()
| PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | # CodeForces Div-1 603-A
# Greedy Approach
n = int(raw_input())
seq = map(int, list(raw_input()))
#print seq
changed = False
changedPos = -1
longest = 0
length = 1
start = seq[0]
for i in xrange(1, n):
if seq[i] != seq[i-1] :
length += 1
else :
if not changed or changedPos == i-1 :
seq[i] = not seq[i-1]
changed = True
length += 1
changedPos = i
#print seq
print length | PYTHON |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class C {
static int n;
static char[] s;
static int[][][] memo;
static int dp(int lst, int flip, int idx)
{
if(idx == n)
return 0;
if(memo[lst][flip][idx] != -1)
return memo[lst][flip][idx];
//go with flipping
int x = 0, y = 0, z = 0;
if(flip == 1)
{
x = dp(1-( s[idx] - '0'), flip, idx + 1) + (idx == 0 || 1- ( s[idx] - '0') != lst ? 1 : 0);
y = dp(1-( s[idx] - '0'), 2, idx + 1) + (idx == 0 || 1-( s[idx] - '0') != lst ? 1 : 0);
}
else
{
x = dp(s[idx] - '0', flip, idx + 1) + (idx == 0 || s[idx] - '0' != lst ? 1 : 0);
if(flip == 0)
{
y = dp(1-( s[idx] - '0'), 1, idx + 1) + (idx == 0 || 1-( s[idx] - '0') != lst ? 1 : 0);
z = dp(1-( s[idx] - '0'), 2, idx + 1) + (idx == 0 || 1-( s[idx] - '0') != lst ? 1 : 0);
}
}
return memo[lst][flip][idx] = Math.max(x, Math.max(y, z));
}
public static void sol() throws IOException
{
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
n = sc.nextInt();
s = sc.next().toCharArray();
memo = new int[2][3][n];
for(int i = 0; i < 2; ++i)
for(int j = 0; j < 3; ++j)
Arrays.fill(memo[i][j], -1);
out.println(dp(0, 0, 0));
out.flush();
out.close();
}
public static void main(String[] args) {new Thread(null, new Runnable() { public void run() {try {
sol();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}}, "2",1<<26).start();}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public boolean ready() throws IOException {return br.ready();}
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.awt.*;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
StringBuilder sb = new StringBuilder();
int n=sc.nextInt();
String s=sc.next();
int count=1;
for(int i=1;i<n;i++){
if(s.charAt(i)!=s.charAt(i-1))count++;
}
if(count+2<=n)count+=2;
else if(count+1<=n)count+=1;
System.out.println(count);
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); }
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
} | JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | from sys import maxsize, stdout, stdin,stderr
mod = int(1e9+7)
import sys
def I(): return int(stdin.readline())
def lint(): return [int(x) for x in stdin.readline().split()]
def S(): return input().strip()
def grid(r, c): return [lint() for i in range(r)]
from collections import defaultdict, Counter, deque
import math
import heapq
from heapq import heappop , heappush
import bisect
from itertools import groupby
def gcd(a,b):
while b:
a %= b
tmp = a
a = b
b = tmp
return a
def lcm(a,b):
return a // gcd(a, b) * b
def check_prime(n):
for i in range(2, int(n ** (1 / 2)) + 1):
if not n % i:
return False
return True
def Bs(a, x):
i=0
j=0
left = 1
right = x
flag=False
while left<right:
mi = (left+right)//2
#print(smi,a[mi],x)
if a[mi]<=x:
left = mi+1
i+=1
else:
right = mi
j+=1
#print(left,right,"----")
#print(i-1,j)
if left>0 and a[left-1]==x:
return i-1, j
else:
return -1, -1
def nCr(n, r):
return (fact(n) // (fact(r)
* fact(n - r)))
# Returns factorial of n
def fact(n):
res = 1
for i in range(2, n+1):
res = res * i
return res
def primefactors(n):
num=0
while n % 2 == 0:
num+=1
n = n / 2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
num+=1
n = n // i
if n > 2:
num+=1
return num
'''
def iter_ds(src):
store=[src]
while len(store):
tmp=store.pop()
if not vis[tmp]:
vis[tmp]=True
for j in ar[tmp]:
store.append(j)
'''
def ask(a):
print('? {}'.format(a),flush=True)
n=I()
return n
def dfs(i,p):
a,tmp=0,0
for j in d[i]:
if j!=p:
a+=1
tmp+=dfs(j,i)
if a==0:
return 0
return tmp/a + 1
def primeFactors(n):
l=[]
while n % 2 == 0:
l.append(2)
n = n // 2
if n > 2:
l.append(n)
return l
n = I()
s = list(map(int,input().strip()))
i,cnt=0,0
tmp=s[0]
ans=1
for i in range(1,n):
if s[i]!=tmp:
ans+=1
tmp=s[i]
print(min(ans+2,n))
| PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int N;
string S;
vector<int> occ;
int main() {
ios::sync_with_stdio(0);
cin >> N >> S;
char last = S[0];
int len = 1;
for (int i = 1; i < N; i++) {
if (S[i] == last)
len++;
else {
occ.push_back(len);
last = S[i];
len = 1;
}
}
occ.push_back(len);
int ans = occ.size(), cnt2 = 0, cnt3 = 0;
for (int i = 0; i < occ.size(); i++) {
cnt2 += (occ[i] >= 2);
cnt3 += (occ[i] >= 3);
}
if (cnt3 >= 1 || cnt2 > 1)
ans += 2;
else if (cnt2 == 1)
ans++;
cout << ans << "\n";
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.util.Scanner;
/**
* Created by Anton on 06/12/2015.
*/
public class altThinking {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Integer altCounter = 1, strSize;
String line;
line = sc.nextLine();
strSize = Integer.parseInt(line);
line = sc.nextLine();
char altChar = line.charAt(0);
for(int i = 1; i < strSize; i++){
if(line.charAt(i)!= altChar){
altCounter++;
altChar = line.charAt(i);
}
}
System.out.println(Math.min(strSize, altCounter+2));
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int a[111111];
char ch[111111];
int main() {
int n, k;
while (~scanf("%d", &n)) {
scanf("%s", ch + 1);
for (int i = 1; i <= n; i++) a[i] = ch[i] - '0';
int ans = 1;
int now = a[1];
for (int i = 2; i <= n; i++) {
if (a[i] != now) ans++;
now = a[i];
}
now = a[1];
int tlen = 1;
int id1 = 1;
for (int i = 2; i <= n; i++) {
if (now == a[i])
tlen++;
else {
if (tlen > 1) {
id1 = i - 1;
break;
}
tlen = 1;
now = a[i];
}
}
if (id1 == 1 && tlen > 1) id1 = n;
tlen = 1;
now = a[n];
int id2 = n;
for (int i = n - 1; i >= 1; i--) {
if (now == a[i])
tlen++;
else {
if (tlen > 1) {
id2 = i + 1;
break;
}
tlen = 1;
now = a[i];
}
}
if (id2 == n && tlen > 1) id2 = 1;
if (id2 + tlen - 1 == id1 && a[id1] == a[id2] && tlen > 1) {
if (tlen >= 3)
ans += 2;
else
ans++;
} else if (tlen > 1)
ans += 2;
else
ans += 0;
printf("%d\n", ans);
}
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const long long int N = 200005;
const long long int mod = 1e9 + 7;
long long int dp[200005][2][3];
long long int n;
char s[200005];
long long int A[N], B[N];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> s;
long long int i, j, k;
for (i = 1; i <= n; i++) {
A[i] = (s[i - 1] - '0');
}
for (i = 1; i <= n; i++) {
bool x = s[i - 1] - '0';
for (j = 0; j < 3; j++) {
for (k = 0; k < 2; k++) dp[i][k][j] = dp[i - 1][k][j];
}
dp[i][x][0] = max(dp[i][x][0], dp[i - 1][!x][0] + 1);
dp[i][x][1] = max(dp[i][x][1], dp[i - 1][!x][1] + 1);
dp[i][x][2] = max(dp[i][x][2], dp[i - 1][!x][2] + 1);
dp[i][x][1] = max(dp[i][x][1], dp[i][!x][0]);
dp[i][!x][1] = max(dp[i][!x][1], dp[i][x][0]);
dp[i][x][2] = max(dp[i][x][2], dp[i][!x][1]);
dp[i][!x][2] = max(dp[i][!x][2], dp[i][x][1]);
}
cout << max(dp[n][0][2], dp[n][1][2]);
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e9, mod = 14741;
int main() {
int n;
cin >> n;
string s;
cin >> s;
char x = s[0];
int p = 1;
for (int i = 0; i < n; i++) {
if (s[i] != x) {
x = s[i];
p++;
}
}
x = s[0];
int i = 0, ok1 = 0, ok2 = 0, p1 = 0, p2 = 0;
while (i < n) {
p1 = 0;
while (i < n && s[i] == x) {
p1++;
i++;
}
p2 = 0;
while (i < n && s[i] != x) {
p2++;
i++;
}
}
if (p + 2 <= n)
cout << p + 2;
else
cout << n;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 |
import java.util.Scanner;
public class AlternativeThinking {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String x = sc.next();
sc.close();
int alternating = 1;
for (int i = 1; i < n; i++) {
if (x.charAt(i) != x.charAt(i - 1)) {
alternating++;
}
}
System.out.println(Math.min(alternating + 2, n));
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
private static int max;
static class FastScanner {
BufferedReader s;
StringTokenizer st;
public FastScanner(InputStream InputStream) {
st = new StringTokenizer("");
s = new BufferedReader(new InputStreamReader(InputStream));
}
public FastScanner(File f) throws FileNotFoundException {
st = new StringTokenizer("");
s = new BufferedReader(new FileReader(f));
}
public int nextInt() throws IOException {
if (st.hasMoreTokens())
return Integer.parseInt(st.nextToken());
else {
st = new StringTokenizer(s.readLine());
return nextInt();
}
}
public BigInteger big() throws IOException {
if (st.hasMoreTokens())
return new BigInteger(st.nextToken());
else {
st = new StringTokenizer(s.readLine());
return big();
}
}
public double nextDouble() throws IOException {
if (st.hasMoreTokens())
return Double.parseDouble(st.nextToken());
else {
st = new StringTokenizer(s.readLine());
return nextDouble();
}
}
public long nextLong() throws IOException {
if (st.hasMoreTokens())
return Long.parseLong(st.nextToken());
else {
st = new StringTokenizer(s.readLine());
return nextLong();
}
}
public String next() throws IOException {
if (st.hasMoreTokens())
return st.nextToken();
else {
st = new StringTokenizer(s.readLine());
return next();
}
}
public String nextLine() throws IOException {
return s.readLine();
}
public void close() throws IOException {
s.close();
}
}
static StringBuilder sb = new StringBuilder("");
public static void main(String[] args) throws java.lang.Exception {
FastScanner in = new FastScanner(System.in);
int n = in.nextInt();
if(n == 1) {
System.out.println(1);
return;
}
String str = in.next();
int a1 = 0, a2 = 0;
for(int x = 0; x < n; x++) {
if(str.charAt(x) == '0')
a2 = a1 + 1;
else
a1 = a2 + 1;
}
boolean flag2 = false;
int flag1 = (str.charAt(0) == str.charAt(1)? 1: 0);
for(int x = 1; x < n - 1; x++) {
if(str.charAt(x) == str.charAt(x - 1) && str.charAt(x) == str.charAt(x + 1)) {
flag2 = true;
break;
}
if(str.charAt(x) == str.charAt(x + 1)) {
flag1++;
if(flag1 == 2)
break;
}
}
System.out.println(Math.max(a1, a2) + (flag2 ? 2: flag1));
}
} | JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.io.*;
import java.util.*;
import java.text.*;
public class Main {
public static void main (String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String st[] = br.readLine().split(" ");
int n=Integer.parseInt(st[0]);
st=br.readLine().split(" ");
String str=st[0];
int l=1;
char prev=str.charAt(0);
for(int i=1;i<str.length();i++){
if(prev!=str.charAt(i)){
l++;
}
prev=str.charAt(i);
}
System.out.println(Math.min(l+2,n));
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 |
import java.util.Arrays;
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author hitarthk
*/
public class C_334_C {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
char[] a = in.nextLine().toCharArray();
//System.out.println(Arrays.toString(a));
int[][] tree = new int[4 * n][1 + 2 + 2];
build(1, 0, n - 1, tree, a);
System.out.println(Math.min(n, tree[1][4] + 2));
}
static void build(int node, int s, int e, int[][] tree, char[] a) {
if (s == e) {
tree[node][4] = 1;
if (a[s] == '0') {
tree[node][0] = 1;
} else {
tree[node][3] = 1;
}
//System.out.println(s+" "+e+" "+Arrays.toString(tree[node]));
} else {
int mid = (s + e) >> 1;
build(2 * node, s, mid, tree, a);
build(2 * node + 1, mid + 1, e, tree, a);
tree[node] = merge(tree[2 * node], tree[2 * node + 1]);
//System.out.println(s+" "+e+" "+Arrays.toString(tree[node]));
}
}
static int[] merge(int[] l, int[] r) {
int[] ans = new int[5];
ans[0] = Math.max(l[0], r[0]);
if (l[0] > 0 && r[2] > 0) {
ans[0] = Math.max(ans[0], l[0] + r[2]);
}
if (l[1] > 0 && r[0] > 0) {
ans[0] = Math.max(ans[0], l[1] + r[0]);
}
ans[1] = Math.max(l[1], r[1]);
if (l[0] > 0 && r[3] > 0) {
ans[1] = Math.max(ans[1], l[0] + r[3]);
}
if (l[1] > 0 && r[1] > 0) {
ans[1] = Math.max(ans[1], l[1] + r[1]);
}
ans[2] = Math.max(l[2], r[2]);
if (l[2] > 0 && r[2] > 0) {
ans[2] = Math.max(ans[2], l[2] + r[2]);
}
if (l[3] > 0 && r[0] > 0) {
ans[2] = Math.max(ans[2], l[3] + r[0]);
}
ans[3] = Math.max(l[3], r[3]);
if (l[2] > 0 && r[3] > 0) {
ans[3] = Math.max(ans[3], l[2] + r[3]);
}
if (l[3] > 0 && r[1] > 0) {
ans[3] = Math.max(ans[3], l[3] + r[1]);
}
ans[4] = Math.max(l[4], r[4]);
for (int i = 0; i < 4; i++) {
ans[4] = Math.max(ans[4], ans[i]);
}
return ans;
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 |
import java.util.*;
import java.io.*;
public class A
{
public static void main(String[] args) throws Exception
{
PrintWriter out = new PrintWriter(System.out);
new A(new FastScanner(System.in), out);
out.close();
}
int N;
int[] vs;
Integer[][][] memo;
int go(int last, int s, int i)
{
if (i == N) return 0;
if (memo[last][s][i] != null)
return memo[last][s][i];
int res = 0;
if (s < 2)
res = Math.max(res, go(last, s+1, i));
int color = s&1;
int cur = vs[i] ^ color;
if (cur != last)
res = Math.max(res, 1+go(cur, s, i+1));
res = Math.max(res, go(last, s, i+1));
return memo[last][s][i] = res;
}
public A(FastScanner in, PrintWriter out)
{
N = in .nextInt();
vs = new int[N];
memo = new Integer[2][3][N];
String s = in.next();
for (int i=0; i<N; i++)
vs[i] = s.charAt(i)-'0';
for (int i=N-1; i>=0; i--)
for (int x=0; x<2; x++)
for (int y=0; y<3; y++)
go(x, y, i);
out.println(Math.max(go(0, 0, 0), go(1, 0, 0)));
}
}
class FastScanner{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream)
{
this.stream = stream;
}
int read()
{
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars){
curChar = 0;
try{
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
boolean isSpaceChar(int c)
{
return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
}
boolean isEndline(int c)
{
return c=='\n'||c=='\r'||c==-1;
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String next(){
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do{
res.appendCodePoint(c);
c = read();
}while(!isSpaceChar(c));
return res.toString();
}
String nextLine(){
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do{
res.appendCodePoint(c);
c = read();
}while(!isEndline(c));
return res.toString();
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class A {
Scanner scanner = new Scanner(System.in);
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
PrintWriter cout = new PrintWriter(System.out);
StringTokenizer tok;
int n;
String s;
int a[];
int mx = 1;
int sm;
int ans = 1;
ArrayList<Integer> p = new ArrayList<Integer>();
private void run() throws IOException {
tok = new StringTokenizer(cin.readLine());
n = Integer.parseInt(tok.nextToken());
tok = new StringTokenizer(cin.readLine());
s = tok.nextToken();
a = new int[n];
for (int i = 0; i < n; ++i) {
if (s.charAt(i) == '0') {
a[i] = 0;
} else {
a[i] = 1;
}
}
sm = a[0];
for (int i = 1; i < n; ++i) {
if (a[i] != sm) {
sm = a[i];
ans++;
}
}
int cur = 1;
for (int i = 1; i < n; ++i) {
if (a[i] == a[i - 1]) {
cur++;
} else {
p.add(cur);
cur = 1;
}
}
p.add(cur);
mx = p.get(0);
for (int i = 0; i < p.size(); ++i) {
mx = Math.max(p.get(i), mx);
}
if (mx >= 3) {
ans += 2;
} else {
int cnt2 = 0;
for (int i = 0; i < p.size(); ++i) {
if (p.get(i) == 2) {
cnt2++;
}
}
if (cnt2 >= 2) {
ans += 2;
} else {
if (cnt2 == 1) {
ans++;
}
}
}
//System.out.println("ans = " + ans);
//System.out.println("mx = " + mx);
cout.println(ans);
scanner.close();
cin.close();
cout.close();
}
public static void main(String[] args) throws IOException {
A solution = new A();
solution.run();
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
char a[2 * maxn + 5];
int main() {
int n;
cin >> n;
cin >> a;
int ans = 1;
int p = a[0], c = 0;
for (int i = 1; i < n; i++) {
if (a[i] != a[i - 1])
ans++;
else if (c < 2 && a[i] == a[i - 1]) {
ans++;
c++;
}
}
cout << ans << endl;
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
void qread(int &x) {
int neg = 1;
x = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') neg = -1;
c = getchar();
}
while (c >= '0' && c <= '9') x = 10 * x + c - '0', c = getchar();
x *= neg;
}
const int maxn = 100005;
int n;
char c[maxn];
int dp[maxn][2][3];
int main() {
scanf("%d%s", &n, c);
memset(dp, -1, sizeof(dp));
dp[0][0][0] = dp[0][1][0] = 0;
for (int(i) = 0; (i) < n; i++)
for (int(j) = 0; (j) < 2; j++)
for (int(k) = 0; (k) < 3; k++) {
int cur = dp[i][j][k];
if (cur < 0) continue;
dp[i + 1][j][k] = max(dp[i + 1][j][k], cur);
if (c[i] - '0' != j)
dp[i + 1][c[i] - '0'][k] = max(dp[i + 1][c[i] - '0'][k], cur + 1);
else if (k < 2)
dp[i + 1][c[i] - '0'][k + 1] =
max(dp[i + 1][c[i] - '0'][k + 1], cur + 1);
}
int ans = 0;
for (int(j) = 0; (j) < 2; j++)
for (int(k) = 0; (k) < 3; k++) ans = max(ans, dp[n][j][k]);
printf("%d", ans);
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 8, inf = 1e18 + 9, mod = 1e9 + 7;
char s[maxn];
long long n, m;
void solve() {
long long i, j, ans = 1;
cin >> n >> (s + 1);
for (i = 2; i <= n; i++)
if (s[i] != s[i - 1]) ans++;
cout << min(ans + 2, n) << endl;
}
signed main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
long long t = 1;
while (t--) solve();
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int count(char str[]) {
int start = 0;
char last;
last = str[0];
start = 1;
for (int i = 1; i < strlen(str); i++) {
if (str[i] != last) {
last = str[i];
start++;
}
}
return start;
}
char flip(char a) {
if (a == '1') return '0';
if (a == '0') return '1';
}
int main() {
int n;
scanf("%d", &n);
char str[200005];
scanf("%s", &str);
int i, j;
i = 1;
j = n - 2;
int s1, s2;
s1 = -1;
s2 = -1;
printf("%d\n", min(2 + count(str), n));
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 |
import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s = sc.next();
int len = 1;
for (int i = 1; i<n; i++)
{
if (s.charAt(i) != s.charAt(i - 1))
len++;
}
System.out.println(Math.min(n, len + 2));
}
} | JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
void cline() { cout << '\n'; }
template <typename T, typename... V>
void cline(T t, V... v) {
cout << t;
if (sizeof...(v)) cout << ' ';
cline(v...);
}
void cspc() { cout << ' '; }
template <typename T, typename... V>
void cspc(T t, V... v) {
cout << t;
if (sizeof...(v)) cout << ' ';
cspc(v...);
}
const int N = 1e6 + 4;
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
int inl = 1;
for (int i = 1; i < n; ++i) {
if (s[i] != s[i - 1]) inl++;
}
int flag = 0;
for (int i = 0; i <= n - 2; ++i) {
if ((s[i] == s[i + 1])) {
flag++;
}
}
if (flag > 1) inl += 2;
if (flag < 2) {
for (int i = 0; i < n - 1; ++i) {
if (i != n - 2) {
if ((s[i] == s[i + 1])) {
if (s[i + 1] == s[i + 2]) inl++;
inl++;
break;
}
} else {
if (s[i] == s[i + 1]) {
inl++;
break;
}
}
}
}
cline(inl);
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | n = int(input())
s = input()
ans = 1
for i in range(1 , n):
if s[i] != s[i-1] :
ans += 1
print(min(n , ans + 2 )) | PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
string s;
int main() {
int n, l = 0;
cin >> n >> s;
for (int i = 1; i < n; i++)
if (s[i] != s[i - 1]) l++;
cout << min(n, l + 3);
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | n,a=input(),raw_input()
print min(a.count("01")+a.count("10")+3,n)
| PYTHON |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
int main() {
std::ios_base::sync_with_stdio(false);
int N, sol = 1;
std::string S;
std::cin >> N;
std::cin >> S;
for (int i = 0; i < N - 1; i++) sol += (S[i] != S[i + 1]);
std::cout << std::min(N, sol + 2);
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 |
import java.awt.Point;
import java.awt.geom.Line2D;
import java.io.BufferedReader;
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.security.GuardedObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.io.InputStream;
import java.math.BigInteger;
import javax.activation.CommandInfo;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
long var=System.currentTimeMillis();
solver.solve(1, in, out);
/*if(System.getProperty("ONLINE_JUDGE")==null){
out.println("["+(double)(System.currentTimeMillis()-var)/1000+"]");
}*/
out.close();
}
}
class Pair {
long x,y,index;
Pair(long x,long y,int index){
this.x=x;
this.y=y;
this.index=index;
}
public String toString(){
return this.x+" "+this.y+" ";
}
}
class tupple{
long x,y,z;
ArrayList<String> list=new ArrayList<>();
tupple(long x,long y,long z){
this.x=x;
this.y=y;
this.z=z;
}
tupple(tupple cl){
this.x=cl.x;
this.y=cl.y;
this.z=cl.z;
this.list=cl.list;
}
}
class Edge{
int u;
int v;
int time;
long cost;
public Edge(int u,int v){
this.u=u;
this.v=v;
time=Integer.MAX_VALUE;
}
public int other(int x){
if(u==x)return v;
return u;
}
}
class Ss{
Integer a[]=new Integer[3];
Ss(Integer a[]){
this.a=a;
}
public int hashCode(){
return a[0].hashCode()+a[1].hashCode()+a[2].hashCode();
}
public boolean equals(Object o){
Ss x=(Ss)o;
for(int i=0;i<3;i++){
if(x.a[i]!=this.a[i]){
return false;
}
}
return true;
}
}
class queary{
int type;
int l;
int r;
int index;
queary(int type,int l,int r){
this.type=type;
this.l=l;
this.r=r;
}
}
class boat implements Comparable<boat>{
int capacity;
int index;
boat(int capacity,int index)
{
this.capacity=capacity;
this.index=index;
}
@Override
public int compareTo(boat o) {
// TODO Auto-generated method stub
return this.capacity-o.capacity;
}
}
class angle{
double x,y,angle;
int index;
angle(int x,int y,double angle){
this.x=x;
this.y=y;
this.angle=angle;
}
}
class TaskC {
static long mod=(long)(1e9+7);
// SegTree tree;
// int min[];
static int prime_len=100001;
static int prime[]=new int[prime_len];
static ArrayList<Integer> primes=new ArrayList<>();
static{
for(int i=2;i<=Math.sqrt(prime.length);i++){
for(int j=i*i;j<prime.length;j+=i){
prime[j]=i;
}
}
for(int i=2;i<prime.length;i++){
if(prime[i]==0){
primes.add(i);
}
}
// System.out.println("end");
// prime[0]=true;
// prime[1]=1;
}
/*
* long pp[]=new long[10000001];
pp[0]=0;
pp[1]=1;
for(int i=2;i<pp.length;i++){
if(prime[i]!=0){
int gcd=(int)greatestCommonDivisor(i/prime[i], prime[i]);
pp[i]=(pp[i/prime[i]]*pp[prime[i]]*gcd)/pp[(int)gcd];
}
else
pp[i]=i-1;
}
* */
boolean visited[][];
char mat[][];
static int time=0;
public void solve(int testNumber, FastScanner in, PrintWriter out) throws IOException {
int n=in.nextInt();
int a[]=new int[n];
int last=0;
int max=0;
int count=1;
int ans=1;
String s=in.readString();
for(int i=0;i<n;i++){
a[i]=s.charAt(i)-'0';
if(i!=0){
if(last!=a[i]){
last=a[i];
count=1;
ans++;
}
else{
count++;
}
}
else{
last=a[0];
}
max=Math.max(max, count);
}
if(max>1){
out.println(Math.min(n,ans+2));
}
else{
out.println(ans);
}
}
static void dfs(List<Integer>[] graph, boolean[] used, List<Integer> res, int u,int parent,List<Integer> collection) {
used[u] = true;
Integer uu=new Integer(u);
collection.add(uu);
for (int v : graph[u]){
if (!used[v]){
dfs(graph, used, res, v,u,collection);
}
else if(collection.contains(v)){
System.out.println("Impossible");
System.exit(0);
}
}
collection.remove(uu);
res.add(u);
}
public static List<Integer> topologicalSort(List<Integer>[] graph) {
int n = graph.length;
boolean[] used = new boolean[n];
List<Integer> res = new ArrayList<>();
for (int i = 0; i < n; i++)
if (!used[i])
dfs(graph, used, res, i,-1,new ArrayList<Integer>());
Collections.reverse(res);
return res;
}
static class pairs{
String company,color;
String type;
boolean visit=false;
pairs(String company,String color,String type){
this.company=company;
this.color=color;
this.type=type;
}
}
BigInteger BMOD=BigInteger.valueOf(mod);
private long inv(long q) {
return BigInteger.valueOf(q).modInverse(BMOD).longValue();
}
static void dfs1(List<Integer>[] graph, boolean[] used, List<Integer> res, int u) {
used[u] = true;
for (int v : graph[u])
if (!used[v])
dfs1(graph, used, res, v);
res.add(u);
}
public static List<Integer> topologicalSort1(List<Integer>[] graph) {
int n = graph.length;
boolean[] used = new boolean[n];
List<Integer> res = new ArrayList<>();
for (int i = n-1; i >0; i--)
if (!used[i])
dfs1(graph, used, res, i);
Collections.reverse(res);
return res;
}
public static long[][] mulmatrix(long m1[][],long m2[][],long mod){
long ans[][]=new long[m1.length][m2[0].length];
for(int i=0;i<m1.length;i++){
for(int j=0;j<m2[0].length;j++){
for(int k=0;k<m1.length;k++){
ans[i][j]+=(m1[i][k]*m2[k][j]);
ans[i][j]%=mod;
}
}
}
return ans;
}
public static long[][] matrixexpo(long m[][],String n,long mod){
if(n.equals("1")){
return m.clone();
}
if(n.equals("10")){
return mulmatrix(m, m , mod);
}
else{
long temp [][]=matrixexpo(m,n.substring(0,n.length()-1),mod);
temp=mulmatrix(temp, temp, mod);
if(n.charAt(n.length()-1)=='0')return temp;
else return mulmatrix(temp, m,mod);
}
}
public static boolean isCompatible(long x[],long y[]){
for(int i=0;i<x.length-1;i++){
if(x[i]==y[i] && x[i+1]==y[i+1] && x[i]==x[i+1] && y[i]==y[i+1]){
return false;
}
}
return true;
}
int phi(int n) {
int res = n;
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
while (n % i == 0) {
n /= i;
}
res -= res / i;
}
}
if (n != 1) {
res -= res / n;
}
return res;
}
long pow(long x,long y,long mod){
if(y<=0){
return 1;
}
if(y==1){
return x%mod;
}
long temp=pow(x,y/2,mod);
if(y%2==0){
return (temp*temp)%mod;
}
else{
return (((temp*temp)%mod)*x)%mod;
}
}
/* long DP(int id,int mask){
//System.out.println("hi"+dp[id][mask]+" "+id+" "+Integer.toBinaryString(mask));
if(id==0 && mask==0){
dp[0][mask]=1;
return 1;
}
else if(id==0){
dp[0][mask]=0;
return 0;
}
if(dp[id][mask]!=-1){
return dp[id][mask];
}
long ans=0;
for(int i=0;i<n;i++){
if((mask & (1<<i))!=0 && c[i][id]>=1){
ans+=DP(id-1,mask ^ (1 << i));
ans%=mod;
}
}
ans+=DP(id-1,mask);
ans%=mod;
dp[id][mask]=ans;
return ans;
}*/
static long greatestCommonDivisor (long m, long n){
long x;
long y;
while(m%n != 0){
x = n;
y = m%n;
m = x;
n = y;
}
return n;
}
long no_of_primes(long m,long n,long k){
long count=0,i,j;
int primes []=new int[(int)(n-m+2)];
if(m==1) primes[0] = 1;
for(i=2; i<=Math.sqrt(n); i++)
{
j = (m/i); j *= i;
if(j<m)
j+=i;
for(; j<=n; j+=i)
{
if(j!=i)
primes[(int)(j-m)] = 1;
}
}
for(i=0; i<=n-m; i++)
if(primes[(int)i]==0 && (i-1)%k==0)
count++;
return count;
}
}
class SegTree {
int n;
long t[];
long mod=(long)(1000000007);
SegTree(int n,long t[]){
this.n=n;
this.t=t;
build();
}
void build() { // build the tree
for (int i = n - 1; i > 0; --i)
t[i]=Math.max(t[i<<1],t[i<<1|1]);
}
void modify(int p, long value) { // set value at position p
for (t[p += n]=value; p > 1; p >>= 1) t[p>>1] = Math.max(t[p], t[p^1]);
}
long query(int l, int r) { // sum on interval [l, r)
long res=Integer.MIN_VALUE;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if ((l&1)!=0) res=Math.max(res,t[l++]);
if ((r&1)!=0) res=Math.max(res,t[--r]);
}
return res;
}
//
//
//
}
class FastScanner
{
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar;
private int snumChars;
private SpaceCharFilter filter;
public FastScanner(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class UF {
private int[] parent; // parent[i] = parent of i
private byte[] rank; // rank[i] = rank of subtree rooted at i (never more than 31)
private int count; // number of components
public UF(int N) {
if (N < 0) throw new IllegalArgumentException();
count = N;
parent = new int[N];
rank = new byte[N];
for (int i = 0; i < N; i++) {
parent[i] = i;
rank[i] = 0;
}
}
public int find(int p) {
if (p < 0 || p >= parent.length) throw new IndexOutOfBoundsException();
while (p != parent[p]) {
parent[p] = parent[parent[p]]; // path compression by halving
p = parent[p];
}
return p;
}
public int count() {
return count;
}
public boolean connected(int p, int q) {
return find(p) == find(q);
}
public boolean union(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ) return false;
// make root of smaller rank point to root of larger rank
if (rank[rootP] < rank[rootQ]) parent[rootP] = rootQ;
else if (rank[rootP] > rank[rootQ]) parent[rootQ] = rootP;
else {
parent[rootQ] = rootP;
rank[rootP]++;
}
count--;
return true;
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.util.Random;
import java.util.Scanner;
public class main {
public static void main (String[] args) throws java.lang.Exception
{
Scanner input = new Scanner(System.in);
String in = input.nextLine();
int n = Integer.parseInt(in);
in = input.nextLine();
String[] to = in.split("");
boolean[] d = new boolean[n];
for(int i = 0; i < n; i++){
if(Integer.parseInt(to[i]) > 0){
d[i] = true;;
} else {
d[i] = false;
}
}
System.out.println(solve(d));
}
private static int solve(boolean[] d) {
int toggles = 1;
int consecutives = 1;
int toAdd = 0;
for (int i = 0; i < d.length - 1; i++){
if(d[i] != d[i + 1]){
toggles++;
if(consecutives == 2 && i < d.length - 2){
if(d[i + 1] == d[i + 2]){
toAdd = Math.max(2, toAdd);
} else if(i < d.length - 3){
if(d[i + 2] == d[i + 3]){
toAdd = Math.max(2, toAdd);
}
}
}
consecutives = 1;
} else {
consecutives++;
if(consecutives >= 2){
toAdd++;
toAdd = Math.min(2, toAdd);
}
}
}
return toggles + toAdd;
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
string s;
cin >> n >> s;
int ans = 1;
for (int i = 1; i < n; i++)
if (s[i] != s[i - 1]) ans++;
if (ans == n)
ans = n;
else if (ans == n - 1)
ans = n;
else
ans += 2;
printf("%d\n", ans);
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const long long inf = 1e17 + 10;
const int N = 1e6 + 10;
const long long mod = 1e9 + 7;
const int base = 131;
const double pi = acos(-1);
map<string, int> ml;
map<int, int> vi;
long long b[N], c[N], num[N], a[N], n, m, k, x, y, z, vis[N];
long long ex, ey, cnt, ans, sum, flag, w;
vector<int> v[N];
map<long long, long long> mp;
map<string, int> can;
set<int> st;
long long dp[N][2];
priority_queue<long long, vector<long long>, greater<long long>> q;
char s[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
cin >> s + 1;
for (int i = 1; i <= n; i++) {
if (s[i] == '0') {
dp[i][0] = dp[i - 1][1] + 1;
dp[i][1] = dp[i - 1][1];
} else {
dp[i][1] = dp[i - 1][0] + 1;
dp[i][0] = dp[i - 1][0];
}
}
cout << min(n, max(dp[n][0] + 2, dp[n][1] + 2));
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, -1, 0, 1, -1, 0, 1};
int dp[100005][3][3];
int nxtz[100005], nxto[100005], n;
string s;
int dpcall(int id, int lst, int ok) {
if (id == n) return 0;
int &ret = dp[id][lst][ok];
if (~ret) return ret;
ret = 0;
if (ok == 2) {
if (s[id] == lst + '0') {
ret = dpcall(id + 1, lst, ok);
} else {
ret = 1 + dpcall(id + 1, s[id] - '0', ok);
}
} else if (ok == 0) {
if (lst == s[id] - '0') {
ret = dpcall(id + 1, lst, ok);
ret = max(ret, dpcall(id + 1, lst ^ 1, 1));
} else {
ret = 1 + dpcall(id + 1, s[id] - '0', ok);
ret = max(ret, 1 + dpcall(id + 1, (s[id] - '0' ^ 1), 1));
}
} else if (ok == 1) {
if (lst == s[id] - '0') {
ret = dpcall(id + 1, lst, ok);
ret = max(ret, dpcall(id + 1, lst ^ 1, 2));
} else {
ret = 1 + dpcall(id + 1, s[id] - '0', ok);
ret = max(ret, 1 + dpcall(id + 1, (s[id] - '0' ^ 1), 2));
}
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
memset((dp), -1, sizeof(dp));
cin >> n;
cin >> s;
int z = n, o = n;
for (int i = n - 1; i >= 0; i--) {
nxtz[i] = z;
nxto[i] = o;
if (s[i] == '1')
o = i;
else
z = i;
}
int ans = 0, res = 0;
for (int i = 0; i < n; i++) {
ans = max(dpcall(i, 2, 0), dpcall(i, 2, 1));
res = max(res, ans);
}
cout << res << endl;
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
char s[100009];
int n;
int main() {
while (scanf("%d", &n) == 1) {
scanf("%s", s);
int c1 = 0, c2 = 0;
for (int(i) = (0); (i) < (n - 1); ++(i))
if (s[i] != s[i + 1])
c1++;
else
c2++;
printf("%d\n", 1 + c1 + min(2, c2));
}
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.util.Scanner;
// NibNalin solution
public class JJ {
static long dp[][][];
static int[] arr;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
String s = in.nextLine();
arr = new int[s.length()];
int id = 0;
for(char c : s.toCharArray()) {
arr[id++] = Character.getNumericValue(c);
}
dp = new long[100_001][3][2];
for(int i=0; i<dp.length; i++)
for(int j=0; j<dp[i].length; j++)
for(int k=0; k < dp[i][j].length; k++) {
dp[i][j][k] = -1;
}
int ans = Math.max(res(1, 0, arr[0]), res(1, 1, arr[0] == 0 ? 1 : 0));
System.out.println(ans + 1);
}
private static int res(int i, int s, int prev) {
if(i == arr.length && (s == 2 || s==1))
return 0;
else if(i == arr.length) {
return -999999999;
}
else if(dp[i][s][prev] != -1)
return (int) dp[i][s][prev];
else {
int ans = 0;
if(s == 0) {
ans = res(i+1, 1, arr[i] == 1 ? 0 : 1) + (arr[i] == prev ? 1 : 0);
ans =Math.max(ans, res(i+1, 1, prev));
ans = Math.max(ans, res(i+1, 0, prev));
// int add = arr[i] != prev ? 1 : 0;
ans = Math.max(ans, res(i+1, 0, arr[i]) + (arr[i] != prev ? 1 : 0 ) );
}
else if(s == 1) {
ans = res(i+1, 2, arr[i]) + (arr[i] != prev ? 1 : 0);
ans = Math.max(ans, res(i+1, 2, prev));
ans = Math.max(ans, (arr[i] == prev ? 1 : 0) + res(i+1, 1, arr[i] == 0 ? 1 : 0 ) );
ans =Math.max(ans, res(i+1, 1, prev ) );
}
else {
ans = res(i+1, 2, arr[i]) + (arr[i] != prev ? 1 : 0);
ans = Math.max(ans, res(i+1, 2, prev));
}
return (int) (dp[i][s][prev] = ans);
}
}
} | JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | n = int(input())
s = input()
ans = 0
for i in range(1, n):
if s[i] != s[i-1]:
ans += 1
print(min(ans+3, n)) | PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.util.Scanner;
/**
* Created by tignatchenko on 15.10.2015.
*/
public class Z1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s = sc.next();
char[] c = s.toCharArray();
int k = 0;
int add = 0;
int sz = 0;
for(int i = 1; i<c.length; i++) {
k++;
if (c[i]!=c[i-1]) {
sz++;
if (k>1) {
add +=k;
}
k = 0;
}
}
sz++;
k++;
if (k>1) {
add += k;
}
if (add>2) {
sz+=2;
} else if (add > 1) {
sz+=1;
}
System.out.println(sz);
}
} | JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | N=int(input())
s=input()
count1=[]
x=s[0]
i=1
count=1
while i<N:
if s[i]==s[i-1]:
count+=1
else:
count1.append(count)
count=1
i+=1
count1.append(count)
if any(i>2 for i in count1):
print(len(count1)+2)
elif count1.count(2)>1:
print(len(count1)+2)
elif count1.count(2)==1:
print(len(count1)+1)
else:
print(len(count1))
| PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | n = int(input().strip())
a = input()
count_pairs = 0;
count_diff = 0;
pred = a[0]
for i in range(1,n):
if a[i-1]==a[i]:
count_pairs += 1
else:
count_diff += 1
count_pairs = min(count_pairs,2)
print(count_pairs+count_diff+1)
| PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class A {
static void solve() throws IOException {
int n = Integer.parseInt( in.readLine() );
char[] c = in.readLine().toCharArray();
int[][] r = new int[2][3];
for ( int i = 0; i < n; i ++ ) {
int[][] t = new int[2][3];
for ( int ps = 0; ps < 3; ps ++ ) {
for ( int s = ps; s <= ps + 1 && s < 3; s ++ ) {
char d = ( char ) ( c[i] ^ ( s & 1 ) );
for ( char pc = '0'; pc <= '1'; pc ++ ) {
t[d - '0'][s] = Math.max( t[d - '0'][s], r[pc - '0'][ps] + ( ( pc ^ d ) & 1 ) );
}
}
}
r = t;
}
int res = 0;
for ( int[] rr : r ) {
for ( int x : rr ) {
res = Math.max( res, x );
}
}
out.println( res );
}
static BufferedReader in;
// static StreamTokenizer in;
static PrintWriter out;
// static int nextInt() throws IOException {
// in.nextToken();
// return ( int ) in.nval;
// }
public static void main( String[] args ) throws IOException {
in = new BufferedReader( new InputStreamReader( System.in ) );
out = new PrintWriter( System.out );
solve();
out.close();
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
char s[1000001];
int main() {
int n;
scanf("%d", &n);
scanf("%s", s);
int bf1 = 0, bf2 = 0, ans = 0, len = 1;
for (int i = 1; i <= n; i++) {
if (s[i] != s[i - 1]) {
if (len >= 3)
bf2 = 1;
else if (len >= 2)
bf1++;
len = 1;
ans++;
} else
len++;
}
if (bf2 || bf1 >= 2)
printf("%d\n", ans + 2);
else if (bf1)
printf("%d\n", ans + 1);
else
printf("%d\n", ans);
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
double EPS = 1e-9;
int conversion(string p) {
int o;
o = atoi(p.c_str());
return o;
}
string toString(int h) {
stringstream ss;
ss << h;
return ss.str();
}
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
long long lcm(long long a, long long b) { return (a * (b / gcd(a, b))); }
long long toi(string p) {
long long x;
istringstream in(p);
in >> x;
return x;
}
long long square(long long n) { return n * n; }
long long fastexp(long long base, long long power) {
if (power == 0)
return 1LL;
else if (power % 2 == 0)
return square(fastexp(base, power / 2));
else
return base * (fastexp(base, power - 1));
}
bool is_prime(int n) {
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return 0;
return 1;
}
int logba(int a, int b) { return log(a) / log(b); }
int rd(double d) {
int x = (int)((double)d + 0.5);
return x;
}
int n;
string a;
int memo[100005][2][100][2];
bool v[100005][2][100][2];
int dp(int ind, int cam, int last, int puedo) {
if (ind == n) return memo[ind][cam][last][puedo];
if (v[ind][cam][last][puedo]) return memo[ind][cam][last][puedo];
v[ind][cam][last][puedo] = true;
int maxi = 0;
if (cam == 0) {
maxi = max(maxi, dp(ind + 1, 0, a[ind], puedo) + (a[ind] != last));
maxi = max(maxi, dp(ind + 1, 0, last, puedo));
if (puedo == 0)
maxi = max(maxi, dp(ind + 1, 1, (a[ind] == '0') ? '1' : '0', 1) +
(a[ind] == last));
} else if (cam == 1) {
maxi = max(maxi, dp(ind + 1, 1, (a[ind] == '0') ? '1' : '0', puedo) +
(a[ind] == last));
maxi = max(maxi, dp(ind + 1, 0, a[ind], puedo) + (a[ind] != last));
}
memo[ind][cam][last][puedo] = maxi;
return maxi;
}
int main() {
cin >> n >> a;
cout << dp(0, 0, 0, 0) << endl;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
void solve() {
int n;
std::cin >> n;
std::string s;
std::cin >> s;
int r = 0;
for (int i = 1; i < n; ++i) {
r += (s[i] != s[i - 1]);
}
r = std::min(r + 3, n);
std::cout << r;
}
int main() {
solve();
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const unsigned long long M = 998244353;
void solve() {
long long n;
cin >> n;
string s;
cin >> s;
long long res = 1;
long long ct = 1;
for (long long i = 1; i < n; i++) {
if (s[i - 1] == s[i])
ct++;
else {
res++;
ct = 1;
}
}
cout << min(res + 2, n);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
while (t--) {
solve();
}
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
string s;
cin >> s;
int cnt = 1;
vector<int> v;
for (int i = 1; i < n; i++)
if (s[i] == s[i - 1])
cnt++;
else
v.push_back(cnt), cnt = 1;
v.push_back(cnt);
int ans = v.size();
bool mark = false;
sort(v.begin(), v.end());
if (v.size() > 1 && v.back() > 1 && v[v.size() - 2] > 1)
ans += 2, mark = true;
if (!mark && v.back() > 2) ans += 2, mark = true;
if (!mark && v.back() > 1) ans++;
cout << ans << endl;
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long test, i, j, xy, flag = 0, n, u, count, d, o1 = 0, o2 = 0, s, e, l,
r, x, y, m, z, max1, x1, y1, k, x2, y2, z1, z2, sum,
min1;
string a;
cin >> n >> a;
i = 0;
count = 0;
x1 = 1;
while (i < n - 1 && a[i] != a[i + 1]) {
i++;
x1++;
}
if (i >= n) {
cout << n << "\n";
return 0;
}
x = i;
i = n - 1;
y1 = 1;
while (i >= 1 && a[i] != a[i - 1]) {
i--;
y1++;
}
y = i;
if (y + 1 == x) {
cout << n << "\n";
return 0;
}
string temp = to_string(a[x]);
count = 0;
for (i = x + 1; i < y; i++) {
if (temp.back() != a[i]) {
temp += a[i];
count++;
}
}
cout << min(n, count + x1 + y1) << "\n";
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
char prev = '-', last = '-';
int count = 0, res = 0, ans = 0, zero = 0, one = 0;
for (char x:s.next().toCharArray()) {
if (x==prev) count++;
else {
if (count>2) res = 2;
if (count>1 && prev=='0') zero++;
if (count>1 && prev=='1') one++;
count = 1;
prev = x; last = x;
ans++;
}
}
if (count>1 && last=='0') zero++;
if (count>1 && last=='1') one++;
if (zero>0 || one>0) res = Math.max(res, 1);
if (count>2) res = 2;
if (zero>1 || one>1 || (zero==1 && one==1)) res = 2;
ans+=res;
System.out.println(ans);
}
} | JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.util.*;
import java.io.*;
import java.math.*;
public class test{
public static void main(String[] args)
{
// int x,y;
FastReader scan=new FastReader();
OutputStream output=System.out;
PrintWriter out=new PrintWriter(output);
int n = scan.nextInt();
String s = scan.next();
int count = 1;
for (int i = 1; i < n; ++i) {
if (s.charAt(i) != s.charAt(i - 1))
++count;
}
out.println(Math.min(count + 2, n));
out.close();
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | n = int(input())
s = list(input())
# n = 86137
# s = '0'
# for i in range(n-2):
# s += '1' if s[-1] == '0' else '0'
# s += s[-1]
# s += s[-1]
ans = len([s[i] for i in range(len(s)-1) if s[i] != s[i+1]])+1
for i in range(n-1):
if s[i] == s[i+1]:
ans += 1
break
for j in range(n-1, 0, -1):
if j-1 == i:
break
elif s[j] == s[j-1]:
ans += 1
break
print(ans)
| PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 10;
long long cnt[N];
void solve() {
string s;
long long n, cc = 0;
cin >> n >> s;
for (long long i = 0; i < N; i++) cnt[i] = 0;
cc = 1, cnt[cc] = 1;
for (long long i = 1; i < n; i++) {
cc += (s[i] != s[i - 1]);
cnt[cc] += 1;
}
long long ans = cc, pp = 0;
for (long long i = 1; i < N; i++) {
if (cnt[i] >= 3LL) {
ans += 2;
cout << ans << "\n";
return;
}
}
for (long long i = 1; i < N; i++) {
if (cnt[i] == 2LL) pp += 1;
}
if (pp > 1) {
ans += 2;
cout << ans << "\n";
} else if (pp == 1) {
ans += 1;
cout << ans << "\n";
} else {
cout << ans << "\n";
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long tc = 1;
while (tc--) {
solve();
}
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const long long MX = 1e9;
const long long INF = 1e9;
void print_a(vector<int> v) {
if (v.size()) cout << v[0];
for (int i = 1; i < v.size(); ++i) cout << ' ' << v[i];
cout << '\n';
}
vector<vector<int> > init_vvi(int n, int m, int val) {
return vector<vector<int> >(n, vector<int>(m, val));
}
vector<vector<long long> > init_vvl(int n, int m, long long val) {
return vector<vector<long long> >(n, vector<long long>(m, val));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
string s;
cin >> s;
int dp[n + 1][2][3], d;
memset(dp, 0, sizeof(dp));
for (int i = 1; i < n + 1; ++i) {
d = s[i - 1] - '0';
for (int j = 0; j < 2; ++j) {
for (int f = 0; f < 3; ++f) {
if (f == 0) {
if (d == j)
dp[i][j][f] = dp[i - 1][j ^ 1][f] + 1;
else
dp[i][j][f] = dp[i - 1][j][f];
} else if (f == 1) {
if (d != j)
dp[i][j][f] = max(dp[i - 1][d][0], dp[i - 1][d][1]) + 1;
else
dp[i][j][f] = dp[i - 1][j][f];
} else {
if (d == j)
dp[i][j][f] = max(dp[i - 1][j ^ 1][1], dp[i - 1][j ^ 1][2]) + 1;
else
dp[i][j][f] = dp[i - 1][j][2];
}
}
}
}
int ans = 0;
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 3; ++j) ans = max(ans, dp[n][i][j]);
cout << ans << '\n';
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int inf = 1e9 + 10;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, c = 1;
string s;
cin >> n;
cin >> s;
for (int i = 1; i <= n - 1; i++) {
if (s[i] != s[i - 1]) c++;
}
cout << min(c + 2, n) << '\n';
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n, len, oo;
char s[100003];
int main() {
scanf("%d", &n);
scanf("%s", s);
len = 1;
oo = 0;
for (int i = 1; i < n; i++) {
if (s[i] == s[i - 1])
oo++;
else
len++;
}
printf("%d\n", len + min(oo, 2));
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | from math import *
from Queue import *
from sys import *
from random import *
n = int(raw_input())
s = raw_input()
b = []
m = 0
i, j = 0, 0
while i < n:
while (j+1 < n) and (s[j+1] == s[j]):
j += 1
b.append(j-i+1)
m = max(m, j-i+1)
i = j+1
j = i
if m == 1:
print(len(b))
exit(0)
if m > 2:
print(len(b)+2)
exit(0)
if sum(b) == len(b) + 1:
print(len(b)+1)
exit(0)
print(len(b)+2)
| PYTHON |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
auto thinking(const std::string& s) -> int64_t {
if (s.size() == 0) return 0;
int64_t count = 1;
int last = s[0];
for (auto it = std::begin(s) + 1; it != std::end(s); it++) {
if (*it != last) {
count++;
last = *it;
}
}
return std::min((int64_t)s.size(), count + 2);
}
auto main(int argc, char* argv[]) -> int {
size_t size;
std::cin >> size;
std::string s;
std::cin >> s;
std::cout << thinking(s) << std::endl;
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
char ch, ch2;
int n, mx, res, cnt, cnt2, t;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
cin >> ch2;
cnt = 1;
cnt2 = 0;
mx = t = 0;
res = 0;
for (int i = 2; i <= n; i++) {
cin >> ch;
if (ch == ch2) {
cnt++;
} else {
if (cnt >= 3) mx = 2;
if (cnt >= 2) cnt2++;
cnt = 1;
res++;
}
ch2 = ch;
}
if (cnt >= 3) mx = 2;
if (cnt >= 2) cnt2++;
res++;
if (cnt2 > 1)
t = 2;
else if (cnt2 == 1)
t = 1;
else
t = 0;
if (t > mx) mx = t;
res += mx;
cout << res << "\n";
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
inline long long getint() {
long long _x = 0, _tmp = 1;
char _tc = getchar();
while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
if (_tc == '-') _tc = getchar(), _tmp = -1;
while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
return _x * _tmp;
}
long long mypow(long long _a, long long _x, long long _mod) {
if (_x == 0) return 1ll;
long long _tmp = mypow(_a, _x / 2, _mod);
_tmp = (_tmp * _tmp) % _mod;
if (_x & 1) _tmp = (_tmp * _a) % _mod;
return _tmp;
}
inline long long add(long long _x, long long _y,
long long _mod = 1000000007ll) {
long long _ = _x + _y;
if (_ >= _mod) _ -= _mod;
return _;
}
inline long long sub(long long _x, long long _y,
long long _mod = 1000000007ll) {
long long _ = _x - _y;
if (_ < 0) _ += _mod;
return _;
}
inline long long mul(long long _x, long long _y,
long long _mod = 1000000007ll) {
long long _ = _x * _y;
if (_ >= _mod) _ %= _mod;
return _;
}
inline bool equal(double _x, double _y) {
return _x > _y - 1e-9 && _x < _y + 1e-9;
}
int __ = 1, cs;
int n;
char c[101010];
void build() {}
int ans;
int cal(int now) {
int tans = 0;
for (int i = 1; i <= n; i++) {
int tmp = c[i] - '0';
if (now == tmp) {
tans++;
now = 1 - now;
}
}
return tans;
}
void init() {
n = getint();
scanf("%s", c + 1);
ans = max(cal(0), cal(1));
}
void solve() {
int l = n + n, r = -n;
for (int i = 1; i < n; i++)
if (c[i] == c[i + 1]) {
l = i + 1;
break;
}
for (int i = n; i > 1; i--)
if (c[i] == c[i - 1]) {
r = i - 1;
break;
}
if (l <= r) {
printf("%d\n", ans + 2);
} else if (l == r + 1) {
printf("%d\n", ans + 1);
} else
printf("%d\n", ans);
}
int main() {
build();
while (__--) {
init();
solve();
}
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n;
char s[100005];
int a[100005];
int dp[100005][2][3];
int maxv[2][3];
int main() {
scanf("%d", &n);
scanf("%s", s + 1);
for (int i = 1; i <= n; ++i) a[i] = s[i] - '0';
int ans = 0;
for (int i = 1; i <= n; ++i) {
dp[i][a[i]][0] = 1;
dp[i][a[i]][0] = max(dp[i][a[i]][0], maxv[a[i] ^ 1][0] + 1);
dp[i][a[i]][1] = max(maxv[a[i]][0] + 1, maxv[a[i] ^ 1][1] + 1);
dp[i][a[i]][2] = max(maxv[a[i]][1] + 1, maxv[a[i] ^ 1][2] + 1);
maxv[a[i]][0] = max(maxv[a[i]][0], dp[i][a[i]][0]);
maxv[a[i]][1] = max(maxv[a[i]][1], dp[i][a[i]][1]);
maxv[a[i]][2] = max(maxv[a[i]][2], dp[i][a[i]][2]);
ans = max(ans, max(max(maxv[a[i]][0], maxv[a[i]][1]), maxv[a[i]][2]));
}
printf("%d\n", ans);
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 |
import java.io.*;
public class A603 {
public static void main(String[] args)throws IOException{
BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int size = Integer.parseInt(inp.readLine());
String[] s1 = inp.readLine().split("");
int[] given = new int[size];
for(int i=0;i<size;i++){
given[i] = Integer.parseInt(s1[i]);
}
int count0 = 0;
int count1 = 0;
boolean last0 = false;
boolean last1 = false;
boolean tri0 = false;
boolean tri1 = false;
for(int i=0;i<size;i++){
int a = given[i];
if(a==0){
if(!last0){
count0++;
last0 = true;
}
if(last1){
count1++;
last1 = false;
}
}else{
if(last0){
count0++;
last0 = false;
}
if(!last1){
count1++;
last1 = true;
}
}
}
System.out.println( Math.min(Math.max(count0,count1)+2,size));
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.util.Scanner;
public class AlternativeThinking {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
cin.nextLine();
String string = cin.nextLine();
int count = 1;
boolean isChange = false;
boolean canChage = true;
for(int i = 1;i < string.length();i ++){
if(isChange){
count++;
if((string.charAt(i) - '0') == (string.charAt(i-1) - '0')){
canChage = false;
isChange = false;
}
}else{
if((string.charAt(i) - '0') != (string.charAt(i-1) - '0')){
count++;
}else{
if(canChage){
isChange = true;
count++;
}
}
}
}
System.out.println(count);
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
string s;
int ted = 0, mx1, mx2, l = 0, n;
void change() {
if (mx1 <= ted) {
mx2 = mx1;
mx1 = ted;
} else if (mx2 <= ted)
mx2 = ted;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> s;
ted = 1;
mx1 = mx2 = 1;
for (int i = 1; i < n; i++) {
if (s[i] != s[i - 1]) {
change();
l++;
ted = 0;
}
ted++;
}
change();
if (mx1 > 2) {
cout << l + 3 << endl;
return 0;
}
if (mx1 == 2 && mx2 == 2) {
cout << l + 3 << endl;
return 0;
}
if (mx1 == 2) {
cout << l + 2 << endl;
return 0;
}
cout << l + 1 << endl;
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | a=int(input())
s=input()
ans=[]
count=1
for i in range(1,len(s)):
if(s[i]==s[i-1]):
count+=1
else:
ans.append(count)
count=1
ans.append(count)
if(len(ans)==1):
if(len(s)==1):
print(1)
if(len(s)==2):
print(2)
if(len(s)>=3):
print(3)
exit()
r=ans.count(1)
if(r==len(ans)):
print(len(ans))
elif(r<=len(ans)-2):
print(len(ans)+2)
else:
temp=max(ans)
if(temp==2):
print(len(ans)+1)
else:
print(len(ans)+2)
| PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | from sys import stdin,stdout
nmbr = lambda: int(stdin.readline())
lst = lambda: list(map(int,stdin.readline().split()))
for _ in range(1):#nmbr()):
n=nmbr()
a=[int(ch) for ch in input()]
n=len(a)
dp=[[[0 for _ in range(2)]for _ in range(3)]for _ in range(n)]
dp[0][0][a[0]]=1
dp[0][0][1^a[0]]=0
dp[0][1][a[0]]=1
dp[0][1][1^a[0]]=1
dp[0][2][a[0]]=1
dp[0][2][1^a[0]]=1
for i in range(1,n):
dp[i][0][a[i]] = max(1+dp[i-1][0][1^a[i]],dp[i-1][0][a[i]])
dp[i][0][1 ^ a[i]] = 0#dp[i-1][0][1^a[i]]
dp[i][1][a[i]] = max(dp[i-1][0][a[i]],1+dp[i-1][0][1^a[i]])#max(dp[i-1][1][a[i]],dp[i-1][0][a[i]],1+dp[i-1][0][1^a[i]])
dp[i][1][1 ^ a[i]] = max(dp[i-1][1][a[i]]+1,dp[i-1][0][a[i]]+1,dp[i-1][0][1^a[i]])
dp[i][2][a[i]] = max(dp[i-1][2][1^a[i]]+1,dp[i-1][1][1^a[i]])
dp[i][2][1 ^ a[i]] = dp[i-1][1][a[i]]+1
ans=0
# print(*dp,sep='\n')
for i in range(3):
for j in range(2):
ans=max(ans,dp[n-1][i][j])
print(ans) | PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 0x3c3c3c3c;
const ll INFL = 0x3c3c3c3c3c3c3c3c;
const int MAX_N = 1e5 + 9;
int n;
string s;
int dp[MAX_N][3][2];
int getAnswer(int curr, int type, int need) {
if (curr == n) {
return 0;
}
if (type >= 3) {
return 0;
}
int &res = dp[curr][type][need];
if (res != -1) {
return res;
}
int v = s[curr] - '0';
if (v == need) {
res = max(res, 1 + getAnswer(curr + 1, type, 1 - need));
} else {
res = max(res, getAnswer(curr + 1, type, need));
res = max(res, getAnswer(curr, type + 1, 1 - need));
}
return res;
}
int main() {
memset(dp, -1, sizeof(dp));
cin >> n >> s;
printf("%d", max(getAnswer(0, 0, 0), getAnswer(0, 0, 1)));
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | n = int(input())
s = input()
arr = list(map(int, s))
cnt = 0
pcnt = 0
for i in range(n - 1):
cnt += arr[i] != arr[i + 1]
pcnt += arr[i] == arr[i + 1]
cnt += min(pcnt, 2)
print(cnt + 1) | PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
char S[100007];
int main() {
int i, n, res, m;
char l;
m = 0;
scanf("%d", &n);
scanf("%s", S);
res = 1;
l = S[0];
for (i = 1; i < n; i++) {
if (l != S[i]) {
res++;
} else {
if (i == 2 || i == n - 2) {
m++;
}
}
l = S[i];
}
if (res == n) {
printf("%d", n);
} else {
if (res == n - 1 && n != 2 && n != 3) {
printf("%d", n);
} else {
if (n == 2 || n == 3) {
printf("%d", n);
} else
printf("%d", res + 2);
}
}
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | n = int(input())
s = input()
repeat = 0
count = 0
for i in range (len(s)-1):
if s[i] == s[i+1]:
repeat += 1
else:
count += 1
count += 1
if repeat == 1:
count += 1
if repeat >= 2:
count += 2
print(count)
| PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n, cnt, f, max;
char t, s[100009], c[100009];
int main() {
cin >> n >> s;
if (n == 2) {
cout << 2;
return 0;
}
t = s[0];
++c[cnt];
for (int i = 1; i < n; i++) {
if (t == s[i])
++c[cnt], f++;
else
++c[++cnt];
t = s[i];
}
if (f == 1) {
cout << cnt + 2;
return 0;
}
if (f > 1)
cout << cnt + 3;
else
cout << cnt + 1;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
long long int M = 1e9 + 7;
long long int n, k, m;
long long int z, q, u, a1, l, r, ax, cx, ay, by, cy, ql, qr, d, x, y;
vector<long long int> v[400000];
long long int vis[5000005];
long long int cd[5000005];
long long int a[400000];
unordered_map<long long int, long long int> ml, dp;
void dfs(long long int i, long long int cnt) {}
long long int me(long long int a, long long int b, long long int M) {
if (b == 0)
return 1;
else if (b % 2 == 0)
return me((a * a) % M, b / 2, M);
else
return (a % M * me((a * a) % M, (b - 1) / 2, M) % M) % M;
}
long long int mI(long long int a, long long int m) { return me(a, m - 2, m); }
long long int da[400000];
struct edge {
long long int a, x, y;
} e[400000];
bool cmp(edge ax, edge bx) {
if (ax.a == bx.a) {
if (ax.x == bx.x) return ax.y < bx.y;
return ax.x < bx.x;
}
return ax.a > bx.a;
}
unordered_map<long long int, long long int> fd, fg, w, fh;
pair<long long int, long long int> p[1000006];
int main() {
cin >> n;
string s;
cin >> s;
long long int d, d1;
d = d1 = -1;
for (int j = 0; j < s.length(); j++) {
if (s[j] == s[j - 1]) {
d = j;
break;
}
}
for (int j = n - 1; j >= 0; j--) {
if (s[j] == s[j - 1]) {
d1 = j;
break;
}
}
for (int j = 0; j < n; j++) {
if (s[j] == s[j - 1])
dp[j] = dp[j - 1];
else
dp[j] = dp[j - 1] + 1;
}
if (d != d1) {
dp[n - 1] += 2;
} else if (d >= 0)
dp[n - 1] += 1;
cout << dp[n - 1];
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
int take() {
int n;
scanf("%d", &n);
return n;
}
double ttake() {
double n;
scanf("%lf", &n);
return n;
}
long long takes() {
long long n;
scanf("%lld", &n);
return n;
}
int cas;
using namespace std;
bool approximatelyEqual(float a, float b, float epsilon) {
return fabs(a - b) <= ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}
bool essentiallyEqual(float a, float b, float epsilon) {
return fabs(a - b) <= ((fabs(a) > fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}
bool definitelyGreaterThan(float a, float b, float epsilon) {
return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}
bool definitelyLessThan(float a, float b, float epsilon) {
return (b - a) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}
int main() {
int tc = 1;
while (tc--) {
int n = take(), id = 0, len1 = 0, len2 = 0;
string s;
cin >> s;
for (int i = 0; i <= n - 1; i += 1) {
if (id == (s[i] - 48)) len1++, id ^= 1;
}
id = 1;
for (int i = 0; i <= n - 1; i += 1) {
if (id == (s[i] - 48)) len2++, id ^= 1;
}
int ans = max(len1, len2);
cout << ans + min(2, n - ans) << "\n";
}
{ return 0; };
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | from sys import stdin,stdout
nmbr = lambda: int(stdin.readline())
lst = lambda: list(map(int,stdin.readline().split()))
for _ in range(1):
n=nmbr()
a=[int(ch) for ch in input()]
n=len(a)
dp=[[[0 for _ in range(2)]for _ in range(3)]for _ in range(n)]
dp[0][0][a[0]]=1
dp[0][0][1^a[0]]=0
dp[0][1][a[0]]=1
dp[0][1][1^a[0]]=1
dp[0][2][a[0]]=1
dp[0][2][1^a[0]]=1
#dp[pos][state][bit] = LAS ending at bit(0/1) till posth position
for i in range(1,n):
dp[i][0][a[i]] = max(1+dp[i-1][0][1^a[i]],dp[i-1][0][a[i]])
dp[i][0][1 ^ a[i]] = 0
dp[i][1][a[i]] = max(dp[i-1][0][a[i]],1+dp[i-1][0][1^a[i]])
dp[i][1][1 ^ a[i]] = max(dp[i-1][1][a[i]]+1,dp[i-1][0][1^a[i]])
dp[i][2][a[i]] =dp[i-1][2][1^a[i]]+1
dp[i][2][1 ^ a[i]] = dp[i-1][1][a[i]]+1
ans=max(dp[n-1][2][0],dp[n-1][2][1])
print(ans) | PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 |
import java.util.*;
import java.io.*;
/**
* Made by egor https://github.com/chermehdi/egor.
*
* @author Azuz
*
*/
public class Main {
int[][][] dp;
char[] arr;
int n;
void solve(Scanner in, PrintWriter out) {
int n = in.nextInt();
char[] arr = in.next().toCharArray();
char last = arr[0];
int cnt = 1;
for (int i = 1; i < n; ++i) {
if (arr[i] != last) {
last = arr[i];
++cnt;
}
}
out.println(Math.min(n, cnt + 2));
}
public int solve(int i, int state, int last) {
if (i == n) {
if (state >= 2) {
return 0;
} else {
return -n;
}
}
if (dp[i][state][last] > Integer.MIN_VALUE) {
return dp[i][state][last];
}
int ret = Integer.MIN_VALUE;
int ord = o(arr[i]);
if (state == 0) {
ret = Math.max(ret, solve(i + 1, state, ord) + (ord != last ? 1 : 0));
ret = Math.max(ret, solve(i + 1, state + 1, (ord ^ 1)) + (ord == last ? 1 : 0));
} else if (state == 1) {
ret = Math.max(ret, solve(i + 1, state, ord ^ 1) + (ord == last ? 1 : 0));
ret = Math.max(ret, solve(i + 1, state + 1, ord) + (ord != last ? 1 : 0));
} else {
ret = Math.max(ret, solve(i + 1, state, ord) + (ord != last ? 1 : 0));
}
return dp[i][state][last] = ret;
}
public int o(char c) {
return c - '0';
}
public static void main(String[] args) {
try(Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out)) {
new Main().solve(in, out);
}
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.util.*;
import java.io.*;
public class File {
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int n = sc.nextInt();
String s = sc.next();
int length = 1;
int doubles = 0;
for (int i = 0; i + 1 < n; i++) {
if (s.charAt(i) != s.charAt(i+1)) {
length++;
}
else {
doubles++;
}
}
int res = length + Math.min(2, doubles);
out.println(res);
out.close();
}
} | JAVA |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.