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 | #include <bits/stdc++.h>
int main() {
std::ios_base::sync_with_stdio(false);
int N, sol = 1;
std::string S;
std::cin >> N >> 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 | #include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 1, INF = 1e17, MOD = 1e9 + 7;
long long n, ans, cnt, t;
string s;
vector<long long> v;
long long ch(char a) { return (long long)a - 48; }
int32_t main() {
ios_base::sync_with_stdio(0);
cin >> n >> s;
if (s[0] == '0')
ans++, t++;
else
ans++;
++cnt;
for (long long i = 1; i < n; ++i) {
if (ch(s[i]) == t) {
t += 1;
t %= 2;
ans++;
}
if (s[i] == s[i - 1]) {
cnt++;
} else {
v.push_back(cnt);
cnt = 1;
}
}
v.push_back(cnt);
sort(v.begin(), v.end());
if (v.back() >= 3) {
cout << ans + 2 << endl;
} else {
if (v.back() >= 2) {
v.pop_back();
if (!v.size()) return cout << ans + 1 << endl, 0;
if (v.back() >= 2) {
cout << ans + 2 << endl;
} else {
cout << ans + 1 << endl;
}
} else {
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 | n=int(input())
s=[int(x) for x in list(input().strip())]
an=[s[0]]
c=0
cq=0
p=False
q=False
for i in range(1,n):
if s[i]^s[i-1]:
an.append(s[i])
if p==True:
if (i+1 )<n:
if s[i+1]==s[i]:
c+=1
p=False
else:
if p:
c+=1
p=True
if True:
print(min(n,len(an)+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;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n;
cin >> n;
char c[n + 1];
long long i, j;
for (i = 1; i <= n; i++) cin >> c[i];
long long a[n + 1], b[n + 1];
a[1] = 1;
for (i = 2; i <= n; i++) {
if (c[i] != c[i - 1])
a[i] = 1 + a[i - 1];
else
a[i] = a[i - 1];
}
long long ans = min(n, a[n] + 2);
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 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
string s;
cin >> s;
vector<long long int> ind;
long long int t = 0;
ind.push_back(0);
for (long long int i = 1; i < n; i++) {
if (s[i] != s[ind[t]]) {
t++;
ind.push_back(i);
}
}
long long int f = 0;
long long int last = 0;
for (long long int i = 1; i <= t; i++) {
if (ind[i] - ind[i - 1] - 1 >= 2) {
f = 1;
break;
}
if (ind[i] - ind[i - 1] - 1 >= 1) {
last++;
}
}
if (n - ind[t] - 1 >= 2) f = 1;
if (n - ind[t] - 1 >= 1) last++;
if (last >= 2) f = 1;
if (f == 1) {
cout << ind.size() + 2 << "\n";
} else if (last >= 1) {
cout << ind.size() + 1 << "\n";
} else {
cout << ind.size() << "\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.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class AlternativeThinking {
static int n;
static char[] s;
static int DP[][][] = new int[3][3][100003];
static int getMax(int curr, int alter, int prev) {
if (curr >= n)
return 0;
if (DP[alter][prev][curr] != -1)
return DP[alter][prev][curr];
int ans = getMax(curr + 1, alter == 0 ? 0 : 2, prev);
if (alter > 0) {
if (alter == 1) {
// can swap curr
if (s[curr] == '1') {
if (prev == 0) {
ans = max(ans, 1 + getMax(curr + 1, 2, 1));
} else {
ans = max(ans, 1 + getMax(curr + 1, 1, 0)); // swap me
}
} else {
if (prev == 0) {
ans = max(ans, 1 + getMax(curr + 1, 1, 1));
} else {
ans = max(ans, 1 + getMax(curr + 1, 2, 0)); // swap
// 10111101 // me
}
}
} else {
// can't swap curr
if (s[curr] == '1') {
if (prev == 0) {
ans = max(ans, 1 + getMax(curr + 1, alter, 1));
}
} else {
if (prev == 1) {
ans = max(ans, 1 + getMax(curr + 1, alter, 0));
}
}
}
} else {
if (prev == 2) {
if (s[curr] == '1') {
ans = max(ans, 1 + getMax(curr + 1, alter, 1));
ans = max(ans, 1 + getMax(curr + 1, 1, 0));
} else {
ans = max(ans, 1 + getMax(curr + 1, alter, 0));
ans = max(ans, 1 + getMax(curr + 1, 1, 1));
}
} else {
if (s[curr] == '1') {
if (prev == 0) {
ans = max(ans, 1 + getMax(curr + 1, alter, 1));
} else {
ans = max(ans, 1 + getMax(curr + 1, 1, 0));
}
} else {
if (prev == 0) {
ans = max(ans, 1 + getMax(curr + 1, 1, 1));
} else {
ans = max(ans, 1 + getMax(curr + 1, alter, 0));
}
}
}
}
return DP[alter][prev][curr] = ans;
}
private static int max(int ans, int i) {
return Math.max(ans, i);
}
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String[] l = bf.readLine().split(" ");
n = Integer.parseInt(l[0]);
s = bf.readLine().toCharArray();
for (int i = 0; i < DP.length; i++) {
for (int j = 0; j < DP[i].length; j++) {
Arrays.fill(DP[i][j], -1);
}
}
System.out.println(getMax(0, 0, 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 | #!/usr/bin/python
import os
import sys
import itertools
def solve(f):
n = f.read_int()
s = f.read_str()
so = s[0]
c = 1
cflip = 0
cl = []
for si in s[1:]:
if so == si:
c += 1
else:
cflip += 1
cl.append(c)
c = 1
so = si
cl.append(c)
if max(cl) >= 3 or len(filter(lambda x: x == 2, cl)) >= 2:
return cflip+3
elif max(cl) == 2:
return cflip+2
else:
return cflip+1
class Reader(object):
def __init__(self, filename=None):
if filename: self.f = open(filename)
else: self.f = sys.stdin
def read_int(self):
return int(self.f.readline().strip())
def read_float(self):
return float(self.f.readline().strip())
def read_long(self):
return long(self.f.readline().strip())
def read_str(self):
return self.f.readline().strip()
def read_int_list(self):
return [int(item) for item in self.f.readline().split()]
def read_float_list(self):
return [float(item) for item in self.f.readline().split()]
def read_long_list(self):
return [long(item) for item in self.f.readline().split()]
def read_str_list(self):
return self.f.readline().split()
if __name__ == '__main__':
filename = sys.argv[1] if len(sys.argv)>1 else None
f = Reader(filename)
print solve(f)
| 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 | input()
v=[0]*8
for x in raw_input():
t=v[:]
d=int(x)
for i in (0,1):
for j in (0,1,2):
y=v[j*2+i]
z=1-d if j%2 else d
if i==z:
u=(j+1)*2+(1-z)
t[u]=max(t[u],y+1)
u=j*2+z
t[u]=max(t[u],y)
else:
u=j*2+z
t[u]=max(t[u],y+1)
v=t
print max(v[:6])
| 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>
using namespace std;
const int MOD = 1e9 + 7;
int main() {
int n;
scanf("%d", &n);
string s;
cin >> s;
int len = 1;
for (int i = 1; i < s.length(); i++) {
if (s[i] == s[i - 1]) {
continue;
}
len++;
}
cout << min(len + 2, n) << 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>
using namespace std;
template <typename... T>
void read(T&... args) {
((cin >> args), ...);
}
template <typename... T>
void write(T&&... args) {
((cout << args << " "), ...);
}
template <typename... T>
void writen(T&&... args) {
((cout << args << "\n"), ...);
}
long long bin(long long a, long long b, long long mod) {
if (b == 0) return 1;
if (b % 2 == 0) return bin((a * a) % mod, b / 2, mod) % mod;
return ((a % mod) * (bin((a * a) % mod, b / 2, mod) % mod)) % mod;
}
void solve() {
int n;
read(n);
char ar[n + 1];
for (int i = 1; i < n + 1; i++) read(ar[i]);
bool is0even = true, is0odd = true, is1even = true, is1odd = true;
bool is0even1 = true, is0odd1 = true, is1even1 = true, is1odd1 = true;
int size0 = 0, size1 = 0;
for (int i = 1; i < n + 1; i++) {
if (i % 2 == 0) {
if (ar[i] != '0') {
is0even = false;
} else {
is1even = false;
}
} else {
if (ar[i] != '0') {
is0odd = false;
} else {
is1odd = false;
}
}
if (i % 2 == 0 && i != 1 && i != n) {
if (ar[i] != '0') {
is0even1 = false;
} else {
is1even1 = false;
}
}
if (i % 2 == 1 && i != 1 && i != n) {
if (ar[i] != '0') {
is0odd1 = false;
} else {
is1odd1 = false;
}
}
if (ar[i] == '1') {
size1 = 1 + size0;
} else {
size0 = 1 + size1;
}
}
int ans = max(size0, size1);
cout << min(n, ans + 2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
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 | import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class AlternativeThinking {
InputStream is;
PrintWriter pw;
String INPUT = "";
long L_INF = (1L << 60L);
void solve() {
int n=ni(), m;
char[] s = ns(n);
StringBuilder com = new StringBuilder();
com.append(s[0]);
char last = s[0];
for (int i = 1; i < s.length; i++) {
if(last!=s[i]){
last = s[i];
com.append(last);
}
}
pw.println( Math.min(n,com.length()+2));
}
void run() throws Exception {
// is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
is = System.in;
pw = new PrintWriter(System.out);
long s = System.currentTimeMillis();
// int t = ni();
// while (t-- > 0)
solve();
pw.flush();
tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new AlternativeThinking().run();
}
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1) throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0) return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b)) ;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++) map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = ni();
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) {
if (!oj) System.out.println(Arrays.deepToString(o));
}
}
| 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 |
if __name__ == '__main__':
n = input()
s = raw_input()
doubles = 0
triples = 0
for i in range(n - 1):
if s[i] == s[i + 1]:
doubles += 1
for i in range(n - 2):
if s[i] == s[i + 1] and s[i + 1] == s[i + 2]:
triples += 1
seq_len = 1
for i in range(n - 1):
if s[i] != s[i+1]:
seq_len += 1
if doubles >= 2 or triples >= 1:
seq_len += 2
elif doubles == 1:
seq_len += 1
print seq_len
| 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>
using namespace std;
int n, dp[100001][2];
char s[1000001];
int main() {
int i, j;
scanf("%d", &n);
scanf("%s", s);
if (s[0] == '0')
dp[0][0] = 1;
else
dp[0][1] = 1;
for (i = 1; i < n; i++) {
if (s[i] == '1')
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + 1);
else
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + 1);
}
cout << min(max(dp[n - 1][0], dp[n - 1][1]) + 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 | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
public class C {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int n = sc.nextInt();
String line = sc.nextLine();
if (n == 2) {
out.println(2);
out.close();
return;
}
if (n == 3) {
out.println(3);
out.close();
return;
}
int alt = 1;
char cur = line.charAt(0);
int maxSame = 1;
int curSame = 1;
int counter = 0;
for (int i = 1; i < n; ++i) {
if (line.charAt(i) == cur) {
curSame += 1;
} else {
if (curSame >= 2) {
counter += 1;
}
curSame = 1;
alt += 1;
}
maxSame = Math.max(curSame, maxSame);
cur = line.charAt(i);
}
if (curSame >= 2) {
counter += 1;
}
if (maxSame >= 3 || counter >= 2) {
alt += 2;
} else if (counter == 1) {
alt += 1;
}
out.println(alt);
out.close();
}
public static PrintWriter out;
static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
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 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;
char s[maxn];
int main() {
int n;
cin >> n;
scanf("%s", s + 1);
int res = 1;
for (int i = 2; i <= n; ++i) res += s[i] != s[i - 1];
int ans = res;
int cnt2 = 0, cnt3 = 0;
for (int i = 1, j = 1; i <= n; i = j) {
while (j <= n && s[i] == s[j]) ++j;
if (j - i == 2) cnt2++;
if (j - i > 2) cnt3++;
}
if (cnt2) ans = max(ans, res + 1);
if (cnt3) ans = max(ans, res + 2);
if (cnt2 > 1) ans = max(ans, res + 2);
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.*;
import java.util.*;
public class Solution {
static Scanner sc=new Scanner(System.in);
static PrintWriter out=new PrintWriter(System.out);
//Main
public static void main(String args[]) {
int test=1;
//test=sc.nextInt();
while(test-->0) {
int n=sc.nextInt();sc.nextLine();
char s[]=sc.nextLine().toCharArray();
int ans=1;
for(int i=1;i<n;i++) if(s[i]!=s[i-1]) ans++;
out.println(Math.min(ans+2, n));
}
out.flush();
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 |
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
import java.math.BigInteger;
public class Main {
static int [][][] dp;
static int [] A;
static int n,INF = (int)1e9;
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//(new FileReader("input.in"));
StringBuilder out = new StringBuilder();
StringTokenizer tk;
n = parseInt(in.readLine());
int i;
A = new int[n];
String s = in.readLine();
for(i=0; i<n; i++)
A[i] = (s.charAt(i)-'0');
dp = new int[n][3][3];
for(i=0; i<n; i++)
for(int j=0; j<3; j++)
Arrays.fill(dp[i][j], -1);
System.out.println(solve(0, 0, 2));
}
public static int solve(int i,int s,int pre) {
if(i == n)
return 0;
if(dp[i][s][pre]!=-1)
return dp[i][s][pre];
int c1 = -1, c2 = -1, c3 = solve(i+1, s,pre);
if(s == 0) {
if(A[i]!=pre) c1 = solve(i+1, 0, A[i]) + 1;
if(A[i]==pre) c2 = solve(i+1, 1, A[i]^1) + 1;
} else if(s == 1) {
if(A[i]!=pre) c1 = solve(i+1, 2, A[i]) + 1;
if(A[i]==pre) c2 = solve(i+1, 1, A[i]^1) + 1;
} else {
if(A[i]!=pre) c1 = solve(i+1, 2, A[i]) + 1;
}
return dp[i][s][pre] = max(c1, max(c2, c3));
}
}
| 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.*;
/*
*/
public class B {
static FastReader sc=null;
public static void main(String[] args) {
sc=new FastReader();
int n=sc.nextInt();
char line[]=sc.next().toCharArray();
int dp[][]=new int[n+1][3];
int recentOne=0,recentZero=0;
for(int i=1;i<=n;i++) {
if(line[i-1]=='1') {
dp[i][0]=dp[recentZero][0]+1;
dp[i][1]=Math.max(dp[recentOne][0],dp[recentZero][1])+1;
dp[i][2]=Math.max(dp[recentZero][2], dp[recentOne][1])+1;
recentOne=i;
}
else {
dp[i][0]=dp[recentOne][0]+1;
dp[i][1]=Math.max(dp[recentZero][0],dp[recentOne][1])+1;
dp[i][2]=Math.max(dp[recentOne][2], dp[recentZero][1])+1;
recentZero=i;
}
}
int ans=0;
for(int i=0;i<=n;i++) {
for(int j=0;j<3;j++)ans=Math.max(ans,dp[i][j]);
}
System.out.println(ans);
}
static int[] ruffleSort(int a[]) {
ArrayList<Integer> al=new ArrayList<>();
for(int i:a)al.add(i);
Collections.sort(al);
for(int i=0;i<a.length;i++)a[i]=al.get(i);
return a;
}
static void print(int a[]) {
for(int e:a) {
System.out.print(e+" ");
}
System.out.println();
}
static class FastReader{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while(!st.hasMoreTokens())
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());
}
int[] readArray(int n) {
int a[]=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
return a;
}
}
}
| 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;
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
long long A[1000005];
long long B[1000005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long tst = 1;
for (long long tt = 1; tt <= tst; tt++) {
long long n;
cin >> n;
string s;
cin >> s;
(s).resize(unique(s.begin(), s.end()) - (s).begin());
cout << min(((long long)(s).size()) + 2, n) << 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 n;
int blocos;
int duplas;
char s[100010];
int main() {
scanf("%d", &n);
blocos = 1;
scanf("%s", &s);
for (int i = 1; i <= n - 1; i++) {
if (s[i] != s[i - 1]) {
blocos++;
} else
duplas++;
}
if (duplas > 2) duplas = 2;
printf("%d\n", blocos + duplas);
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 = input()
s = raw_input()
cnt = 0
pre = ''
for i in s:
if i != pre:
cnt += 1
pre = i
if "111" in s or "000" in s:
cnt += 2
else:
flag = 0
i = 0
while i < n and flag < 2:
if i + 1 < n and s[i] == s[i + 1]:
flag += 1
i = i + 1
i += 1
cnt += flag
print cnt
| 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.*;
import java.util.Scanner;
import java.util.jar.Pack200;
public class Main {
public static void main (String[] args) throws IOException {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
String line = in.nextLine();
int count = 1;
int count2= 0 ;
char prev = line.charAt(0);
for(int i =1; i<line.length(); i++){
char c = line.charAt(i);
if(c!=prev){
count++;
}
else{
count2++;
}
prev = c;
}
if(count2>2){
count2=2;
}
System.out.println(count2+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 |
import java.util.*;
import java.io.*;
/**
*
* @author umang
*/
public class A603 {
public static int mod = (int) (1e9+7);
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int n=in.nextInt();
String s=in.readString();
int ans=1;
char prev=s.charAt(0);
for(int i=1;i<n;i++){
char c=s.charAt(i);
if(prev!=c){
ans++;
prev=c;
}
}
out.println(min(ans+2,n));
out.close();
}
static class Pair{
int x;
int y;
int i;
Pair(int x,int y,int i){
this.i=i;
this.x=x;
this.y=y;
}
}
public static class CompareTable implements Comparator{
public int compare(Object o1,Object o2){
Pair p1 = (Pair) o1;
Pair p2 = (Pair) o2;
if(p1.x>p2.x)
return -1;
else if(p1.x<p2.x)
return 1;
else{
if(p1.y<p2.y)
return -1;
else if(p1.y>p2.y)
return 1;
else
return 0;
}
}
}
public static boolean isPal(String s){
for(int i=0, j=s.length()-1;i<=j;i++,j--){
if(s.charAt(i)!=s.charAt(j)) return false;
}
return true;
}
public static String rev(String s){
StringBuilder sb=new StringBuilder(s);
sb.reverse();
return sb.toString();
}
public static long gcd(long x,long y){
if(x%y==0)
return y;
else
return gcd(y,x%y);
}
public static int gcd(int x,int y){
if(x%y==0)
return y;
else
return gcd(y,x%y);
}
public static long gcdExtended(long a,long b,long[] x){
if(a==0){
x[0]=0;
x[1]=1;
return b;
}
long[] y=new long[2];
long gcd=gcdExtended(b%a, a, y);
x[0]=y[1]-(b/a)*y[0];
x[1]=y[0];
return gcd;
}
public static int abs(int a,int b){
return (int)Math.abs(a-b);
}
public static long abs(long a,long b){
return (long)Math.abs(a-b);
}
public static int max(int a,int b){
if(a>b)
return a;
else
return b;
}
public static int min(int a,int b){
if(a>b)
return b;
else
return a;
}
public static long max(long a,long b){
if(a>b)
return a;
else
return b;
}
public static long min(long a,long b){
if(a>b)
return b;
else
return a;
}
public static long pow(long n,long p,long m){
long result = 1;
if(p==0)
return 1;
if (p==1)
return n;
while(p!=0)
{
if(p%2==1)
result *= n;
if(result>=m)
result%=m;
p >>=1;
n*=n;
if(n>=m)
n%=m;
}
return result;
}
public static long pow(long n,long p){
long result = 1;
if(p==0)
return 1;
if (p==1)
return n;
while(p!=0)
{
if(p%2==1)
result *= n;
p >>=1;
n*=n;
}
return result;
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(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 int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public long[] nextLongArray(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
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 String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(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;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| 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>
const int mod = (int)1e9 + 7;
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
string s;
cin >> n >> s;
char c = s[0];
int ans = 1;
for (int i = 1; i < n; i++) {
if (s[i] != c) {
c = s[i];
ans++;
}
}
cout << min(n, ans + 2) << "\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 main() {
int n, sum = 1, d = 0, y = 0;
cin >> n;
string a;
cin >> a;
for (int i = 0; i <= n - 2; i++) {
if (a[i] != a[i + 1]) sum++;
}
if (sum == n || sum == (n - 1))
cout << n;
else {
for (int i = 1; i <= n - 3; i++) {
if (a[i] == a[i + 1]) {
sum += 2;
cout << sum;
y = 1;
break;
}
}
if (y != 1) {
if (a[0] == a[1]) sum++;
if (a[n - 2] == a[n - 1]) sum++;
cout << sum;
}
}
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 inf_int = 2e9;
long long inf_ll = 2e18;
const double pi = 3.1415926535898;
template <typename T>
void prin(vector<T>& a) {
for (int i = 0; i < a.size(); i++) {
cout << a[i] << " ";
}
}
template <typename T>
void prin_new_line(vector<T>& a) {
for (int i = 0; i < a.size(); i++) {
cout << a[i] << "\n";
}
}
int sum_vec(vector<int>& a) {
int s = 0;
for (int i = 0; i < a.size(); i++) {
s += a[i];
}
return s;
}
template <typename T>
T max(vector<T>& a) {
T ans = a[0];
for (int i = 1; i < a.size(); i++) {
ans = max(ans, a[i]);
}
return ans;
}
template <typename T>
T min(vector<T>& a) {
T ans = a[0];
for (int i = 1; i < a.size(); i++) {
ans = min(ans, a[i]);
}
return ans;
}
int min(int a, int b, int c) { return min(a, min(b, c)); }
long long min(long long a, long long b, long long c) {
return min(a, min(b, c));
}
int max(int a, int b, int c) { return max(a, max(b, c)); }
long long max(long long a, long long b, long long c) {
return max(a, max(b, c));
}
double s_triangle(double x1, double y1, double x2, double y2, double x3,
double y3) {
return (((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)) / 2);
}
void solve() {
int n;
cin >> n;
string a;
cin >> a;
int ans = 1;
for (int i = 1; i < n; i++) {
if (a[i] != a[i - 1]) ans++;
}
cout << min(ans + 2, n);
}
int main() {
if (!0) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int 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;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
using ll = long long int;
using ull = long long unsigned;
using lf = long double;
void solve() {
int n;
cin >> n;
string s;
cin >> s;
vector<int> partial(n, 0);
partial.front() = 1;
bool lastZero = s.front() == '0';
for (int i = 1; i < n; ++i) {
if (lastZero && (s[i] == '1')) {
lastZero = !lastZero;
partial[i] = partial[i - 1] + 1;
} else if (!lastZero && (s[i] == '0')) {
lastZero = !lastZero;
partial[i] = partial[i - 1] + 1;
} else {
partial[i] = partial[i - 1];
}
}
int result = partial.back();
int addition = 0;
for (int i = 0; i < n; ++i) {
if (i > 0 && partial[i - 1] == partial[i]) {
addition = min(addition + 1, 2);
continue;
}
}
cout << result + addition << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(20);
cout << fixed;
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 | import java.util.*;
import java.io.*;
public class A
{
public static void main(String ar[]) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
char c[]=br.readLine().toCharArray();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=c[i]-48;
int d[][]=new int[n][3];
d[0][0]=1; d[0][1]=1; d[0][2]=1;
for(int i=1;i<n;i++)
{
d[i][0]=d[i-1][0]+check(a[i-1],a[i]);
d[i][1]=Math.max(d[i-1][0]+check(1-a[i],a[i-1]),d[i-1][1]+check(a[i],a[i-1]));
d[i][2]=Math.max(d[i-1][1]+check(1-a[i-1],a[i]),d[i-1][2]+check(a[i],a[i-1]));
}
System.out.println(Math.max(d[n-1][0],Math.max(d[n-1][1],d[n-1][2])));
}
public static int check(int x,int y)
{
if(x!=y)
return 1;
return 0;
}
} | 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 | #!/usr/bin/env python3
# 604C_thinking.py - Codeforces.com/problemset/problem/604/C by Sergey 2015
import unittest
import sys
###############################################################################
# Thinking Class (Main Program)
###############################################################################
class Thinking:
""" Thinking representation """
def __init__(self, test_inputs=None):
""" Default constructor """
it = iter(test_inputs.split("\n")) if test_inputs else None
def uinput():
return next(it) if it else sys.stdin.readline().rstrip()
# Reading single elements
[self.n] = map(int, uinput().split())
self.s = uinput()
def calculate(self):
""" Main calcualtion function of the class """
result = 1
empty = 0
prev = self.s[0]
for i in range(1, self.n):
cur = self.s[i]
if cur != prev:
result += 1
else:
empty += 1
prev = cur
result += min(empty, 2)
return str(result)
###############################################################################
# Unit Tests
###############################################################################
class unitTests(unittest.TestCase):
def test_single_test(self):
""" Thinking class testing """
# Constructor test
test = "8\n10000011"
d = Thinking(test)
self.assertEqual(d.n, 8)
self.assertEqual(d.s, "10000011")
# Sample test
self.assertEqual(Thinking(test).calculate(), "5")
# Sample test
test = "2\n01"
self.assertEqual(Thinking(test).calculate(), "2")
# Sample test
test = "1\n0"
self.assertEqual(Thinking(test).calculate(), "1")
# My tests
test = "6\n010100"
self.assertEqual(Thinking(test).calculate(), "6")
# Time limit test
# self.time_limit_test(5000)
def time_limit_test(self, nmax):
""" Timelimit testing """
import random
import timeit
# Random inputs
test = str(nmax) + " " + str(nmax) + "\n"
numnums = [str(i) + " " + str(i+1) for i in range(nmax)]
test += "\n".join(numnums) + "\n"
nums = [random.randint(1, 10000) for i in range(nmax)]
test += " ".join(map(str, nums)) + "\n"
# Run the test
start = timeit.default_timer()
d = Thinking(test)
calc = timeit.default_timer()
d.calculate()
stop = timeit.default_timer()
print("\nTimelimit Test: " +
"{0:.3f}s (init {1:.3f}s calc {2:.3f}s)".
format(stop-start, calc-start, stop-calc))
if __name__ == "__main__":
# Avoiding recursion limitaions
sys.setrecursionlimit(100000)
if sys.argv[-1] == "-ut":
unittest.main(argv=[" "])
# Print the result string
sys.stdout.write(Thinking().calculate())
| 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>
const long long MOD = 1e9 + 7;
const int MAX_N = 100000 + 100;
const long long INF = 1e18;
const long long delta = 10957;
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int n;
cin >> n;
string s;
cin >> s;
if (n == 1) return cout << 1, 0;
int m = 0;
int f = 0;
for (int i = 0; i < n - 1; ++i) {
int l = 1;
if (s[i] == s[i + 1]) {
while (s[i] == s[i + 1]) {
l++;
i++;
}
}
if (l == 2)
f++;
else if (l > 2)
f += 2;
m++;
}
if (n > 1 and s[n - 1] != s[n - 2]) m++;
if (m == 1 and n > 2) return cout << 3, 0;
if (f > 1)
m += 2;
else if (f > 0)
m++;
cout << m;
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.ArrayList;
import java.util.Scanner;
import java.lang.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String s = in.next();
if(n == 1)
System.out.println(1);
else {
ArrayList<Integer> a = new ArrayList(s.length());
int cnt = 1;
char curr = s.charAt(0);
for(int i = 1; i < s.length(); i++){
if(s.charAt(i) != curr){
curr = s.charAt(i);
a.add(cnt);
cnt = 0;
}
cnt++;
if(i == s.length()-1){
a.add(cnt);
}
}
int three = 0, two = 0;
for(int i = 0; i < a.size(); i++){
if(a.get(i) >= 3){
three++;
} else if(a.get(i) == 2){
two++;
}
}
if(three != 0)
System.out.println(a.size()+2);
else if(two > 1)
System.out.println(a.size()+2);
else if(two == 1)
System.out.println(a.size()+1);
else
System.out.println(a.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.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author ATailouloute
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
QuickScanner in = new QuickScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
PrintWriter out;
public void solve(int testNumber, QuickScanner in, PrintWriter out) {
this.out = out;
int n = in.nextInt();
String ss = in.nextString();
out.println(solve(ss));
}
int solve(String ss) {
char[] s = ss.toCharArray();
int x = -1, y = -1;
for (int i = 1; i < s.length; i++) {
if (s[i] == s[i - 1]) {
x = i;
break;
}
}
if (x != -1) {
for (int i = x; i < s.length - 1; i++) {
if (s[i] == s[i + 1]) {
y = i;
}
}
}
if (x != -1 && y != -1) {
flip(s, x, y);
}
int ret = count(s);
y = -1;
s = ss.toCharArray();
for (int i = 0; i < s.length - 1; i++) {
if (s[i] == s[i + 1]) {
y = i;
}
}
if (y != -1) {
flip(s, 0, y);
ret = Math.max(ret, count(s));
}
y = -1;
s = ss.toCharArray();
for (int i = s.length - 1; i > 0; i--) {
if (s[i] == s[i - 1]) {
y = i;
}
}
if (y != -1) {
flip(s, y, s.length - 1);
ret = Math.max(ret, count(s));
}
return ret;
}
void flip(char[] s, int from, int to) {
for (int i = from; i <= to; i++) {
s[i] = s[i] == '0' ? '1' : '0';
}
}
int count(char[] s) {
int ret = 1;
for (int i = 1; i < s.length; i++) {
if (s[i] != s[i - 1]) ret++;
}
return ret;
}
}
static class QuickScanner {
BufferedReader br;
StringTokenizer st;
InputStream is;
public QuickScanner(InputStream stream) {
is = stream;
br = new BufferedReader(new InputStreamReader(stream), 32768);
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
String nextString() {
return nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
}
}
| 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 | __author__ = 'MoonBall'
import sys
# sys.stdin = open('data/C.in', 'r')
T = 1
def process():
input()
s = input()
if len(s) == 1:
print(1)
return
b = []
for x in s:
if (not b) or b[-1][0] != x:
b.append((x, 1))
else:
b[-1] = (b[-1][0], b[-1][1] + 1)
ans = len(b)
c2 = 0
c3 = 0
for x in b:
if x[1] >= 2:
c2 = c2 + 1
ans = max(ans, len(b) + 1)
if x[1] >= 3:
c3 = c3 + 1
if c2 >= 2:
ans = max(ans, len(b) + 2)
if c3 >= 1:
ans = max(ans, len(b) + 2)
print(ans)
for _ in range(T):
process()
| 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 + 3;
char s[MAXN];
int main() {
int n;
scanf("%d", &n);
scanf("%s", s + 1);
int c = 0;
int p = 0;
char previ = 'a';
int ans = 0;
for (int i = 1; i <= n; i++) {
if (s[i] != previ)
p = 1, ans++;
else {
p++;
if (p >= 2) c++;
}
previ = s[i];
}
if (c > 2) c = 2;
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 | n = int(input())
s = input()
dl = 1
now = s[0]
k = 0
for i in range(1, n):
if now != s[i]:
now = s[i]
dl += 1
for i in range(n - 1):
if s[i] == s[i + 1] == '0' or s[i] == s[i + 1] == '1':
k += 1
if k >= 2:
print(dl + 2)
elif k == 1:
print(dl + 1)
else:
print(dl)
| 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 | def main():
input()
l, a = [0, 0], '*'
for b in input():
l[a == b] += 1
a = b
print(l[0] + min(l[1], 2))
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 |
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.lang.*;
public class P603A {
//static long mod=1000000007;
public static int LAS(char a[],int n)
{
int ans=1;
for(int i=0;i<n-1;i++)
{
if(a[i]!=a[i+1])
{
ans++;
}
}
return min(n,ans+2);
}
public static void main(String[] args) throws Exception{
InputReader in = new InputReader(System.in);
PrintWriter pw=new PrintWriter(System.out);
//int t=in.readInt();
// while(t-->0)
//{
int n=in.readInt();
//long n=in.readLong();
/*int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=in.readInt();
}*/
String a=in.readString();
char c[]=a.toCharArray();
int l=0;
pw.println(LAS(c,n));
//}
pw.close();
}
static BigInteger ways(int N, int K) {
BigInteger ret = BigInteger.ONE;
for(int i=N;i>=N-K+1;i--)
{
ret = ret.multiply(BigInteger.valueOf(i));
}
for (int j = 1; j<=K; j++) {
ret = ret.divide(BigInteger.valueOf(j));
}
ret=ret.mod(BigInteger.valueOf(1000000007));
return ret;
}
public static long InverseEuler(long n, long MOD)
{
return pow(n,MOD-2,MOD);
}
public static long ways(int n, int r, long MOD)
{
// vector<long long> f(n + 1,1);
long f[]=new long[n+1];
Arrays.fill(f,1);
for (int i=2; i<=n;i++)
f[i]= (f[i-1]*i) % MOD;
return (f[n]*((InverseEuler(f[r], MOD) * InverseEuler(f[n-r], MOD)) % MOD)) % MOD;
}
public static int prime(int n)
{
int f=1;
if(n==1)
return 0;
for(int i=2;i<=(Math.sqrt(n));)
{
if(n%i==0)
{
f=0;
break;
}
if(i==2)
i++;
else
i+=2;
}
if(f==1)
return 1;
else
return 0;
}
static void permutations(int n) {
int[] p = new int[n];
for (int index = 0; index < n; ++index) {
p[index] = index + 1;
}
while (true) {
System.out.println(Arrays.toString(p));
int k = n - 1;
while (k > 0) {
if (p[k] < p[k - 1]) {
k--;
}
else {
break;
}
}
if (k == 0) {
break;
}
int j = n - 1;
while (p[j] < p[k - 1]) {
j--;
}
int t = p[j];
p[j] = p[k - 1];
p[k - 1] = t;
int i = 0;
while (k + i < n - 1 - i) {
t = p[k + i];
p[k + i] = p[n - i - 1];
p[n - i - 1] = t;
i++;
}
}
}
static boolean isPrime(int number) {
if (number <= 1) {
return false;
}
if (number <= 3) {
return true;
}
if (number % 2 == 0 || number % 3 == 0) {
return false;
}
int i = 5;
while (i * i <= number) {
if (number % i == 0 || number % (i + 2) == 0) {
return false;
}
i += 6;
}
return true;
}
public static long gcd(long x,long y)
{
if(x%y==0)
return y;
else return gcd(y,x%y);
}
public static BigInteger fact(int n)
{
BigInteger f=BigInteger.ONE;
for(int i=1;i<=n;i++)
{
f=f.multiply(BigInteger.valueOf(i));
}
//f=f.mod(BigInteger.valueOf(m));
return f;
}
public static int gcd(int x,int y)
{
if(x%y==0)
return y;
else return gcd(y,x%y);
}
public static int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
public static int min(int a,int b)
{
if(a>b)
return b;
else
return a;
}
public static long max(long a,long b)
{
if(a>b)
return a;
else
return b;
}
public static long min(long a,long b)
{
if(a>b)
return b;
else
return a;
}
public static long pow(long n,long p,long m)
{
long result = 1;
if(p==0)
return 1;
if (p==1)
return n;
while(p!=0)
{
if(p%2==1)
result *= n;
if(result>m)
result%=m;
p >>=1;
n*=n;
if(n>m)
n%=m;
}
return result;
}
static long ncr(long n,long k)
{
long w=1;
for(long i=n,j=1;i>=n-k+1;i--,j++)
{
w=w*i;
w=w/j;
w%=1000000007;
}
return w;
}
static class Pair implements Comparable<Pair>
{
int a,b;
Pair (int a,int b)
{
this.a=a;
this.b=b;
}
public int compareTo(Pair o) {
// TODO Auto-generated method stub
if(this.a!=o.a)
return Integer.compare(this.a,o.a);
else
return Integer.compare(this.b, o.b);
//return 0;
}
}
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
{
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public double readDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long 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 boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(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 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();
}
public void flush()
{
writer.flush();
}
}
/* USAGE
//initialize
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
//read int
int i = in.readInt();
//read string
String s = in.readString();
//read int array of size N
int[] x = IOUtils.readIntArray(in,N);
//printline
out.printLine("X");
//flush output
out.flush();
//remember to close the
//outputstream, at the end
out.close();
*/
static class IOUtils {
public static int[] readIntArray(InputReader in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++)
array[i] = in.readInt();
return array;
}
}
//BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//StringBuilder sb=new StringBuilder("");
//InputReader in = new InputReader(System.in);
// OutputWriter out = new OutputWriter(System.out);
//PrintWriter pw=new PrintWriter(System.out);
//String line=br.readLine().trim();
//int t=Integer.parseInt(br.readLine());
// while(t-->0)
//{
//int n=Integer.parseInt(br.readLine());
//long n=Long.parseLong(br.readLine());
//String l[]=br.readLine().split(" ");
//int m=Integer.parseInt(l[0]);
//int k=Integer.parseInt(l[1]);
//String l[]=br.readLine().split(" ");
//l=br.readLine().split(" ");
/*int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(l[i]);
}*/
//System.out.println(" ");
//}
}
| 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 | __author__ = 'trunghieu11'
def solve(n, s):
answer = 1
cur = s[0]
same = 0
for i in range(1, n):
answer += s[i] != cur
cur = s[i]
same += s[i] == s[i - 1]
return answer + min(2, same)
if __name__ == '__main__':
n = int(raw_input())
s = raw_input()
print solve(n, s) | 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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.ArrayList;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author baukaman
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
String s = in.next();
out.println(get(s));
}
public int get(String s) {
Integer[] arr = decompose(s);
int ret = arr.length;
for (int i = 0; i < arr.length; i++)
if (arr[i] > 2)
return ret + 2;
int a2cnt = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 1)
a2cnt++;
}
if (a2cnt > 1)
return ret + 2;
for (int i = 0; i < arr.length; i++)
if (arr[i] > 1)
return ret + 1;
return ret;
}
public Integer[] decompose(String s) {
int n = s.length();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int a = 1;
int j = i + 1;
while (j < n && s.charAt(j) == s.charAt(i)) {
a++;
j++;
}
list.add(a);
i = j - 1;
}
return list.toArray(new Integer[0]);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| 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 INF = 1000000000;
const long double PI = 3.14159265358979323846264338327;
void solution() {
int n;
cin >> n;
string s;
cin >> s;
char lastDigit = s[0];
vector<int> indexes;
indexes.push_back(0);
for (int i = 1; i < n; i++) {
if (s[i] != lastDigit) {
indexes.push_back(i);
lastDigit = s[i];
}
}
if (indexes.size() == n) {
cout << n << "\n";
return;
}
int maxDiff = 0;
for (int i = 1; i < indexes.size(); i++) {
maxDiff = max(maxDiff, indexes[i] - indexes[i - 1]);
}
maxDiff = max(maxDiff, n - 1 - indexes.back());
if (maxDiff >= 2) {
cout << min(int(indexes.size()) + 2, n) << "\n";
return;
}
cout << indexes.size() + 1 << "\n";
}
void include_file() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
int main() {
solution();
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;
long long n, i, k = 0;
char a[100000];
int main() {
cin >> n;
for (i = 0; i < n; i++) {
cin >> a[i];
if (i != 0 && a[i] == a[i - 1]) k++;
}
if (k == 1) {
cout << n;
return 0;
}
if (k >= 2) {
cout << n - k + 2;
return 0;
}
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 | #include <bits/stdc++.h>
using namespace std;
const long long int N = 1e5 + 1000;
long long int dp[N], n, ans, cnt = 1, mx;
string s;
int main() {
cin >> n >> s;
dp[0] = 1;
for (int i = 1; i < n; i++) {
dp[i] = dp[i - 1];
dp[i] += (s[i] != s[i - 1]);
if (s[i] == s[i - 1]) {
cnt++;
if (cnt > 1) ans++;
} else
cnt = 1;
mx = max(cnt, mx);
}
if (ans == 1 && mx == 2) {
cout << n << '\n';
return 0;
}
if (mx > 2 || ans > 1)
cout << dp[n - 1] + 2;
else
cout << dp[n - 1];
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())
a = list(input())
sum = 0
pr = '2'
for i in range(n):
if a[i] != pr:
sum += 1
pr = a[i]
eo = 0
for i in range(n - 1):
if a[i] == a[i + 1]:
eo += 1
if eo >= 2:
print(min(n, sum + 2))
elif eo == 1 or len(a) == 2:
print(min(sum + 1, n))
else:
print(sum)
| 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.cf3xx.cf334.div1;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
/**
* Created by hama_du on 2016/08/17.
*/
public class Main {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
char[] c = in.nextToken().toCharArray();
int maxlen = 0;
int lc = 0;
int cnt = 0;
for (int i = 0; i < n ; ) {
int j = i;
while (j < n && c[i] == c[j]) {
j++;
}
if (maxlen == j-i) {
lc++;
} else if (maxlen < j-i) {
maxlen = j-i;
lc = 1;
}
cnt++;
i = j;
}
if (maxlen >= 3 || (maxlen == 2 && lc >= 2)) {
cnt += 2;
} else if (maxlen >= 2) {
cnt++;
}
out.println(cnt);
out.flush();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int[] nextInts(int n) {
int[] ret = new int[n];
for (int i = 0; i < n; i++) {
ret[i] = nextInt();
}
return ret;
}
private int[][] nextIntTable(int n, int m) {
int[][] ret = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ret[i][j] = nextInt();
}
}
return ret;
}
private long[] nextLongs(int n) {
long[] ret = new long[n];
for (int i = 0; i < n; i++) {
ret[i] = nextLong();
}
return ret;
}
private long[][] nextLongTable(int n, int m) {
long[][] ret = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ret[i][j] = nextLong();
}
}
return ret;
}
private double[] nextDoubles(int n) {
double[] ret = new double[n];
for (int i = 0; i < n; i++) {
ret[i] = nextDouble();
}
return ret;
}
private int next() {
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 char nextChar() {
int c = next();
while (isSpaceChar(c))
c = next();
if ('a' <= c && c <= 'z') {
return (char) c;
}
if ('A' <= c && c <= 'Z') {
return (char) c;
}
throw new InputMismatchException();
}
public String nextToken() {
int c = next();
while (isSpaceChar(c))
c = next();
StringBuilder res = new StringBuilder();
do {
res.append((char) c);
c = next();
} while (!isSpaceChar(c));
return res.toString();
}
public int nextInt() {
int c = next();
while (isSpaceChar(c))
c = next();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = next();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = next();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = next();
while (isSpaceChar(c))
c = next();
long sgn = 1;
if (c == '-') {
sgn = -1;
c = next();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = next();
} while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
return Double.valueOf(nextToken());
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static void debug(Object... o) {
System.err.println(Arrays.deepToString(o));
}
}
| 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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
String s = in.next();
int cnt = 0;
for (int i = 0; i + 1 < s.length(); ++i) if (s.charAt(i) == s.charAt(i + 1)) ++cnt;
cnt -= 2;
cnt = Math.max(cnt, 0);
out.println(s.length() - cnt);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
} | 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().split())
a = input()
prev,cur = ('0','0')
t = 1
flag = 0
x = 1
if ( len(a) > 1 ):
for i in range(len(a)):
cur = a[i]
if ( i > 0 ):
if ( prev == cur ):
x = x + 1
else:
x = 1
t += 1
if ( x >= 2 ):
flag += 1
prev = cur
#print ( "%d %d" % ( t, flag))
if ( flag >= 2 ):
t += 2
elif ( flag > 0 ):
t += 1
print (int(t))
| 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;
char s[100100];
int main() {
int n, now = 1, add = 0;
scanf("%d", &n);
scanf("%s", s);
for (int i = 1; i < n; i++) {
if (s[i] != s[i - 1])
now++;
else
add++;
}
printf("%d\n", now + min(add, 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()
print(min(n,sum(s[i]!=s[i-1] for i in range(1,n))+3)) | 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 double EPS = 1e-9;
const int INF = 0x7f7f7f7f;
int main() {
ios_base::sync_with_stdio(false);
int n, cnt = 1;
string str;
cin >> n >> str;
for (int i = 1; i <= n - 1; i++) {
if (str[i] != str[i - 1]) cnt++;
}
int ans = min(cnt + 2, 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 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
char str[N];
int f[N][2];
void updata(int &x, int y) { x = max(x, y); }
int main() {
int n;
scanf("%d", &n);
scanf("%s", str + 1);
n = strlen(str + 1);
memset(f, -1, sizeof(f));
f[0][0] = f[0][1] = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < 2; ++j)
if (f[i][j] != -1) {
updata(f[i + 1][j], f[i][j]);
if (str[i + 1] - '0' != j) {
updata(f[i + 1][j ^ 1], f[i][j] + 1);
}
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < 2; ++j)
if (f[i][j] != -1) {
updata(f[i + 1][j], f[i][j]);
if (str[i + 1] - '0' == j) {
updata(f[i + 1][j ^ 1], f[i][j] + 1);
}
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < 2; ++j)
if (f[i][j] != -1) {
updata(f[i + 1][j], f[i][j]);
if (str[i + 1] - '0' != j) {
updata(f[i + 1][j ^ 1], f[i][j] + 1);
}
}
printf("%d\n", max(f[n][0], f[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 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
String s = in.next();
ArrayList<Integer> list = new ArrayList<>();
int cnt = 0;
for (int i = 1; i < n; i++) {
if (s.charAt(i) == s.charAt(i - 1)) {
cnt++;
}
}
cnt = Math.max(cnt - 2, 0);
n -= cnt;
out.print(n);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch ( IOException e ) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| 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;
string s;
int main() {
int n, dem = 0;
cin >> n;
cin >> s;
for (int i = 0; i < s.length(); i++) {
if (s[i] != s[i + 1]) dem++;
}
if (dem == n)
cout << dem;
else if (dem + 2 < n)
cout << dem + 2;
else if (dem < n && dem + 2 >= n)
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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main
{
void solve() throws Exception
{
int n = nextInt();
char[] a = nextToken().toCharArray();
int b = n;
int e = -1;
int ans = 1;
for( int i = 0; i < n; i++ )
{
if( i > 0 && a[ i - 1 ] != a[ i ] ) ans++;
if( b == n && i > 0 && a[ i ] == a[ i - 1 ] ) b = i;
if( i < n - 1 && a[ i ] == a[ i + 1 ] ) e = i;
}
if( b <= e ) ans += 2; else if( b != n || e != -1 ) ans++;
out.println( ans );
}
public static void main( String[] args )
{
new Main().run();
}
public void run()
{
try
{
in = new BufferedReader( new InputStreamReader( System.in ) );
out = new PrintWriter( System.out );
solve();
}
catch( Exception e )
{
e.printStackTrace();
System.exit( 1 );
}
finally
{
out.close();
}
}
BufferedReader in;
PrintWriter out;
StringTokenizer st;
String nextToken() throws IOException
{
while( st == null || !st.hasMoreTokens() ) st = new StringTokenizer( in.readLine() );
return st.nextToken();
}
int nextInt() throws IOException
{
return Integer.parseInt( nextToken() );
}
long nextLong() throws IOException
{
return Long.parseLong( nextToken() );
}
} | 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, m, f, g, te = 0, num = 0;
bool pls = 0;
string s;
cin >> n;
cin >> s;
char cur = '4';
for (int i = 0; i < s.size(); i++) {
if (s[i] != cur) {
cur = s[i];
te++;
}
if (i >= 2) {
string t = s.substr(i - 2, 3);
if (t[0] == t[1] && t[1] == t[2]) {
pls = 1;
}
}
if (i > 0) {
string t = s.substr(i - 1, 2);
if (t[0] == t[1]) num++;
}
}
cout << min(n, te + 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 | /* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
String s=sc.next();
for(int i=0;i<n;i++)
a[i]=s.charAt(i)-'0';
int compressed=1;
for(int i=1;i<n;i++)
if(a[i]!=a[i-1])compressed++;
System.out.println(Math.min(n,compressed+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 |
n = int(raw_input())
results = [int(x) for x in raw_input()]
dp = []
for i in xrange(n):
dp.append([])
for j in xrange(3):
dp[i].append([])
for k in xrange(2):
dp[i][j].append(0)
dp[0][2][0] = 0
dp[0][2][1] = 0
if results[0] == 0:
dp[0][1][0] = 0
dp[0][1][1] = 1
dp[0][0][0] = 1
dp[0][0][1] = 0
else:
dp[0][1][0] = 1
dp[0][1][1] = 0
dp[0][0][0] = 0
dp[0][0][1] = 1
for i in xrange(1, n):
cur = results[i]
p2c = dp[i - 1][2][cur]
p2o = dp[i - 1][2][1 - cur]
p1c = dp[i - 1][1][cur]
p1o = dp[i - 1][1][1 - cur]
p0c = dp[i - 1][0][cur]
p0o = dp[i - 1][0][1 - cur]
dp[i][0][cur] = max(p0c, 1 + p0o)
dp[i][0][1 - cur] = p0o
dp[i][1][1 - cur] = max(p1o, 1 + p1c, 1 + p0c)
dp[i][1][cur] = p1c
dp[i][2][cur] = max(p2c, p1c, 1 + p2o, 1 + p1o)
dp[i][2][1 - cur] = p2o
print max(dp[n - 1][2][0], dp[n - 1][2][1], dp[n - 1][1][0], dp[n-1][1][1])
| 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>
using namespace std;
long long n, i, t = 3;
string s;
int main() {
cin >> n >> s;
for (i = 1; i < n; i++)
if (s[i] != s[i - 1]) t++;
cout << min(t, 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.lang.*;
import java.io.*;
import java.util.*;
import java.math.*;
import static java.lang.Math.*;
public class Solution implements Runnable {
void solve() throws Exception {
int n = sc.nextInt();
String s = sc.nextToken();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = s.charAt(i) - '0';
}
int[][][] dp = new int[n + 1][3][2];
for (int i = 0; i < n; i++) {
if (a[i] == 0) {
dp[i + 1][0][0] = dp[i][0][1] + 1;
dp[i + 1][0][1] = dp[i][0][1];
dp[i + 1][1][1] = max(dp[i][1][0], dp[i][0][0]) + 1;
dp[i + 1][1][0] = max(dp[i][1][0], dp[i][0][0]);
dp[i + 1][2][0] = max(dp[i][1][1], dp[i][2][1]) + 1;
dp[i + 1][2][1] = dp[i][2][1];
} else {
dp[i + 1][0][1] = dp[i][0][0] + 1;
dp[i + 1][0][0] = dp[i][0][0];
dp[i + 1][1][0] = max(dp[i][1][1], dp[i][0][1]) + 1;
dp[i + 1][1][1] = max(dp[i][1][1], dp[i][0][1]);
dp[i + 1][2][1] = max(dp[i][1][0], dp[i][2][0]) + 1;
dp[i + 1][2][0] = dp[i][2][0];
}
}
int ans = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
ans = max(ans, dp[n][i][j]);
}
}
out.println(ans);
}
BufferedReader in;
PrintWriter out;
FastScanner sc;
final String INPUT_FILE = "";
final String OUTPUT_FILE = "";
static Throwable throwable;
public static void main(String[] args) throws Throwable {
Thread thread = new Thread(null, new Solution(), "", (1 << 26));
thread.start();
thread.join();
thread.run();
if (Solution.throwable != null)
throw Solution.throwable;
}
public void run() {
try {
if (INPUT_FILE.equals("")) {
in = new BufferedReader(new InputStreamReader(System.in));
} else {
in = new BufferedReader(new FileReader(INPUT_FILE));
}
if (OUTPUT_FILE.equals("")) {
out = new PrintWriter(System.out);
} else {
out = new PrintWriter(OUTPUT_FILE);
}
sc = new FastScanner(in);
solve();
} catch (Exception e) {
throwable = e;
} finally {
out.close();
}
}
}
class FastScanner {
BufferedReader reader;
StringTokenizer strTok;
FastScanner(BufferedReader reader) {
this.reader = reader;
}
public String nextToken() throws Exception {
while (strTok == null || !strTok.hasMoreTokens()) {
strTok = new StringTokenizer(reader.readLine());
}
return strTok.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
} | 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())
lis=[0]*n
bb=[]
p=s[0];lis[0]=1;kk=0
for i in range(1,len(s)):
if s[i]!=p:
lis[i]=1
p=s[i]
for i in range(1,len(s)):
# print(s[i],s[i-1])
if s[i]==s[i-1]:
kk+=1
else:
bb.append(kk);kk=0
bb.append(kk)
ans=c=0
#print(lis,bb)
if max(bb)>1:
print(sum(lis)+2)
else:
print(sum(lis)+min(2,bb.count(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>
using namespace std;
int main() {
int n, res = 1;
string in;
cin >> n >> in;
for (int i = 1; i < n; i++) {
res += (in[i] != in[i - 1]);
}
cout << min(res + 2, 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;
char s[101000];
int b[101000];
int n, m, ans;
int main() {
cin >> n;
scanf("%s", s + 1);
int now = 0;
for (int i = 1; i <= n; i++) {
if (s[now] != s[i]) ans++, now = i;
if (s[i] == s[i - 1]) m++;
}
ans += min(2, m);
cout << ans << 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 | input()
s=input()
t,r=int(s[0]),0
for i in s:
if int(i)==t:r+=1;t=1-t
print(min(r+2,len(s))) | 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 MAX = 100001;
char s[MAX];
int n;
int mem[MAX][3][3];
int dp(int index, int prev, int flip) {
if (index >= n) return 0;
int cur = s[index] - '0';
int &ans = mem[index][cur][flip];
if (ans != -1) return ans;
int choice1 = 0;
int f = flip;
if (f == 1) f = 0;
if (prev == cur)
choice1 = dp(index + 1, prev, f);
else
choice1 = 1 + dp(index + 1, cur, f);
int choice2 = 0;
if (flip != 0) {
flip = 1;
if (cur == 1)
cur = 0;
else
cur = 1;
if (prev == cur)
choice2 = dp(index + 1, prev, flip);
else
choice2 = 1 + dp(index + 1, cur, flip);
}
return ans = max(choice1, choice2);
}
int main() {
cin >> n >> s;
memset(mem, -1, sizeof(mem));
cout << dp(0, 2, 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.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
public class AlternativeThinking {
private static Reader in;
private static PrintWriter out;
public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter (System.out, true);
int n = in.nextInt();
char []data = new char[n];
int result = 1,
count = 0,
maxCount = -1;
data[0] = in.nextChar();
for (int i = 1; i < n; i++) {
data[i] = in.nextChar();
if (data[i] == data[i-1]) {
//out.println("kaka");
count += 1;
maxCount = Math.max(maxCount, count);
} else {
//out.println("dfgdf");
result++;
count = 0;
}
}
out.println(Math.min(result+2, n));
out.close();
in.close();
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader(){
din=new DataInputStream(System.in);
buffer=new byte[BUFFER_SIZE];
bufferPointer=bytesRead=0;
}
public Reader(String file_name) throws IOException{
din=new DataInputStream(new FileInputStream(file_name));
buffer=new byte[BUFFER_SIZE];
bufferPointer=bytesRead=0;
}
public String readLine() throws IOException{
byte[] buf=new byte[64]; // line length
int cnt=0,c;
while((c=read())!=-1){
if(c=='\n')break;
buf[cnt++]=(byte)c;
}
return new String(buf,0,cnt);
}
public int nextInt() throws IOException{
int ret=0;byte c=read();
while(c<=' ')c=read();
boolean neg=(c=='-');
if(neg)c=read();
do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');
if(neg)return -ret;
return ret;
}
public long nextLong() throws IOException{
long ret=0;byte c=read();
while(c<=' ')c=read();
boolean neg=(c=='-');
if(neg)c=read();
do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');
if(neg)return -ret;
return ret;
}
public double nextDouble() throws IOException{
double ret=0,div=1;byte c=read();
while(c<=' ')c=read();
boolean neg=(c=='-');
if(neg)c = read();
do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');
if(c=='.')while((c=read())>='0'&&c<='9')
ret+=(c-'0')/(div*=10);
if(neg)return -ret;
return ret;
}
public char nextChar() throws IOException{
byte c=read();
while(c<=' ')c=read();
return (char)c;
}
private void fillBuffer() throws IOException{
bytesRead=din.read(buffer,bufferPointer=0,BUFFER_SIZE);
if(bytesRead==-1)buffer[0]=-1;
}
private byte read() throws IOException{
if(bufferPointer==bytesRead)fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException{
if(din==null) return;
din.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 | """
Codeforces Round #334 (Div. 2)
Problem 604 C. Alternative Thinking
@author yamaton
@date 2015-12-01
"""
import itertools as it
import functools
import operator
import collections
import math
import sys
def solve(s, n):
lens = [sum(1 for i in iterable) for (_, iterable) in it.groupby(s)]
maxlen = max(lens)
altlen = len(lens)
if maxlen == 1:
return altlen
elif maxlen >= 3:
return altlen + 2
else:
if lens.count(2) == 1:
return altlen + 1
else:
return altlen + 2
# def pp(*args, **kwargs):
# return print(*args, file=sys.stderr, **kwargs)
def main():
n = int(input())
s = input().strip()
assert len(s) == n
result = solve(s, n)
print(result)
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 | #include <bits/stdc++.h>
using namespace std;
const int OO = 0x3f3f3f3f;
const int _OO = -1 * OO;
const double EPS = (1e-9);
const double pi = 3.14159265;
using namespace std;
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dy8[8] = {1, -1, 0, 1, -1, 1, -1, 0};
int n;
const int N = 1e5;
int dp[N];
string s;
string ch(string sss, int l, int r) {
for (int i = l; i <= r; i++) {
if (sss[i] == '0')
sss[i] = '1';
else
sss[i] = '0';
}
return sss;
}
int solve(int i, int pre, string sss) {
if (i == n) return 0;
int c1 = solve(i + 1, pre, sss);
int c2 = _OO;
if (pre == n || sss[i] != sss[pre]) c2 = solve(i + 1, i, sss) + 1;
return max(c1, c2);
}
int ans(int i) {
if (i == n) return 0;
int &ret = dp[i];
if (~ret) return ret;
int mx = 0;
for (int j = i; j < n; j++) {
string ss = ch(s, i, j);
ret = solve(0, n, ss);
mx = max(mx, ret);
}
return ret = mx;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
cin >> s;
int eq = 0;
for (int i = 1; i < ((int)((s).size())); i++) eq += (s[i] == s[i - 1]);
int mx = (n - eq) + 2;
cout << min(mx, 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 | # Author : nitish420 --------------------------------------------------------------------
import os
import sys
from io import BytesIO, IOBase
# mod=10**9+7
# sys.setrecursionlimit(10**6)
# from functools import lru_cache
def main():
n=int(input())
s=list(map(int,input()))
ans=1
for i in range(1,n):
if s[i]!=s[i-1]:
ans+=1
print(min(ans+2,n))
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = 'x' in file.mode or 'r' not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b'\n') + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode('ascii'))
self.read = lambda: self.buffer.read().decode('ascii')
self.readline = lambda: self.buffer.readline().decode('ascii')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
# endregion
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 | #include <bits/stdc++.h>
using namespace std;
string b;
long long int a[100005];
int main() {
long long int n, i;
cin >> n >> b;
for (i = 0; i < b.length(); i++) a[i] = b[i] - 48;
long long int count = 1;
for (i = 1; i < n; i++) count += (a[i] != a[i - 1]);
cout << min(count + 2, n) << 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;
string str;
int maxSub(int i, int j, bool ee) {
int ans = 0;
int last = str[i] - '0';
for (int k = i; k <= j; k++) {
int c = str[k] - '0';
if (c == last) {
last = 1 - last;
ans++;
}
}
return min(ans + 2, j - i + 1);
}
int main() {
int n;
cin >> n;
cin >> str;
cout << maxSub(0, n - 1, true) << 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 n, i, maxx = -1e17, ans = 1;
string s;
cin >> n;
cin >> s;
for (i = 1; i < n; i++) {
if (s[i] != s[i - 1]) ++ans;
}
cout << min(ans + 2, 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;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
int inf = 0x3f3f3f3f;
long long infl = 0x3f3f3f3f3f3f3f3fLL;
long double infd = 1.0 / 0.0;
const long long MOD = 1e9 + 7;
const long double pi = 2 * acos(0.0);
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
;
int n;
cin >> n;
string s;
cin >> s;
int as = 1;
char pre = s[0];
for (int i = 1; i < n; i++) {
if (s[i] == pre) continue;
pre = s[i];
as++;
}
cout << min(n, as + 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.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt(),cnt = 1;
char arr[] = sc.nextLine().toCharArray();
for(int i = 1; i < n; i++) {
if(arr[i] != arr[i-1]) cnt++;
}
out.println(Math.min(n, cnt + 2));
out.close();
}
}
class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
} | 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
import math
MAXNUM = math.inf
MINNUM = -1 * math.inf
ASCIILOWER = 97
ASCIIUPPER = 65
def getInt():
return int(sys.stdin.readline().rstrip())
def getInts():
return map(int, sys.stdin.readline().rstrip().split(" "))
def getString():
return sys.stdin.readline().rstrip()
def printOutput(ans):
sys.stdout.write()
pass
def solve(string):
tab = [[0 for _ in range(3)] for _ in range(len(string))]
states = [[0 for _ in range(3)] for _ in range(len(string))]
if string[0] == "1":
states[0][0] = "1"
states[0][2] = "1"
states[0][1] = "0"
else:
states[0][0] = "0"
states[0][2] = "0"
states[0][1] = "1"
tab[0][0] = 1
tab[0][1] = 1
tab[0][2] = 1
for i in range(1, len(string)):
ele = string[i]
if ele != states[i - 1][0]:
tab[i][0] = tab[i - 1][0] + 1 # add one for alternating
states[i][0] = ele
tab[i][1] = tab[i - 1][
0
] # flipping here would be same as tab[i-1] since they would be consecutive?
else:
tab[i][0] = tab[i - 1][0]
states[i][0] = ele # they are the same, so it doesn't matter
tab[i][1] = tab[i - 1][0] + 1 # flipping here would result in + 1
if (
ele == states[i - 1][1]
): # we want to flip this, so we need compare for likeness
tab[i][1] = max(
tab[i - 1][1] + 1, tab[i][1]
) # either continue flipping or start flipping
states[i][1] = str((int(ele) - 1) % 2)
if ele != states[i - 1][2]:
tab[i][2] = tab[i - 1][2] + 1
states[i][2] = ele
else:
tab[i][2] = tab[i - 1][2]
states[i][2] = ele
if tab[i][1] > tab[i][2]:
tab[i][2] = tab[i][1]
states[i][2] = states[i][1]
return max(tab[-1])
def readinput():
a = getInts()
print(solve(getString()))
readinput()
| 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>
int n, i = 1, a = 3;
char s[100001];
int main() {
scanf("%d%s", &n, s);
for (; i < n; i++) a += (s[i] != s[i - 1]);
if (a > n) a = n;
printf("%d", a);
}
| 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("-O2")
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << "\t";
err(++it, args...);
}
template <typename T1, typename T2>
ostream& operator<<(ostream& c, pair<T1, T2>& v) {
c << "(" << v.first << "," << v.second << ")";
return c;
}
template <template <class...> class TT, class... T>
ostream& operator<<(ostream& out, TT<T...>& c) {
out << "{ ";
for (auto& x : c) out << x << " ";
out << "}";
return out;
}
const int LIM = 1e5 + 5, MOD = 1e9 + 7;
const long double EPS = 1e-9;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
string s;
cin >> s;
int dp[n][2][3];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 2; ++j) {
for (int k = 0; k < 3; ++k) dp[i][j][k] = 0;
}
}
for (int i = 0; i < s.length(); ++i) {
int j = s[i] - '0';
if (i == 0) {
dp[i][j][0] = 1;
dp[i][1 - j][1] = 1;
dp[i][j][2] = 1;
continue;
}
dp[i][j][0] = 1 + dp[i - 1][1 - j][0];
dp[i][1 - j][1] = 1 + max(dp[i - 1][j][0], dp[i - 1][j][1]);
dp[i][j][2] = 1 + max({dp[i - 1][1 - j][0], dp[i - 1][1 - j][1],
dp[i - 1][1 - j][2]});
for (int j = 0; j < 2; ++j) {
for (int k = 0; k < 3; ++k) {
dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][k]);
}
}
}
int ans = 0;
for (int j = 0; j < 2; ++j) {
for (int k = 0; k < 3; ++k) {
ans = max(dp[n - 1][j][k], 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.io.*;
import java.util.*;
/**
* Created by peacefrog on 11/4/15.
* Time : 10:24 PM
*/
public class Task_C {
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
PrintWriter out;
long timeBegin, timeEnd;
public void runIO() throws IOException {
timeBegin = System.currentTimeMillis();
InputStream inputStream;
OutputStream outputStream;
if (ONLINE_JUDGE) {
inputStream = System.in;
Reader.init(inputStream);
outputStream = System.out;
out = new PrintWriter(outputStream);
} else {
inputStream = new FileInputStream("/home/peacefrog/Dropbox/IdeaProjects/Problem Solving/input");
Reader.init(inputStream);
out = new PrintWriter(System.out);
}
solve();
out.flush();
out.close();
timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
/*
* Start Solution Here
*/
private void solve() throws IOException {
int n = Reader.nextInt();
String s = Reader.next(); //This Variable default in Code Template
int point= 1;
for (int i = 1; i < n; i++) {
if(s.charAt(i) != s.charAt(i-1) )
{
point++;
}
}
for (int i = 1; i < n; i++) {
if(s.charAt(i) == s.charAt(i-1) )
{
point = Math.min(n , point+2);
break;
}
}
out.println(point);
}
class Node implements Comparable<Node>{
int a ;
long w ;
public Node(int a, long b) {
this.a = a;
this.w = b;
}
@Override
public int compareTo(Node o) {
return Long.compare( w , o.w);
}
}
public static void main(String[] args) throws IOException {
new Task_C().runIO();
}
static class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/**
* call this method to initialize reader for InputStream
*/
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/**
* get next word
*/
static String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
static String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
}
| 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.StringTokenizer;
import static java.lang.Math.min;
public class Code_603A_AlternativeThinking{
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(System.out);
StringTokenizer in=new StringTokenizer(br.readLine());
int n=Integer.parseInt(in.nextToken());
in=new StringTokenizer(br.readLine());
String s=in.nextToken();
int res=1;
for(int i=1;i<n;++i)
if(s.charAt(i)!=s.charAt(i-1))
++res;
out.print(min(res+2,n));
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;
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int xChange[] = {0, 1, 0, -1};
int yChange[] = {1, 0, -1, 0};
const int N = 100001;
string s;
int n, dp[N][2][3];
int solve(int idx, int last, int flag) {
if (idx == n) return 0;
if (dp[idx][last][flag] != -1) return dp[idx][last][flag];
int op1, op2;
op1 = op2 = 0;
if (s[idx] - '0' != last) {
if (flag == 1) {
op1 = solve(idx + 1, s[idx] - '0', 2) + 1;
} else
op1 = solve(idx + 1, s[idx] - '0', flag) + 1;
}
if (s[idx] - '0' == last && flag < 2) {
op1 = solve(idx + 1, 1 - last, 1) + 1;
}
op2 = solve(idx + 1, last, flag);
return dp[idx][last][flag] = max(op1, op2);
}
int main() {
ios::sync_with_stdio(false), cin.tie(NULL);
cin >> n >> s;
memset(dp, -1, sizeof dp);
cout << solve(0, 2, 0);
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 | ans = 1
n = int(input())
s = input()
for i in range(1, n):
ans += int(s[i]!=s[i-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 | import java.util.*;
import java.io.*;
/* Mighty Cohadar */
public class Alfa {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
assert 1 <= n && n <= 1e5 : "out of range, n: " + n;
scanner.nextLine();
char[] A = scanner.nextLine().toCharArray();
assert A.length == n;
int d0 = 0;
int d1 = 0;
int e0 = 0;
int e1 = 0;
int f0 = 0;
int f1 = 0;
for (int i = 0; i < n; i++) {
if (A[i] == '0') {
f0 = 1 + f1;
} else {
f1 = 1 + f0;
}
if (A[i] == '0') {
e1 = 1 + e0;
} else {
e0 = 1 + e1;
}
e0 = Math.max(e0, f0);
e1 = Math.max(e1, f1);
if (A[i] == '0') {
d0 = 1 + d1;
} else {
d1 = 1 + d0;
}
d0 = Math.max(d0, e0);
d1 = Math.max(d1, e1);
}
int res = Math.max(d0, d1);
System.out.println(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=input()
b = raw_input().strip()
def solve(n,b):
if n==1:
return 1
if n==2:
return 2
pre=0
v = 2
bb = []
nn=0
for i in b:
if i != v:
pre += 1
bb.append(nn)
nn=0
v=i
nn+=1
bb.append(nn)
bb=bb[1:]
if pre==n:
return n
#print bb
#print pre
ne=0
no=0
is2 = 0
count = 0
for i in range(len(bb)):
if bb[i]>=3:
return pre+2
if bb[i]>=2:
count += 1
if count >=2:
return pre+2
if count ==1:
return n
return pre+1
print solve(n,b)
| 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 | s1=int(input())
s2=input()
d=1
et=s2[0]
p=0
tp="2"
for i in range(1,s1):
if s2[i]!=et:
d=d+1
et=s2[i]
if s2[i]==s2[i-1] and tp!=s2[i]:
p=p+1
#tp=s2[i]
#if s2[i]!=tp:
# tp=2
if p==0:
print(d)
elif p==1:
print (d+1)
else:
print (d+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;
int n;
string cad;
int mem[5][111111][5];
int go(int prev, int pos, int ch) {
if (pos == n) return 0;
int &r = mem[prev][pos][ch];
if (r != -1) return r;
r = go(prev, pos + 1, ch);
int curr = cad[pos] - '0';
if (ch == 1) curr = 1 - curr;
r = max(r, go(curr, pos + 1, ch) + (curr != prev));
if (ch < 2) r = max(r, go(prev, pos, ch + 1));
return r;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
memset(mem, -1, sizeof(mem));
cin >> n >> cad;
cout << go(2, 0, 0) << '\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.io.*;
import java.util.*;
public class cf {
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());
} catch (Exception e){e.printStackTrace();}
}
public String next() {
if (st.hasMoreTokens()) return st.nextToken();
try {st = new StringTokenizer(br.readLine());}
catch (Exception e) {e.printStackTrace();}
return st.nextToken();
}
public int nextInt() {return Integer.parseInt(next());}
public long nextLong() {return Long.parseLong(next());}
public double nextDouble() {return Double.parseDouble(next());}
public String nextLine() {
String line = "";
if(st.hasMoreTokens()) line = st.nextToken();
else try {return br.readLine();}catch(IOException e){e.printStackTrace();}
while(st.hasMoreTokens()) line += " "+st.nextToken();
return line;
}
}
static int mod = 1000000007;
static long oo = Long.MAX_VALUE;
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt();
String s = sc.nextLine();
int ans = 1;
char bit = s.charAt(0);
for(int i=1;i<n;i++) {
if(s.charAt(i) != bit) {
bit = (bit == '0') ? '1' : '0';
ans++;
}
}
pw.println(Math.min(n, ans + 2));
pw.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;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int result = 1;
for (int i = 1; i < n; i++) {
if (s[i] != s[i - 1]) result++;
}
cout << min(n, result + 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;
long long n, m, k, i, j;
string s;
int main() {
cin >> n;
cin >> s;
for (i = 1; i <= n; i++) {
if (s[i - 1] != s[i]) k++;
}
cout << min(k + 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;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int startidx = 0, endidx = 0, count = 0, flag = 0, flag2 = 0;
while (startidx < n) {
while (s[endidx] == s[startidx]) {
endidx++;
}
if (endidx - startidx >= 3) {
flag = 1;
}
if (endidx - startidx >= 2) {
flag2 += 1;
}
startidx = endidx;
count++;
}
if (flag) {
count += 2;
} else if (flag2 >= 2) {
count += 2;
} else if (flag2 >= 1) {
count++;
}
cout << count << 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 | n = int(input())
x = input()
q = []
for y in x:
q.append(int(y))
# checking the actual score
def get_score(x, y):
# O(n)
score = 1
f = q[x]
for i in range(x + 1, y):
if q[i] != f:
score += 1
f = 1 - f
return score
flip = 1
for i in range(1, n):
if flip == 1:
if q[i] == q[i - 1]: # and (i + 1 == n or q[i] == q[i + 1]):
flip = 0
q[i] = 1 - q[i]
elif flip == 0 and q[i] == q[i-1]:
q[i] = 1 - q[i]
else:
break
print(get_score(0, 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 main() {
long long n, count = 1, flag = 0, i;
cin >> n;
string a;
cin >> a;
for (i = 1; i < a.size(); i++)
if (a[i] != a[i - 1]) count++;
cout << min(count + 2, 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 MAX = 100001;
const int mod = 1e9 + 7;
vector<int> g[MAX];
int par[MAX];
bool vis[MAX];
long long powr(long long a, long long n, long long m) {
long long res = 1;
a = a % m;
while (n) {
if (n & 1) {
res = (res * a) % m;
}
a = (a * a) % m;
n >>= 1;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
string s;
cin >> s;
int ans = 1;
for (int i = 1; i < n; i++) {
if (s[i] != s[i - 1]) ans++;
}
ans = min(ans + 2, n);
cout << 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 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.StringTokenizer;
/**
* Created by yujiahao on 3/17/16.
*/
public class cf_334_c {
private FastScanner in;
private PrintWriter out;
public void solve() throws IOException {
int n = in.nextInt();
String str = in.next();
char[] num = str.toCharArray();
int left = -1;
int right = -1;
// search from left;
for (int i=1; i<num.length; i++){
if (num[i]==num[i-1]){
left = i;
break;
}
}
// search from right;
for (int i=num.length-2; i>=0; i--){
if (num[i]==num[i+1]){
right = i;
break;
}
}
// flip
if (left!=-1){
if (left>right) {
right = n - 1;
}
for (int i=left; i<=right; i++){
if (num[i]=='1')
num[i] = '0';
else
num[i] = '1';
}
}
// calculate 0101, 1010
int oneMax = 0;
int zeroMax = 0;
for (int i=0; i<n; i++){
if (num[i]=='1'){
oneMax = zeroMax+1;
}else{
zeroMax = oneMax+1;
}
}
int res = Math.max(zeroMax, oneMax);
out.print(res);
}
public void run() {
try {
in = new FastScanner();
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class FastScanner {
private BufferedReader br;
private StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() { return new BigInteger(next());}
}
public static void main(String[] arg) {
new cf_334_c().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;
int n, i, j = 0, k = 0, arr[100001] = {0}, l;
void solve() {
char str[100001];
cin >> n;
cin >> str;
l = str[0];
for (i = 0; i < n; i++) {
if (str[i] != l) {
if (j == 0) {
j = 2;
arr[i] = arr[0] = 1;
} else {
j++;
arr[i] = 1;
}
l = 97 - l;
}
}
if (j == 0) {
if (n < 3) {
cout << n << endl;
} else {
cout << 3 << endl;
}
return;
}
l = 0;
for (i = 0; i < n; i++) {
if (arr[i] == 0) {
if (i == 0 && arr[1] == 1) {
l++;
k = 1;
} else if (i == n - 1 && arr[n - 2] == 1) {
l++;
if (l == 1) {
k = 1;
}
if (l > 1) {
k = 2;
break;
}
} else if (arr[i - 1] + arr[i + 1] == 1) {
k = 2;
break;
} else if (arr[i - 1] + arr[i + 1] == 2) {
l++;
if (l == 1) {
k = 1;
}
if (l > 1) {
k = 2;
break;
}
}
}
}
cout << j + k << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
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 | import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
char[] a = in.next().toCharArray();
int[][] r = new int[n][3];
int[] lastpos = new int[2];
Arrays.fill(lastpos, -1);
int res = 0;
for (int i = 0; i < n; i++) {
int prev = 1 - (a[i] - '0');
for (int j = 0; j <= 2; j++) {
r[i][j] = 1;
if (lastpos[prev] != -1) {
r[i][j] = r[lastpos[prev]][j] + 1;
}
if (lastpos[1 - prev] != -1 && j > 0) {
r[i][j] = Math.max(r[lastpos[1 - prev]][j - 1] + 1,
r[i][j]);
}
res = Math.max(r[i][j], res);
}
lastpos[a[i] - '0'] = i;
}
System.out.println(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 | #include <bits/stdc++.h>
using namespace std;
bool isprime(long long int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
vector<long long int> prime;
void sieve(long long int n) {
bool bakh[n + 1];
memset(bakh, true, sizeof(bakh));
for (long long int p = 2; p * p <= n; p++) {
if (bakh[p] == true) {
for (long long int i = p * p; i <= n; i += p) bakh[i] = false;
}
}
for (long long int p = 2; p <= n; p++)
if (bakh[p]) prime.push_back(p);
}
long long int eulertotient(long long int z) {
long long int fac = z;
for (long long int i = 0; prime[i] * prime[i] <= z; i++) {
if (z % prime[i] == 0) {
fac -= (fac / prime[i]);
while (z % prime[i] == 0) z /= prime[i];
}
}
if (z > 1) fac -= (fac / z);
return fac;
}
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long int lcm(long long int a, long long int b) {
long long int g = gcd(a, b);
long long int ans = (a * b) / g;
return ans;
}
long long int modInverse(long long int a, long long int m) {
long long int hvf = gcd(a, m);
if (hvf == 1) return power(a, m - 2, m);
return -1;
}
void multiply(long long int F[2][2], long long int M[2][2]) {
long long int x = F[0][0] * M[0][0] + F[0][1] * M[1][0];
long long int y = F[0][0] * M[0][1] + F[0][1] * M[1][1];
long long int z = F[1][0] * M[0][0] + F[1][1] * M[1][0];
long long int w = F[1][0] * M[0][1] + F[1][1] * M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}
void powermat(long long int F[2][2], long long int n) {
if (n == 0 || n == 1) return;
long long int M[2][2] = {{1, 1}, {1, 0}};
powermat(F, n / 2);
multiply(F, F);
if (n % 2 != 0) multiply(F, M);
}
long long int fib(long long int n) {
long long int F[2][2] = {{1, 1}, {1, 0}};
if (n == 0) return 0;
powermat(F, n - 1);
return F[0][0];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int n;
cin >> n;
string st;
cin >> st;
long long int ans = 0;
char c1 = '0', c2 = '1';
long long int l1 = 0, l2 = 0;
for (long long int i = 0; i < n; i++) {
if (c1 == st[i]) {
l1++;
c1 = 48 + 49 - st[i];
}
if (c2 == st[i]) {
l2++;
c2 = 48 + 49 - st[i];
}
}
long long int mx = 0;
for (long long int i = 0; i < n - 1; i++) {
if (st[i] == st[i + 1]) mx++;
}
if (mx == 1)
cout << 1 + max(l1, l2) << endl;
else if (mx >= 2)
cout << 2 + max(l1, l2) << endl;
else
cout << max(l1, l2) << 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>
using namespace std;
const int maxN(1e5 + 10);
typedef int i_N[maxN];
int N, Gx[maxN][2], Fx[maxN][2][2];
i_N A;
string S;
int main() {
scanf("%d\n", &N);
getline(cin, S);
for (int i = 0; i < (int)S.size(); i++) A[i + 1] = S[i] == '1';
A[0] = A[N + 1] = -1;
for (int i = N; i; i--) {
Gx[i][0] = Gx[i + 1][0];
Gx[i][1] = Gx[i + 1][1];
if (A[i] == 0)
Gx[i][0] = max(Gx[i][0], Gx[i + 1][1] + 1);
else
Gx[i][1] = max(Gx[i][1], Gx[i + 1][0] + 1);
}
int ans(0);
for (int i = 1; i <= N; i++) {
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++) Fx[i][j][k] = Fx[i - 1][j][k];
if (A[i] == 0) {
Fx[i][0][0] = max(Fx[i][0][0], Fx[i - 1][1][0] + 1);
Fx[i][1][1] = max(Fx[i][1][1], max(Fx[i - 1][0][0], Fx[i - 1][0][1]) + 1);
} else {
Fx[i][1][0] = max(Fx[i][1][0], Fx[i - 1][0][0] + 1);
Fx[i][0][1] = max(Fx[i][0][1], max(Fx[i - 1][1][0], Fx[i - 1][1][1]) + 1);
}
ans = max(ans, max(Fx[i][0][1] + Gx[i + 1][1], Fx[i][1][1] + Gx[i + 1][0]));
}
cout << 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 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
public class CFTest80 {
static BufferedReader br;
static LinkedList<String> tokList;
static {
br = new BufferedReader(new InputStreamReader(System.in));
tokList = new LinkedList<String>();
}
public static void main(String[] args) throws IOException {
int n=readInt();
String s=readLine();
br.close();
int num=1;
char p=s.charAt(0);
int sw=0;
for(int i=1;i<n;i++){
char c=s.charAt(i);
if(c!=p){
num++;
}else{
if(sw<2){
num++;
sw++;
}
}
p=c;
}
System.out.println(num);
}
static void print(String s){
System.out.print(s);
}
static void println(String s){
System.out.print(s);
}
static int nextInt() throws IOException {
return Integer.parseInt(nextTok());
}
static long nextLong() throws IOException {
return Long.parseLong(nextTok());
}
static double nextDouble() throws IOException {
return Double.parseDouble(nextTok());
}
static String nextTok() throws IOException {
if (tokList.size() == 0) {
String[] strTok = br.readLine().split(" ");
for (int i = 0; i < strTok.length; i++) {
tokList.add(strTok[i]);
}
}
return tokList.removeFirst();
}
static public String readLine() throws IOException {
return br.readLine();
}
static public long readLong() throws IOException {
return Long.parseLong(br.readLine());
}
static public int readInt() throws IOException {
return Integer.parseInt(br.readLine());
}
static public int[] readIntArr() throws IOException {
String[] str = br.readLine().split(" ");
int arr[] = new int[str.length];
for (int i = 0; i < arr.length; i++)
arr[i] = Integer.parseInt(str[i]);
return arr;
}
static public double[] readDoubleArr() throws IOException {
String[] str = br.readLine().split(" ");
double arr[] = new double[str.length];
for (int i = 0; i < arr.length; i++)
arr[i] = Double.parseDouble(str[i]);
return arr;
}
static public long[] readLongArr() throws IOException {
String[] str = br.readLine().split(" ");
long arr[] = new long[str.length];
for (int i = 0; i < arr.length; i++)
arr[i] = Long.parseLong(str[i]);
return arr;
}
static public double readDouble() throws IOException {
return Double.parseDouble(br.readLine());
}
} | 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.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CODEFORCES604A {
static class Reader
{
BufferedReader r;
StringTokenizer str;
Reader()
{
r=new BufferedReader(new InputStreamReader(System.in));
}
Reader(String fileName) throws FileNotFoundException
{
r=new BufferedReader(new FileReader(fileName));
}
public String getNextToken() throws IOException
{
if(str==null||!str.hasMoreTokens())
{
str=new StringTokenizer(r.readLine());
}
return str.nextToken();
}
public int nextInt() throws IOException
{
return Integer.parseInt(getNextToken());
}
public long nextLong() throws IOException
{
return Long.parseLong(getNextToken());
}
public double nextDouble() throws IOException
{
return Double.parseDouble(getNextToken());
}
public String nextString() throws IOException
{
return getNextToken();
}
public int[] intArray(int n) throws IOException
{
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=nextInt();
return a;
}
public long[] longArray(int n) throws IOException
{
long a[]=new long[n];
for(int i=0;i<n;i++)
a[i]=nextLong();
return a;
}
public String[] stringArray(int n) throws IOException
{
String a[]=new String[n];
for(int i=0;i<n;i++)
a[i]=nextString();
return a;
}
public long gcd(long a, long b) {
if(b == 0){
return a;
}
return gcd(b, a%b);
}
}
public static void main(String args[]) throws IOException{
Reader r=new Reader();
PrintWriter pr=new PrintWriter(System.out,false);
//C. Alternative Thinking
int len=r.nextInt();
String str=r.nextString();
int tmp=1;
for(int a=1;a<len;a++)
{
if(str.charAt(a)!=str.charAt(a-1)) tmp++;
}
pr.print(Math.min(tmp+2,len));
pr.flush();
pr.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 | n , s = int(input()), input()
ans = 1
for i in range (1,n):
ans += (s[i] != s[i-1])
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;
char T[100000 + 1];
int main() {
ios::sync_with_stdio(0);
cin >> n >> T;
for (int a = 0; a < n; ++a) T[a] -= '0';
int ret1 = 0, ret2 = 0, i1 = 0, i2 = 1;
for (int a = 0; a < n; ++a) {
if (T[a] == i1) {
++ret1;
i1 = !i1;
}
if (T[a] == i2) {
++ret2;
i2 = !i2;
}
}
cout << min(max(ret1, ret2) + 2, 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 main() {
int n;
int inv = 1;
char c[100005];
scanf("%d", &n);
scanf("%s", c);
if (n == 1) {
printf("1");
return 0;
}
int p = 0;
for (int i = 0; i < n - 1; i++) {
inv += (c[i] != c[i + 1]);
p += (c[i] == c[i + 1]);
}
inv += max(2 * (p >= 2), 1 * (p >= 1));
printf("%d", max(inv, 2));
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.