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 | /* package codechef; // 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 Codechef
{
static int solve(int idx,int flip,int stop,String s,char prev,int dp[][][][]){
if(idx==s.length()) return 0;
int z=0;
if(dp[idx][flip][stop][prev=='1'?1:0]!=-1) return dp[idx][flip][stop][prev=='1'?1:0];
if(prev=='0'&&s.charAt(idx)=='1'){
if(flip==1&&stop==0)z=1+solve(idx+1,flip,1,s,s.charAt(idx),dp);
else z=1+solve(idx+1,flip,stop,s,s.charAt(idx),dp);}
else if(prev=='1'&&s.charAt(idx)=='0'){
if(flip==1&&stop==0) z=1+solve(idx+1,flip,1,s,s.charAt(idx),dp);
else z=1+solve(idx+1,flip,stop,s,s.charAt(idx),dp);
}
else {
if(flip==1&&stop==0) z=solve(idx+1,flip,1,s,prev,dp);
z=solve(idx+1,flip,stop,s,prev,dp);}
int y=0;
if((flip==1&&stop==0)||(flip==0)){
if(prev=='0'&&s.charAt(idx)=='0'){
//if(flip==0) y=1+solve(idx+1,1,stop,s,'1');
y=1+solve(idx+1,1,stop,s,'1',dp);
}
else if(prev=='1'&&s.charAt(idx)=='1'){
y=1+solve(idx+1,1,stop,s,'0',dp);
}
else {
y=solve(idx+1,1,stop,s,prev,dp);}
}
dp[idx][flip][stop][prev=='1'?1:0]= Math.max(z,y);
return dp[idx][flip][stop][prev=='1'?1:0];
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String s=sc.next();
int l;
int dp[][][][]=new int[n][2][2][2];
for(int i=0;i<n;i++){
for(int j=0;j<2;j++){
for(int k=0;k<2;k++){
for(int u=0;u<2;u++){
dp[i][j][k][u]=-1;
}
}
}
}
if(s.charAt(0)=='0') l=solve(0,0,0,s,'1',dp);
else l=solve(0,0,0,s,'0',dp);
System.out.println(l);
}
}
| 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")
#pragma GCC target("avx,avx2,fma")
bool prime[100005];
void Sieve(long long int n) {
memset(prime, true, sizeof(prime));
for (long long int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (long long int i = p * p; i <= n; i += p) prime[i] = false;
}
}
}
long long int LAS(long long int arr[], long long int n) {
long long int inc = 1;
long long int dec = 1;
for (long long int i = 1; i < n; i++) {
if (arr[i] > arr[i - 1]) {
inc = dec + 1;
} else if (arr[i] < arr[i - 1]) {
dec = inc + 1;
}
}
return max(inc, dec);
}
void solve() {
long long int n;
cin >> n;
string s;
cin >> s;
long long int x = 0, y = 0;
for (auto i : s) {
if (i == '1')
x++;
else
y++;
}
if (x == 0 || y == 0) {
if (n > 2)
cout << 3 << '\n';
else
cout << n << '\n';
return;
}
vector<char> f;
long long int ans = 0;
long long int zf = 0, of = 0;
for (long long int i = 0; i < n; i++) {
long long int z = 0, o = 0;
while (i < n && f.size() > 0 && s[i] == f.back()) {
i++;
if (s[i] == '0')
z++;
else
o++;
}
if (z + 1 >= 2) zf++;
if (o + 1 >= 2) of++;
if (z + 1 > 2) {
ans += 2;
}
if (o + 1 > 2) ans += 2;
if (i < n) f.push_back(s[i]);
}
long long int size = f.size();
ans += size;
if ((zf >= 1 && of >= 1) || (zf >= 2 || of >= 2)) {
cout << size + 2 << '\n';
} else if (zf >= 1 || of >= 1) {
if ((s[0] == s[1] && s[1] == s[2]) ||
(s[n - 3] == s[n - 2] && s[n - 2] == s[n - 1]))
cout << size + 2 << '\n';
else
cout << size + 1 << '\n';
} else
cout << ans << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long 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;
char s[100005];
int main() {
int a = 0, b = 0, n;
scanf("%d", &n);
scanf("%s", s);
for (int i = 0; i < n; i++) {
if (s[i] == '1')
a = max(a, b + 1);
else
b = max(b, a + 1);
}
printf("%d\n", min(n, max(a, b) + 2));
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int MN = 100005, inf = 1000000005, mod = 1000000007;
const long long INF = 1000000000000000005LL;
int dp[MN][2][3];
char opis[MN];
int main() {
int n;
scanf("%d", &n);
scanf("%s", opis);
for (int i = 1; i <= n; ++i) {
int cur = opis[i - 1] - '0';
dp[i][0][0] = dp[i - 1][0][0];
dp[i][0][1] = dp[i - 1][0][1];
dp[i][0][2] = dp[i - 1][0][2];
dp[i][1][0] = dp[i - 1][1][0];
dp[i][1][1] = dp[i - 1][1][1];
dp[i][1][2] = dp[i - 1][1][2];
dp[i][cur][0] = max(dp[i][cur][0], dp[i - 1][cur ^ 1][0] + 1);
dp[i][cur][1] =
max(dp[i][cur][1], 1 + max(dp[i - 1][cur ^ 1][1], dp[i - 1][cur][0]));
dp[i][cur][2] =
max(dp[i][cur][2], 1 + max(dp[i - 1][cur][1], dp[i - 1][cur ^ 1][2]));
}
int res = 0;
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 3; ++j) res = max(res, dp[n][i][j]);
printf("%d", res);
}
| 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 argc, char const *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
int n;
cin >> n;
string s;
cin >> s;
vector<vector<int>> dp(n, vector<int>(2));
dp[0][(s[0] - '0') % 2] = 1;
for (int i = 1; i < n; i++) {
dp[i][(s[i] - '0') % 2] = dp[i - 1][1 - (s[i] - '0') % 2] + 1;
dp[i][1 - (s[i] - '0') % 2] = dp[i - 1][1 - (s[i] - '0') % 2];
}
int ans = max(dp[n - 1][0], dp[n - 1][1]);
cout << min(n, ans + 2) << '\n';
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author I_love_PloadyFree
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, InputReader in, OutputWriter out) {
in.readInt();
char[] s = in.next().toCharArray();
out.print(countLen(s) + magic(s));
}
private int magic(char[] s) {
int n = s.length;
for (int i = 2; i < n; i++)
if (s[i] == s[i - 1] && s[i - 1] == s[i - 2])
return 2;
List<Integer> ends = new ArrayList<>();
for (int i = 1; i < n; i++)
if (s[i] == s[i - 1])
ends.add(i);
for (int i = 1; i < ends.size(); i++)
if (ends.get(i) - ends.get(i - 1) >= 2) return 2;
if (n >= 2)
if (s[0] == s[1] || s[n - 1] == s[n - 2])
return 1;
for (int i = 1; i < n; i++)
if (s[i] == s[i - 1])
return 1;
return 0;
}
private int countLen(char[] s) {
int count = 0;
int prev = -1;
for (char c : s) {
if (c != prev) {
prev = c;
count++;
}
}
return count;
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c))
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void print(int i) {
writer.print(i);
}
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.util.Scanner;
public class something
{
public static void main(String[]args)
{
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
String a=scan.next();
int smae=0,diff=0;
for(int i=1;i<n;i++)
{
if(a.charAt(i)==a.charAt(i-1))
smae++;
else
diff++;
}
System.out.println(1+diff+Math.min(2,smae));
}
} | 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 oo = 0x3f3f3f3f;
long long fastpower(long long b, long long p) {
long long ans = 1;
while (p) {
if (p % 2) {
ans = (ans * b);
}
b = (b * b);
p /= 2;
}
return ans;
}
string makeitstring(long long n) {
string ans;
while (n) {
long long mod = n % 10;
mod += 48;
char m = mod;
ans = m + ans;
n /= 10;
}
return ans;
}
long long makeitnumber(string s) {
long long ans = 0;
for (long long i = 0; i < s.size(); i++) {
long long num = s[i] - '0';
ans += (num * fastpower(10, (long long)s.size() - i - 1));
}
return ans;
}
bool valid(int x, int y, int n, int m) {
return (x >= 0 && y < m && x < n && y >= 0);
}
double EPS = 0.000000000001;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
int n;
cin >> n;
string s;
cin >> s;
int ans = 1;
bool ch = s[0] - '0';
ch = !ch;
for (int i = 1; i < n; i++) {
if (s[i] - '0' == ch) {
ans++;
ch = !ch;
}
}
cout << min(ans + 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 | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
boolean[] str = new boolean[n];
String string = s.next();
for (int i = 0; i < n; i++) {
char c = string.charAt(i);
str[i] = c == '1';
}
int ds = 0;
boolean c = str[0];
int current = 1;
for (int i = 1; i < str.length; i++) {
if (str[i] != c) {
c = str[i];
current++;
}
if (str[i] == str[i - 1])
ds++;
}
if (ds >= 2) {
current += 2;
} else if (ds == 1) {
current += 1;
}
System.out.println(current);
}
}
| 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.File;
import java.util.Scanner;
import java.util.StringTokenizer;
public class p008 {
public static void main(String args[]) throws Exception {
StringTokenizer stok = new StringTokenizer(new Scanner(System.in).useDelimiter("\\A").next());
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(stok.nextToken());
char[] s = stok.nextToken().toCharArray();
int i=0;
int gc = 0,max = Integer.MIN_VALUE;
int v = 0;
boolean fl = true;
while(i<n) {
int cnt = 1;
while(++i<n &&s[i]==s[i-1]) cnt++;
gc++;
if(cnt>2) v=2;
if(cnt>1) {
if(!fl) v=2; else { fl=false;v=Math.max(v, 1);}
}
max = Math.max(max, cnt);
}
System.out.println((v+gc));
}
} | 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.math.*;
/**
*
* @author sarthak
*/
public class rnd334_C {
static int[] a;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
s.nextLine();
String ip=s.nextLine();
a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ip.charAt(i)-48;
}
int[] dp = new int[n];
dp[0] = 1;
int len1 = (a[0] == 0) ? 0 : 1;
int len0 = (a[0] == 0) ? 1 : 0;
int an = 1;
for (int i = 1; i < n; i++) {
if (a[i] == 0) {
dp[i] = len1 + 1;
len0 = dp[i];
} else {
dp[i] = len0 + 1;
len1 = dp[i];
}
an = Math.max(an, dp[i]);
}
int cnt0=0;
for(int i=0;i<n-1;i++)
{
if(a[i]==a[i+1]&&a[i]==0)
cnt0++;
}
int cnt1=0;
for(int i=0;i<n-1;i++)
{
if(a[i]==a[i+1]&&a[i]==1)
cnt1++;
}
if(cnt1==0 && cnt0==0)
an=an;
else if(cnt1>=2 || cnt0>=2 || (cnt1+cnt0>=2))
an=an+2;
else an++;
System.out.println(an);
}
}
| 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;
public class C {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int n = Integer.parseInt(br.readLine());
char[] x = br.readLine().toCharArray();
int count = 0;
int prev = -1;
for (int i = 0; i < n; i++) {
if (x[i] != prev) {
prev = x[i];
count++;
}
}
out.println(Math.min(n, count + 2));
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 main() {
int n;
while (scanf("%d", &n) != EOF) {
string s;
cin >> s;
int res1 = 1;
for (int i = 1; i < s.size(); i++) {
if (s[i] != s[i - 1]) res1++;
}
int res2 = 1;
int r = 1;
for (int i = 1; i < s.size(); i++) {
if (s[i] != s[i - 1]) {
r = max(r, res2);
} else
res2++;
}
r = max(r, res2);
if (r == 1) r = 0;
if (r == 2) r = 1;
cout << res1 + min(2, r) << endl;
}
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.stream.IntStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author I_love_PloadyFree
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
char[] s = in.next().toCharArray();
long len = IntStream.range(1, n).filter(i -> s[i] != s[i - 1]).count() + 1;
long add = Math.min(2, IntStream.range(1, n).filter(i -> s[i] == s[i - 1]).count());
out.print(len + add);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c))
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void print(long i) {
writer.print(i);
}
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class hacker49 {
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
OutputStream outputStream =System.out;
PrintWriter out =new PrintWriter(outputStream);
FastReader s=new FastReader();
int n=s.nextInt();
String str=s.next();
int[] a=new int[n+1];
for(int i=1;i<=n;i++) {
a[i]=(int)(str.charAt(i-1)-'0');
}
int c=1;
for(int i=2;i<=n;i++) {
if(a[i]!=a[i-1]) {
c++;
}
}
int k=-1;
int u=-1;
for(int i=1;i<n;i++) {
if(a[i]==a[i+1]) {
k=i;
break;
}
}
for(int i=n;i>=2;i--) {
if(a[i]==a[i-1]) {
u=i;
break;
}
}
if(k!=-1 && u!=-1 && u-k>1) {
c+=2;
}else if(k!=-1 || u!=-1) {
c++;
}
System.out.println(c);
out.close();
}
static void sf(long[] arr){
int n = arr.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n-i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static HashMap<Integer,pair> e=new HashMap<>();
static ArrayList<Integer>[] f1=new ArrayList[200001];
static PriorityQueue<pair> q=new PriorityQueue<>();
static ArrayList<pair>[] f=new ArrayList[200001];
static class pair implements Comparable<pair>{
private int a;
private int b;
pair(int a,int b){
this.a=a;
this.b=b;
}
public int compareTo(pair o) {
return Integer.compare(o.b, this.b);
}
//
}
static int[] col=new int[200001];
static int[] subtree=new int[200001];
static int[] vis=new int[200001];
static int dfs(int node) {
vis[node]=1;
int c=0;
for(int i=0;i<f[node].size();i++) {
if(vis[f[node].get(i).a]==0) {
c+=dfs(f[node].get(i).a);
}
}
subtree[node]+=c;
subtree[node]+=col[node];
return subtree[node];
}
public static int upper_bound(long[] a ,long x,int n) {
int l=-1;
int r=n;
while(r>l+1) {
int mid=(l+r)/2;
if(a[mid]<x) {
l=mid;
}else {
r=mid;
}
}
return r;
}
public static int lower_bound(long[] a ,long x,int n) {
int l=-1;
int r=n;
while(r>l+1) {
int mid=(l+r)/2;
if(a[mid]<=x) {
l=mid;
}else {
r=mid;
}
}
return l;
}
public static long[] merge_sort(long[] A, int start, int end) {
if (end > start) {
int mid = (end + start) / 2;
long[] v = merge_sort(A, start, mid);
long[] o = merge_sort(A, mid + 1, end);
return (merge(v, o));
} else {
long[] y = new long[1];
y[0] = A[start];
return y;
}
}
public static long[] merge(long a[], long b[]) {
// int count=0;
long[] temp = new long[a.length + b.length];
int m = a.length;
int n = b.length;
int i = 0;
int j = 0;
int c = 0;
while (i < m && j < n) {
if (a[i] < b[j]) {
temp[c++] = a[i++];
} else {
temp[c++] = b[j++];
}
}
while (i < m) {
temp[c++] = a[i++];
}
while (j < n) {
temp[c++] = b[j++];
}
return temp;
}
public static int[] Create(int[] a,int n) {
int[] b=new int[n+1];
for(int i=1;i<=n;i++) {
int j=i;
int h=a[i];
while(i<=n) {
b[i]+=h;
i=get_next(i);
}
i=j;
}
return b;
}
public static int get_next(int a) {
return a+(a&-a);
}
public static int get_parent(int a) {
return a-(a&-a);
}
public static int query_1(int[] b,int index) {
int sum=0;
if(index<=0) {
return 0;
}
while(index>0) {
sum+=b[index];
index=get_parent(index);
}
return sum;
}
public static int query(int[] b,int n,int l,int r) {
int sum=0;
sum+=query_1(b,r);
sum-=query_1(b,l-1);
return sum;
}
public static void update(int[] a,int[] b,int n,int index,int val) {
int diff=val-a[index];
a[index]+=diff;
while(index<=n) {
b[index]+=diff;
index=get_next(index);
}
}
// public static void Create(int[] a,pair[] segtree,int low,int high,int pos) {
// if(low>high) {
// return;
// }
// if(low==high) {
// segtree[pos].b.add(a[low]);
// return ;
// }
// int mid=(low+high)/2;
// Create(a,segtree,low,mid,2*pos);
// Create(a,segtree,mid+1,high,2*pos+1);
// segtree[pos].b.addAll(segtree[2*pos].b);
// segtree[pos].b.addAll(segtree[2*pos+1].b);
// }
// public static void update(pair[] segtree,int low,int high,int index,int pos,int val,int prev) {
// if(index>high || index<low) {
// return ;
// }
// if(low==high && low==index) {
// segtree[pos].b.remove(prev);
// segtree[pos].b.add(val);
// return;
// }
// int mid=(high+low)/2;
// update(segtree,low,mid,index,2*pos,val,prev);
// update(segtree,mid+1,high,index,2*pos+1,val,prev);
// segtree[pos].b.clear();
// segtree[pos].b.addAll(segtree[2*pos].b);
// segtree[pos].b.addAll(segtree[2*pos+1].b);
//
// }
// public static pair query(pair[] segtree,int low,int high,int qlow,int qhigh ,int pos) {
// if(low>=qlow && high<=qhigh) {
// return segtree[pos];
// }
// if(qhigh<low || qlow>high) {
// return new pair();
// }
// int mid=(low+high)/2;
// pair a1=query(segtree,low,mid,qlow,qhigh,2*pos);
// pair a2=query(segtree,mid+1,high,qlow,qhigh,2*pos+1);
// pair a3=new pair();
// a3.b.addAll(a1.b);
// a3.b.addAll(a2.b);
// return a3;
//
// }
//
// public static int nextPowerOf2(int n)
// {
// n--;
// n |= n >> 1;
// n |= n >> 2;
// n |= n >> 4;
// n |= n >> 8;
// n |= n >> 16;
// n++;
// return n;
// }
//
//// static class pair implements Comparable<pair>{
//// private int a;
//// private long b;
////// private long c;
//// pair(int a,long b){
//// this.a=a;
//// this.b=b;
////// this.c=c;
//// }
//// public int compareTo(pair o) {
////// if(this.a!=o.a) {
////// return Long.compare(this.a, o.a);
////// }else {
//// return Long.compare(o.b,this.b);
////// }
//// }
//// }
// static class pair implements Comparable<pair>{
// private int a;
// private int b;
// pair(int a,int b){
//// this.b=new HashSet<>();
// this.a=a;
// this.b=b;
// }
// public int compareTo(pair o) {
// return Integer.compare(this.b, o.b);
// }
// }
public static int lower_bound(ArrayList<Long> a ,int n,long x) {
int l=0;
int r=n;
while(r>l+1) {
int mid=(l+r)/2;
if(a.get(mid)<=x) {
l=mid;
}else {
r=mid;
}
}
return l;
}
public static int[] is_prime=new int[1000001];
public static ArrayList<Long> primes=new ArrayList<>();
public static void sieve() {
long maxN=1000000;
for(long i=1;i<=maxN;i++) {
is_prime[(int) i]=1;
}
is_prime[0]=0;
is_prime[1]=0;
for(long i=2;i*i<=maxN;i++) {
if(is_prime[(int) i]==1) {
// primes.add((int) i);
for(long j=i*i;j<=maxN;j+=i) {
is_prime[(int) j]=0;
}
}
}
for(long i=0;i<=maxN;i++) {
if(is_prime[(int) i]==1) {
primes.add(i);
}
}
}
// public static pair[] merge_sort(pair[] A, int start, int end) {
// if (end > start) {
// int mid = (end + start) / 2;
// pair[] v = merge_sort(A, start, mid);
// pair[] o = merge_sort(A, mid + 1, end);
// return (merge(v, o));
// } else {
// pair[] y = new pair[1];
// y[0] = A[start];
// return y;
// }
// }
// public static pair[] merge(pair a[], pair b[]) {
// pair[] temp = new pair[a.length + b.length];
// int m = a.length;
// int n = b.length;
// int i = 0;
// int j = 0;
// int c = 0;
// while (i < m && j < n) {
// if (a[i].b >= b[j].b) {
// temp[c++] = a[i++];
//
// } else {
// temp[c++] = b[j++];
// }
// }
// while (i < m) {
// temp[c++] = a[i++];
// }
// while (j < n) {
// temp[c++] = b[j++];
// }
// return temp;
// }
// public static long im(long a) {
// return binary_exponentiation_1(a,mod-2)%mod;
// }
public static double bn1(double a,double n) {
double res=1;
while(n>0) {
if(n%2!=0) {
// res=((res)%(1000000007) * (a)%(1000000007))%(1000000007);
res=(res*a);
n--;
}else {
// a=((a)%(1000000007) *(a)%(1000000007))%(1000000007);
a=(a*a);
n/=2;
}
}
return (res);
}
public static long bn(long a,long n) {
long res=1;
while(n>0) {
if(n%2!=0) {
res=res*a;
n--;
}else {
a*=a;
n/=2;
}
}
return res;
}
public static long[] fac=new long[100001];
public static void find_factorial() {
fac[0]=1;
fac[1]=1;
for(int i=2;i<=100000;i++) {
fac[i]=(fac[i-1]*i)%(mod);
}
}
static long mod=1000000007;
public static long GCD(long a,long b) {
if(b==(long)0) {
return a;
}
return GCD(b , a%b);
}
static long c=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 | n = int(input())
A = list(map(int, ' '.join(input()).split()))
res = 1
for i in range(1, len(A)):
if A[i] != A[i - 1]:
res += 1
print(min(res + 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 ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
const int Sz = 1e5 + 3;
const ll MOD = 1e9 + 7;
const int oo = 1e9;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
string s;
int n;
cin >> n;
cin >> s;
int ans = 3;
for (int i = (1); i <= (n - 1); ++i) {
ans += (s[i] != s[i - 1]);
}
cout << min(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 | #! /bin/python
n = int(input())
tab = str(input())
d = 1
tmp = 1
changes = 1
for i in range(1, n):
if tab[i] != tab[i - 1]:
changes += 1
print(min(changes + 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>
int max(int a, int b) { return a > b ? a : b; }
char s[101000];
int a[101000], g[101000][2], f[101000][2], F[101000][2];
int main() {
int n;
scanf("%d", &n);
scanf("%s", s);
for (int i = 0; i < n; i++) a[i] = s[i] - '0';
g[0][1 - a[0]] = 1;
f[0][0] = 1;
f[0][1] = 1;
F[0][a[0]] = 1;
for (int i = 1; i < n; i++) {
F[i][0] = F[i - 1][0];
F[i][1] = F[i - 1][1];
F[i][a[i]] = max(F[i][a[i]], F[i - 1][1 - a[i]] + 1);
}
for (int i = 1; i < n; i++) {
if (a[i] == 1) {
g[i][0] = max(g[i - 1][1] + 1, g[i - 1][0]);
g[i][0] = max(F[i - 1][0], g[i][0]);
g[i][0] = max(F[i - 1][1] + 1, g[i][0]);
g[i][1] = g[i - 1][1];
g[i][1] = max(g[i][1], F[i - 1][1]);
} else {
g[i][0] = g[i - 1][0];
g[i][1] = max(g[i - 1][0] + 1, g[i - 1][1]);
g[i][0] = max(g[i][0], F[i - 1][0]);
g[i][1] = max(g[i][1], max(F[i - 1][1], F[i - 1][0] + 1));
}
if (a[i] == 1) {
f[i][0] = max(f[i - 1][0], g[i][0]);
f[i][1] = max(f[i - 1][0] + 1, g[i][1]);
f[i][1] = max(f[i][1], f[i - 1][1]);
} else {
f[i][0] = max(f[i - 1][1] + 1, g[i][0]);
f[i][1] = max(f[i - 1][1], g[i][1]);
f[i][0] = max(f[i][0], f[i - 1][0]);
}
}
printf("%d\n", max(f[n - 1][0], f[n - 1][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 | #include <bits/stdc++.h>
using namespace std;
int Calcul(string s) {
char e;
int r = 1;
e = s[0];
for (int i = 1; i < s.size(); i++) {
if (e != s[i]) {
r++;
}
e = s[i];
}
return r;
}
int main() {
int n, a;
cin >> n;
string s;
cin >> s;
if (n == 1) {
cout << 1;
} else {
if (n == 2) {
cout << 2;
}
if (n > 2) {
a = Calcul(s);
if (a == n)
cout << a;
else if (a == n - 1)
cout << a + 1;
else
cout << a + 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()
li = [s[0]]
for i in range(1, n):
if s[i] != li[-1]:
li.append(s[i])
print(min(n, len(li) + 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, a = 1;
char t, l;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
scanf("%d", &n);
scanf("%c", &t);
scanf("%c", &l);
for (int i = 1; i < n; i++) {
scanf("%c", &t);
a += (t != l);
l = t;
}
printf("%d", min(a + 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 | n=input()
s=raw_input()
s+='*'
count=0
for i in range(0,n):
if(s[i]!=s[i+1]):
count+=1
#print count
if(("000" in s) or ("111" in s) or ("1100" in s) or ("0011" in s)):
count+=2
elif(s.count("11")>1 or s.count("00")>1):
count+=2
elif(s=="1101" or s=="0010" or s=="1011" or s=="0100"):
count+=1
elif(s.count("11")>0 and s.count("00")>0):
count+=2
elif(s.count("00")==1 or s.count("11")==1):
count+=1
print count | 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 main() {
int n, ans = 1;
string s;
cin >> n >> s;
for (int i = 1; i < n; i++)
if (s[i] != s[i - 1]) ans++;
cout << min(n, ans + 2);
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int s = 0, t = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') t = -1;
c = getchar();
}
while (isdigit(c)) s = s * 10 + c - 48, c = getchar();
return s * t;
}
const int N = 1e5 + 5;
int n, now, pre, c1, c2, c3, ans, f;
char c[N];
int main() {
n = read();
scanf("%s", c + 1);
now = c[1] - '0', c1 = 1, c2 = 0, ans = 1;
for (register int i = 2; i <= n; ++i) {
if (c[i] - '0' != now)
ans++, now ^= 1, c2 = c1, c1 = 1;
else
c1++;
if (c1 > 2) f = 2;
if (c2 >= 2 && c1 >= 2) f = 2;
if (c1 == 2) c3++;
}
if (ans == n)
cout << ans << endl;
else if (f)
cout << ans + f << endl;
else if (c3 >= 2)
cout << ans + 2 << endl;
else
cout << ans + 1 << endl;
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | n = int(input())
s = input()
res = 1
for i in range(n-1):
if s[i] != s[i+1]:
res += 1
if res == n:
print(res)
elif res == n-1:
print(res+1)
else:
print(res + 2)
| PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.awt.*;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Abc {
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
int n=sc.nextInt();
String s=sc.next();
int zero=0;int one=0;
int max=1;
int cnt2=0;
int c=1;
for (int i=0;i<n;i++){
if (zero%2==0 && s.charAt(i)=='0'){
zero++;
}
else if (zero%2==1 && s.charAt(i)=='1'){
zero++;
}
if (one%2==0 && s.charAt(i)=='1'){
one++;
}
else if (one%2==1 && s.charAt(i)=='0'){
one++;
}
if (i+1<n && s.charAt(i)==s.charAt(i+1)){
max=Math.max(max,c+1);
c++;
}else c=1;
if (c==2)cnt2++;
}
if (max>=3 || cnt2>1) System.out.println(Math.max(zero,one)+2);
else if (cnt2==1) System.out.println(Math.max(zero,one)+1);
else System.out.println(Math.max(zero,one));
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, L = 0, R = 0;
string s;
cin >> n >> s;
for (int i = 0; i < n; i++) (s[i] != s[i - 1]) ? ++L : ++R;
cout << L + min(2, R);
}
| 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(raw_input())
s = raw_input()
length = len(s)
cnt = 0
for i in range(0 , length - 1):
if(s[i] == s[i+1]):
cnt += 1
print min(n , n - cnt + 2)
| PYTHON |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class icpc
{
public static void main(String[] args) throws IOException
{
// Reader in = new Reader();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
String s = in.readLine();
int[] A = new int[s.length()];
for (int i=0;i<s.length();i++) A[i] = s.charAt(i) - '0';
ArrayList<Integer> B = new ArrayList<>();
ArrayList<Integer> C = new ArrayList<>();
for (int i=0;i<n;i++)
{
int j = i + 1;
while (j < n && A[j] == A[i]) j++;
B.add(A[i]);
C.add(j - i);
i = j - 1;
}
int ansYet = B.size();
int[] count = new int[4];
for (int i=0;i<B.size();i++)
{
if (C.get(i) > 2)
count[3]++;
else if (C.get(i) > 1)
count[2]++;
else
count[1]++;
}
if (count[3] >= 1)
ansYet += 2;
else if (count[2] >= 2)
ansYet += 2;
else if (count[2] == 1)
ansYet += 1;
System.out.println(ansYet);
}
}
class DSU
{
int[] parent;
int[] size;
//Pass number of total nodes as parameter to the constructor
DSU(int n)
{
this.parent = new int[n];
this.size = new int[n];
Arrays.fill(parent, -1);
}
public void makeSet(int v)
{
parent[v] = v;
size[v] = 1;
}
public int findSet(int v)
{
if (v == parent[v]) return v;
return parent[v] = findSet(parent[v]);
}
public void unionSets(int a, int b)
{
a = findSet(a);
b = findSet(b);
if (a != b)
{
if (size[a] < size[b])
{
int temp = a;
a = b;
b = temp;
}
parent[b] = a;
size[a] += size[b];
}
}
}
class FastFourierTransform
{
private void fft(double[] a, double[] b, boolean invert)
{
int count = a.length;
for (int i = 1, j = 0; i < count; i++)
{
int bit = count >> 1;
for (; j >= bit; bit >>= 1)
j -= bit;
j += bit;
if (i < j)
{
double temp = a[i];
a[i] = a[j];
a[j] = temp;
temp = b[i];
b[i] = b[j];
b[j] = temp;
}
}
for (int len = 2; len <= count; len <<= 1)
{
int halfLen = len >> 1;
double angle = 2 * Math.PI / len;
if (invert)
angle = -angle;
double wLenA = Math.cos(angle);
double wLenB = Math.sin(angle);
for (int i = 0; i < count; i += len)
{
double wA = 1;
double wB = 0;
for (int j = 0; j < halfLen; j++)
{
double uA = a[i + j];
double uB = b[i + j];
double vA = a[i + j + halfLen] * wA - b[i + j + halfLen] * wB;
double vB = a[i + j + halfLen] * wB + b[i + j + halfLen] * wA;
a[i + j] = uA + vA;
b[i + j] = uB + vB;
a[i + j + halfLen] = uA - vA;
b[i + j + halfLen] = uB - vB;
double nextWA = wA * wLenA - wB * wLenB;
wB = wA * wLenB + wB * wLenA;
wA = nextWA;
}
}
}
if (invert)
{
for (int i = 0; i < count; i++)
{
a[i] /= count;
b[i] /= count;
}
}
}
public long[] multiply(long[] a, long[] b)
{
int resultSize = Integer.highestOneBit(Math.max(a.length, b.length) - 1) << 2;
resultSize = Math.max(resultSize, 1);
double[] aReal = new double[resultSize];
double[] aImaginary = new double[resultSize];
double[] bReal = new double[resultSize];
double[] bImaginary = new double[resultSize];
for (int i = 0; i < a.length; i++)
aReal[i] = a[i];
for (int i = 0; i < b.length; i++)
bReal[i] = b[i];
fft(aReal, aImaginary, false);
fft(bReal, bImaginary, false);
for (int i = 0; i < resultSize; i++)
{
double real = aReal[i] * bReal[i] - aImaginary[i] * bImaginary[i];
aImaginary[i] = aImaginary[i] * bReal[i] + bImaginary[i] * aReal[i];
aReal[i] = real;
}
fft(aReal, aImaginary, true);
long[] result = new long[resultSize];
for (int i = 0; i < resultSize; i++)
result[i] = Math.round(aReal[i]);
return result;
}
}
class NumberTheory
{
public boolean isPrime(long n)
{
if(n < 2)
return false;
for(long x = 2;x * x <= n;x++)
{
if(n % x == 0)
return false;
}
return true;
}
public ArrayList<Long> primeFactorisation(long n)
{
ArrayList<Long> f = new ArrayList<>();
for(long x=2;x * x <= n;x++)
{
while(n % x == 0)
{
f.add(x);
n /= x;
}
}
if(n > 1)
f.add(n);
return f;
}
public int[] sieveOfEratosthenes(int n)
{
//Returns an array with the smallest prime factor for each number and primes marked as 0
int[] sieve = new int[n + 1];
for(int x=2;x * x <= n;x++)
{
if(sieve[x] != 0)
continue;
for(int u=x*x;u<=n;u+=x)
{
if(sieve[u] == 0)
{
sieve[u] = x;
}
}
}
return sieve;
}
public long gcd(long a, long b)
{
if(b == 0)
return a;
return gcd(b, a % b);
}
public long phi(long n)
{
double result = n;
for(long p=2;p*p<=n;p++)
{
if(n % p == 0)
{
while (n % p == 0)
n /= p;
result *= (1.0 - (1.0 / (double)p));
}
}
if(n > 1)
result *= (1.0 - (1.0 / (double)n));
return (long)result;
}
public Name extendedEuclid(long a, long b)
{
if(b == 0)
return new Name(a, 1, 0);
Name n1 = extendedEuclid(b, a % b);
Name n2 = new Name(n1.d, n1.y, n1.x - (long)Math.floor((double)a / b) * n1.y);
return n2;
}
public long modularExponentiation(long a, long b, long n)
{
long d = 1L;
String bString = Long.toBinaryString(b);
for(int i=0;i<bString.length();i++)
{
d = (d * d) % n;
if(bString.charAt(i) == '1')
d = (d * a) % n;
}
return d;
}
}
class Name
{
long d;
long x;
long y;
public Name(long d, long x, long y)
{
this.d = d;
this.x = x;
this.y = y;
}
}
class SuffixArray
{
int ALPHABET_SZ = 256, N;
int[] T, lcp, sa, sa2, rank, tmp, c;
public SuffixArray(String str)
{
this(toIntArray(str));
}
private static int[] toIntArray(String s)
{
int[] text = new int[s.length()];
for (int i = 0; i < s.length(); i++) text[i] = s.charAt(i);
return text;
}
public SuffixArray(int[] text)
{
T = text;
N = text.length;
sa = new int[N];
sa2 = new int[N];
rank = new int[N];
c = new int[Math.max(ALPHABET_SZ, N)];
construct();
kasai();
}
private void construct()
{
int i, p, r;
for (i = 0; i < N; ++i) c[rank[i] = T[i]]++;
for (i = 1; i < ALPHABET_SZ; ++i) c[i] += c[i - 1];
for (i = N - 1; i >= 0; --i) sa[--c[T[i]]] = i;
for (p = 1; p < N; p <<= 1)
{
for (r = 0, i = N - p; i < N; ++i) sa2[r++] = i;
for (i = 0; i < N; ++i) if (sa[i] >= p) sa2[r++] = sa[i] - p;
Arrays.fill(c, 0, ALPHABET_SZ, 0);
for (i = 0; i < N; ++i) c[rank[i]]++;
for (i = 1; i < ALPHABET_SZ; ++i) c[i] += c[i - 1];
for (i = N - 1; i >= 0; --i) sa[--c[rank[sa2[i]]]] = sa2[i];
for (sa2[sa[0]] = r = 0, i = 1; i < N; ++i)
{
if (!(rank[sa[i - 1]] == rank[sa[i]]
&& sa[i - 1] + p < N
&& sa[i] + p < N
&& rank[sa[i - 1] + p] == rank[sa[i] + p])) r++;
sa2[sa[i]] = r;
}
tmp = rank;
rank = sa2;
sa2 = tmp;
if (r == N - 1) break;
ALPHABET_SZ = r + 1;
}
}
private void kasai()
{
lcp = new int[N];
int[] inv = new int[N];
for (int i = 0; i < N; i++) inv[sa[i]] = i;
for (int i = 0, len = 0; i < N; i++)
{
if (inv[i] > 0)
{
int k = sa[inv[i] - 1];
while ((i + len < N) && (k + len < N) && T[i + len] == T[k + len]) len++;
lcp[inv[i] - 1] = len;
if (len > 0) len--;
}
}
}
}
class ZAlgorithm
{
public int[] calculateZ(char input[])
{
int Z[] = new int[input.length];
int left = 0;
int right = 0;
for(int k = 1; k < input.length; k++) {
if(k > right) {
left = right = k;
while(right < input.length && input[right] == input[right - left]) {
right++;
}
Z[k] = right - left;
right--;
} else {
//we are operating inside box
int k1 = k - left;
//if value does not stretches till right bound then just copy it.
if(Z[k1] < right - k + 1) {
Z[k] = Z[k1];
} else { //otherwise try to see if there are more matches.
left = k;
while(right < input.length && input[right] == input[right - left]) {
right++;
}
Z[k] = right - left;
right--;
}
}
}
return Z;
}
public ArrayList<Integer> matchPattern(char text[], char pattern[])
{
char newString[] = new char[text.length + pattern.length + 1];
int i = 0;
for(char ch : pattern) {
newString[i] = ch;
i++;
}
newString[i] = '$';
i++;
for(char ch : text) {
newString[i] = ch;
i++;
}
ArrayList<Integer> result = new ArrayList<>();
int Z[] = calculateZ(newString);
for(i = 0; i < Z.length ; i++) {
if(Z[i] == pattern.length) {
result.add(i - pattern.length - 1);
}
}
return result;
}
}
class KMPAlgorithm
{
public int[] computeTemporalArray(char[] pattern)
{
int[] lps = new int[pattern.length];
int index = 0;
for(int i=1;i<pattern.length;)
{
if(pattern[i] == pattern[index])
{
lps[i] = index + 1;
index++;
i++;
}
else
{
if(index != 0)
{
index = lps[index - 1];
}
else
{
lps[i] = 0;
i++;
}
}
}
return lps;
}
public ArrayList<Integer> KMPMatcher(char[] text, char[] pattern)
{
int[] lps = computeTemporalArray(pattern);
int j = 0;
int i = 0;
int n = text.length;
int m = pattern.length;
ArrayList<Integer> indices = new ArrayList<>();
while(i < n)
{
if(pattern[j] == text[i])
{
i++;
j++;
}
if(j == m)
{
indices.add(i - j);
j = lps[j - 1];
}
else if(i < n && pattern[j] != text[i])
{
if(j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
return indices;
}
}
class Hashing
{
public long[] computePowers(long p, int n, long m)
{
long[] powers = new long[n];
powers[0] = 1;
for(int i=1;i<n;i++)
{
powers[i] = (powers[i - 1] * p) % m;
}
return powers;
}
public long computeHash(String s)
{
long p = 31;
long m = 1_000_000_009;
long hashValue = 0L;
long[] powers = computePowers(p, s.length(), m);
for(int i=0;i<s.length();i++)
{
char ch = s.charAt(i);
hashValue = (hashValue + (ch - 'a' + 1) * powers[i]) % m;
}
return hashValue;
}
}
class BasicFunctions
{
public long min(long[] A)
{
long min = Long.MAX_VALUE;
for(int i=0;i<A.length;i++)
{
min = Math.min(min, A[i]);
}
return min;
}
public long max(long[] A)
{
long max = Long.MAX_VALUE;
for(int i=0;i<A.length;i++)
{
max = Math.max(max, A[i]);
}
return max;
}
}
class MergeSortInt
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r) {
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int[n1];
int R[] = new int[n2];
/*Copy data to temp arrays*/
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r) {
if (l < r) {
// Find the middle point
int m = (l + r) / 2;
// Sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
}
class MergeSortLong
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(long arr[], int l, int m, int r) {
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
long L[] = new long[n1];
long R[] = new long[n2];
/*Copy data to temp arrays*/
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
void sort(long arr[], int l, int r) {
if (l < r) {
// Find the middle point
int m = (l + r) / 2;
// Sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
}
class Node
{
int x;
int y;
public Node(int x, int y)
{
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object ob)
{
if(ob == null)
return false;
if(!(ob instanceof Node))
return false;
if(ob == this)
return true;
Node obj = (Node)ob;
if(this.x == obj.x && this.y == obj.y)
return true;
return false;
}
@Override
public int hashCode()
{
return (int)this.x;
}
}
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;
}
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();
}
}
class FenwickTree
{
public void update(long[] fenwickTree,long delta,int index)
{
index += 1;
while(index < fenwickTree.length)
{
fenwickTree[index] += delta;
index = index + (index & (-index));
}
}
public long prefixSum(long[] fenwickTree,int index)
{
long sum = 0L;
index += 1;
while(index > 0)
{
sum += fenwickTree[index];
index -= (index & (-index));
}
return sum;
}
}
class SegmentTree
{
public int nextPowerOfTwo(int num)
{
if(num == 0)
return 1;
if(num > 0 && (num & (num - 1)) == 0)
return num;
while((num &(num - 1)) > 0)
{
num = num & (num - 1);
}
return num << 1;
}
public int[] createSegmentTree(int[] input)
{
int np2 = nextPowerOfTwo(input.length);
int[] segmentTree = new int[np2 * 2 - 1];
for(int i=0;i<segmentTree.length;i++)
segmentTree[i] = Integer.MIN_VALUE;
constructSegmentTree(segmentTree,input,0,input.length-1,0);
return segmentTree;
}
private void constructSegmentTree(int[] segmentTree,int[] input,int low,int high,int pos)
{
if(low == high)
{
segmentTree[pos] = input[low];
return;
}
int mid = (low + high)/ 2;
constructSegmentTree(segmentTree,input,low,mid,2*pos + 1);
constructSegmentTree(segmentTree,input,mid+1,high,2*pos + 2);
segmentTree[pos] = Math.max(segmentTree[2*pos + 1],segmentTree[2*pos + 2]);
}
public int rangeMinimumQuery(int []segmentTree,int qlow,int qhigh,int len)
{
return rangeMinimumQuery(segmentTree,0,len-1,qlow,qhigh,0);
}
private int rangeMinimumQuery(int segmentTree[],int low,int high,int qlow,int qhigh,int pos)
{
if(qlow <= low && qhigh >= high){
return segmentTree[pos];
}
if(qlow > high || qhigh < low){
return Integer.MIN_VALUE;
}
int mid = (low+high)/2;
return Math.max(rangeMinimumQuery(segmentTree, low, mid, qlow, qhigh, 2 * pos + 1),
rangeMinimumQuery(segmentTree, mid + 1, high, qlow, qhigh, 2 * pos + 2));
}
}
class Trie
{
private class TrieNode
{
Map<Character, TrieNode> children;
boolean endOfWord;
public TrieNode()
{
children = new HashMap<>();
endOfWord = false;
}
}
private final TrieNode root;
public Trie()
{
root = new TrieNode();
}
public void insert(String word)
{
TrieNode current = root;
for (int i = 0; i < word.length(); i++)
{
char ch = word.charAt(i);
TrieNode node = current.children.get(ch);
if (node == null)
{
node = new TrieNode();
current.children.put(ch, node);
}
current = node;
}
current.endOfWord = true;
}
public boolean search(String word)
{
TrieNode current = root;
for (int i = 0; i < word.length(); i++)
{
char ch = word.charAt(i);
TrieNode node = current.children.get(ch);
if (node == null)
{
return false;
}
current = node;
}
return current.endOfWord;
}
public void delete(String word)
{
delete(root, word, 0);
}
private boolean delete(TrieNode current, String word, int index)
{
if (index == word.length())
{
if (!current.endOfWord)
{
return false;
}
current.endOfWord = false;
return current.children.size() == 0;
}
char ch = word.charAt(index);
TrieNode node = current.children.get(ch);
if (node == null)
{
return false;
}
boolean shouldDeleteCurrentNode = delete(node, word, index + 1);
if (shouldDeleteCurrentNode)
{
current.children.remove(ch);
return current.children.size() == 0;
}
return false;
}
}
class SegmentTreeLazy
{
public int nextPowerOfTwo(int num)
{
if(num == 0)
return 1;
if(num > 0 && (num & (num - 1)) == 0)
return num;
while((num &(num - 1)) > 0)
{
num = num & (num - 1);
}
return num << 1;
}
public int[] createSegmentTree(int input[])
{
int nextPowOfTwo = nextPowerOfTwo(input.length);
int segmentTree[] = new int[nextPowOfTwo*2 -1];
for(int i=0; i < segmentTree.length; i++){
segmentTree[i] = Integer.MAX_VALUE;
}
constructMinSegmentTree(segmentTree, input, 0, input.length - 1, 0);
return segmentTree;
}
private void constructMinSegmentTree(int segmentTree[], int input[], int low, int high,int pos)
{
if(low == high)
{
segmentTree[pos] = input[low];
return;
}
int mid = (low + high)/2;
constructMinSegmentTree(segmentTree, input, low, mid, 2 * pos + 1);
constructMinSegmentTree(segmentTree, input, mid + 1, high, 2 * pos + 2);
segmentTree[pos] = Math.min(segmentTree[2*pos+1], segmentTree[2*pos+2]);
}
public void updateSegmentTreeRangeLazy(int input[], int segmentTree[], int lazy[], int startRange, int endRange, int delta)
{
updateSegmentTreeRangeLazy(segmentTree, lazy, startRange, endRange, delta, 0, input.length - 1, 0);
}
private void updateSegmentTreeRangeLazy(int segmentTree[], int lazy[], int startRange, int endRange, int delta, int low, int high, int pos)
{
if(low > high)
{
return;
}
if (lazy[pos] != 0)
{
segmentTree[pos] += lazy[pos];
if (low != high)
{
lazy[2 * pos + 1] += lazy[pos];
lazy[2 * pos + 2] += lazy[pos];
}
lazy[pos] = 0;
}
if(startRange > high || endRange < low)
{
return;
}
if(startRange <= low && endRange >= high)
{
segmentTree[pos] += delta;
if(low != high) {
lazy[2*pos + 1] += delta;
lazy[2*pos + 2] += delta;
}
return;
}
int mid = (low + high)/2;
updateSegmentTreeRangeLazy(segmentTree, lazy, startRange, endRange, delta, low, mid, 2*pos+1);
updateSegmentTreeRangeLazy(segmentTree, lazy, startRange, endRange, delta, mid+1, high, 2*pos+2);
segmentTree[pos] = Math.min(segmentTree[2*pos + 1], segmentTree[2*pos + 2]);
}
public int rangeMinimumQueryLazy(int segmentTree[], int lazy[], int qlow, int qhigh, int len)
{
return rangeMinimumQueryLazy(segmentTree, lazy, qlow, qhigh, 0, len - 1, 0);
}
private int rangeMinimumQueryLazy(int segmentTree[], int lazy[], int qlow, int qhigh, int low, int high, int pos)
{
if(low > high)
{
return Integer.MAX_VALUE;
}
if (lazy[pos] != 0)
{
segmentTree[pos] += lazy[pos];
if (low != high)
{
lazy[2 * pos + 1] += lazy[pos];
lazy[2 * pos + 2] += lazy[pos];
}
lazy[pos] = 0;
}
if(qlow > high || qhigh < low)
{
return Integer.MAX_VALUE;
}
if(qlow <= low && qhigh >= high)
{
return segmentTree[pos];
}
int mid = (low+high)/2;
return Math.min(rangeMinimumQueryLazy(segmentTree, lazy, qlow, qhigh, low, mid, 2 * pos + 1), rangeMinimumQueryLazy(segmentTree, lazy, qlow, qhigh, mid + 1, high, 2 * pos + 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=input()
r=map(int,list(raw_input()))
dp0=0
dp1=0
for i in r:
if i:
dp1=dp0+1
else:
dp0=dp1+1
ans=max(dp0,dp1)
print min(n,ans+2)
| PYTHON |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | n = int(raw_input())
s = raw_input().strip()
p = '.'
k = c = 0
for x in s:
k += p != x
c += p == x
p = x
if c > 2:
c = 2
print k + c
| 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.util.*;
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.math.MathContext;
import java.text.DecimalFormat;
import java.text.Format;
import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import java.awt.List;
public class Main {
public static void main(String[] args){
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
//public ArrayList<Integer> ar;
public boolean[] bs;
//public int[] ll;
// GCD && LCM
long gcd(long a ,long b){return b == 0 ? a : gcd(b , a % b);}
long lcm(long a , long b){return a*(b/gcd(a, b));}
// Sieve of erothenese
void sieve(int UB){
//ar = new ArrayList();
bs = new boolean[UB+1];
//ll = new int[UB + 1];
bs[0] = bs[1] = true;
//ll[1] =ll[0]= 1;
for(int i = 2; i*i <= UB; i++){
if(!bs[i]){
//ll[i] = i;
for(int j = i*i ; j <= UB ; j += i){
bs[j] = true;
//if(ll[j] == 0)
// ll[j] = i;
}
}
}
// for(int i = 2; i <= UB; i++){
// if(!bs[i])ar.add(i);
//}
}
// REverse a String
String rev(String s){
return new StringBuilder(s).reverse().toString();
}
// all pair shortest path
public void ShortestPath(int n , int[][] grid){
for(int k = 1; k <= n; k++){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
grid[i][j] = min(grid[i][j] , grid[i][k] + grid[k][j]);
}
}
}
//return grid;
}
/* SOLUTION IS RIGHT HERE */
public void solve(int testNumber, InputReader in, PrintWriter out) {
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// Scanner sc = new Scanner(System.in);
int n = in.nextInt();
char[] ss = in.next().toCharArray();
int res = 1;
for(int i = 1; i < n; i++){
if(ss[i] != ss[i-1])
res += 1;
}
out.println(min(res + 2, n));
}
}
// union
static class dsu{
int[] union , size;
public dsu(int n){
union = new int[n+1];
size = new int[n+1];
for(int i = 0; i <= n; i++){
union[i] = i;
size[i] = 1;
}
}
public int root(int s){
while(s != union[s])
s = union[s];
return s;
}
public boolean connected(int a , int b){
return root(a) == root(b);
}
public void union(int a, int b){
int ea = root(a);
int eb = root(b);
if(a == b);
else if(size[ea] > size[eb]){
union[eb] = ea;
size[ea] += size[eb];
}else{
union[ea] = eb;
size[eb] += size[ea];
}
}
}
// pair class
static class pair implements Comparable<pair>{
int first , second ;
public pair(){this.first = 0; this.second = 0;}
public pair(int f , int s){
this.first = f;
this.second = s;
}
@Override
public int compareTo(pair one) {
if(this.first < one.first)
return -1;
if(this.first > one.first)
return 1;
if(this.second < one.second)
return -1;
if(this.second > one.second)
return 1;
return 0;
}
}
// ||||||| INPUT READER ||||||||
static class InputReader {
private byte[] buf = new byte[2048];
private int index;
private int total;
private InputStream in;
public InputReader(InputStream stream){
in = stream;
}
public int scan(){
if(total == -1)
throw new InputMismatchException();
if(index >= total){
index = 0;
try{
total = in.read(buf);
}catch(IOException e){
throw new InputMismatchException();
}
if(total <= 0)
return -1;
}
return buf[index++];
}
public long scanlong(){
long integer = 0;
int n = scan();
while(isWhiteSpace(n))
n = scan();
int neg = 1;
if(n == '-'){
neg = -1;
n = scan();
}
while(!isWhiteSpace(n)){
if(n >= '0' && n <= '9'){
integer *= 10;
integer += n - '0';
n = scan();
}
else throw new InputMismatchException();
}
return neg*integer;
}
private int scanInt() {
int integer = 0;
int n = scan();
while(isWhiteSpace(n))
n = scan();
int neg = 1;
if(n == '-'){
neg = -1;
n = scan();
}
while(!isWhiteSpace(n)){
if(n >= '0' && n <= '9'){
integer *= 10;
integer += n - '0';
n = scan();
}
else throw new InputMismatchException();
}
return neg*integer;
}
public double scandouble(){
double doubll = 0;
int n = scan();
int neg = 1;
while(isWhiteSpace(n))
n = scan();
if(n == '-'){
neg = -1;
n = scan();
}
while(!isWhiteSpace(n) && n != '.'){
if(n >= '0' && n <= '9'){
doubll *= 10;
doubll += n - '0';
n = scan();
}
}
if(n == '.'){
n = scan();
double temp = 1;
while(!isWhiteSpace(n)){
if(n >= '0' && n <= '9'){
temp /= 10;
doubll += (n - '0')*temp;
n = scan();
}
}
}
return neg*doubll;
}
private float scanfloat() {
float doubll = 0;
int n = scan();
int neg = 1;
while(isWhiteSpace(n))
n = scan();
if(n == '-'){
neg = -1;
n = scan();
}
while(!isWhiteSpace(n) && n != '.'){
if(n >= '0' && n <= '9'){
doubll *= 10;
doubll += n - '0';
n = scan();
}
}
if(n == '.'){
n = scan();
double temp = 1;
while(!isWhiteSpace(n)){
if(n >= '0' && n <= '9'){
temp /= 10;
doubll += (n - '0')*temp;
n = scan();
}
}
}
return neg*doubll;
}
public String scanstring(){
StringBuilder sb = new StringBuilder();
int n = scan();
while(isWhiteSpace(n))
n = scan();
while(!isWhiteSpace(n)){
sb.append((char)n);
n = scan();
}
return sb.toString();
}
public boolean isWhiteSpace(int n){
if(n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1)
return true;
return false;
}
/// Input module
public int nextInt(){
return scanInt();
}
public long nextLong(){
return scanlong();
}
public double nextDouble(){
return scandouble();
}
public float nextFloat(){
return scanfloat();
}
public String next(){
return scanstring();
}
public int[] readIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = nextInt();
}
return array;
}
}
}
| 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;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
string s;
cin >> s;
char prev = s[0];
long long cnt = 1;
for (long long i = 1; i < n; i++) {
if (s[i] != prev) {
cnt++;
}
prev = s[i];
}
cout << min(n, cnt + 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.util.*;
public class test1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String res=sc.next();
int score=1,c=0,k=0;
for(int i=0;i<n-1;i++){
if(res.charAt(i)!=res.charAt(i+1)){
score++;
if(c>=2){
score+=2;
c=0;
k++;
}
}else if(k==0){
c++;
}
}
if(k==0){
if(c<=2){
System.out.println(score+c);
}else{
System.out.println(score+2);
}
}else{
System.out.println(score);
}
}
} | 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 N = 100005;
const long long MOD = 1e9 + 7;
const double eps = 1e-9;
int n;
string s;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
cin >> s;
int ans = 0;
for (int i = 1; i < n; i++) ans += (s[i] != s[i - 1]);
cout << min(n, ans + 3) << "\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 sys
#FUCK
n = int(input())
s = input()
p = s[0]
ans = 1
for i in range(1,n):
if s[i] != p:
ans += 1
p = s[i]
if (0 <= s.find("00") < s.rfind("00")) or (0 <= s.find("11") < s.rfind("11")) or (0 <= s.find("11") < s.rfind("00")) or (0 <= s.find("00") < s.rfind("11")):
ans += 2
elif s.find("00") >= 0 or s.find("11") >= 0:
ans += 1
print(ans) | PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | //import com.sun.org.apache.xpath.internal.operations.String;
import java.io.*;
import java.util.*;
public class scratch_25 {
//static long count=0;
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() throws IOException {
while (!tokenizer.hasMoreTokens()) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
}
public static class Graph{
public static class Vertex{
HashMap<Integer,Integer> nb= new HashMap<>(); // for neighbours of each vertex
}
public static HashMap<Integer,Vertex> vt; // for vertices(all)
public Graph(){
vt= new HashMap<>();
}
public static int numVer(){
return vt.size();
}
public static boolean contVer(int ver){
return vt.containsKey(ver);
}
public static void addVer(int ver){
Vertex v= new Vertex();
vt.put(ver,v);
}
public static int numEdg(){
int count=0;
ArrayList<Integer> vrtc= new ArrayList<>(vt.keySet());
for (int i = 0; i <vrtc.size() ; i++) {
count+=(vt.get(vrtc.get(i))).nb.size();
}
return count/2;
}
public static boolean contEdg(int ver1, int ver2){
if(vt.get(ver1)==null || vt.get(ver2)==null){
return false;
}
Vertex v= vt.get(ver1);
if(v.nb.containsKey(ver2)){
return true;
}
return false;
}
public static void addEdge(int ver1, int ver2, int weight){
if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){
return;
}
Vertex v1= vt.get(ver1);
Vertex v2= vt.get(ver2);
v1.nb.put(ver2,weight); // if previously there is an edge, then this replaces that edge
v2.nb.put(ver1,weight);
}
public static void delEdge(int ver1, int ver2){
if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){
return;
}
vt.get(ver1).nb.remove(ver2);
vt.get(ver2).nb.remove(ver1);
}
public static void delVer(int ver){
if(!vt.containsKey(ver)){
return;
}
Vertex v1= vt.get(ver);
ArrayList<Integer> arr= new ArrayList<>(v1.nb.keySet());
for (int i = 0; i <arr.size() ; i++) {
int s= arr.get(i);
vt.get(s).nb.remove(ver);
}
vt.remove(ver);
}
public class Pair{
int vname;
ArrayList<Integer> psf= new ArrayList<>(); // path so far
int dis;
int col;
}
public HashMap<Integer,Integer> bfs(int src){
HashMap<Integer, Boolean> prcd= new HashMap<>(); // jo jo ho chuke hain unke liye
HashMap<Integer, Integer> ans= new HashMap<>();
LinkedList<Pair> queue= new LinkedList<>(); // for bfs queue
Pair strtp= new Pair();
strtp.vname= src;
ArrayList<Integer> ar= new ArrayList<>();
ar.add(src);
strtp.psf= ar;
strtp.dis=0;
queue.addLast(strtp);
while(!queue.isEmpty()){
Pair rp = queue.removeFirst();
if(prcd.containsKey(rp.vname)){
continue;
}
prcd.put(rp.vname,true);
ans.put(rp.vname,rp.dis);
// if(contEdg(rp.vname,dst)){
// return true;
// }
Vertex a= vt.get(rp.vname);
ArrayList<Integer> arr= new ArrayList<>(a.nb.keySet());
for (int i = 0; i <arr.size() ; i++) {
int s= arr.get(i);
if(!prcd.containsKey(s)){
Pair np = new Pair();
np.vname= s;
np.dis+=rp.dis+a.nb.get(s);
np.psf.addAll(rp.psf);
np.psf.add(s);
// np.psf.add(s);
// np.psf= rp.psf+" "+s;
queue.addLast(np);
}
}
}
return ans;
// return false;
}
public HashMap<Integer,Integer> dfs(int src){
HashMap<Integer, Boolean> prcd= new HashMap<>(); // jo jo ho chuke hain unke liye
HashMap<Integer, Integer> ans= new HashMap<>();
LinkedList<Pair> stack= new LinkedList<>(); // for bfs queue
Pair strtp= new Pair();
strtp.vname= src;
ArrayList<Integer> ar= new ArrayList<>();
ar.add(src);
strtp.psf= ar;
strtp.dis=0;
stack.addFirst(strtp);
while(!stack.isEmpty()){
Pair rp = stack.removeFirst();
if(prcd.containsKey(rp.vname)){
continue;
}
prcd.put(rp.vname,true);
ans.put(rp.vname,rp.dis);
// if(contEdg(rp.vname,dst)){
// return true;
// }
Vertex a= vt.get(rp.vname);
ArrayList<Integer> arr= new ArrayList<>(a.nb.keySet());
for (int i = 0; i <arr.size() ; i++) {
int s= arr.get(i);
if(!prcd.containsKey(s)){
Pair np = new Pair();
np.vname= s;
np.dis+=rp.dis+a.nb.get(s);
np.psf.addAll(rp.psf);
np.psf.add(s);
// np.psf.add(s);
// np.psf= rp.psf+" "+s;
stack.addFirst(np);
}
}
}
return ans;
// return false;
}
public boolean isCycle(){
HashMap<Integer, Boolean> prcd= new HashMap<>(); // jo jo ho chuke hain unke liye
// HashMap<Integer, Integer> ans= new HashMap<>();
LinkedList<Pair> queue= new LinkedList<>(); // for bfs queue
ArrayList<Integer> keys = new ArrayList<>(vt.keySet());
for (int i = 0; i <keys.size(); i++) {
int cur= keys.get(i);
if(prcd.containsKey(cur)){
continue;
}
Pair sp = new Pair();
sp.vname= cur;
ArrayList<Integer> as= new ArrayList<>();
as.add(cur);
sp.psf= as;
queue.addLast(sp);
while(!queue.isEmpty()){
Pair rp= queue.removeFirst();
if(prcd.containsKey(rp.vname)){
return true;
}
prcd.put(rp.vname,true);
Vertex v1= vt.get(rp.vname);
ArrayList<Integer> nbrs= new ArrayList<>(v1.nb.keySet());
for (int j = 0; j <nbrs.size() ; j++) {
int u= nbrs.get(j);
Pair np= new Pair();
np.vname= u;
queue.addLast(np);
}
}
}
return false;
}
public ArrayList<ArrayList<Integer>> genConnctdComp(){
HashMap<Integer, Boolean> prcd= new HashMap<>(); // jo jo ho chuke hain unke liye
ArrayList<ArrayList<Integer>> ans= new ArrayList<>();
// HashMap<Integer, Integer> ans= new HashMap<>();
LinkedList<Pair> queue= new LinkedList<>(); // for bfs queue
// int con=-1;
ArrayList<Integer> keys = new ArrayList<>(vt.keySet());
for (int i = 0; i <keys.size(); i++) {
int cur= keys.get(i);
if(prcd.containsKey(cur)){
//return true;
continue;
}
ArrayList<Integer> fu= new ArrayList<>();
fu.add(cur);
Pair sp = new Pair();
sp.vname= cur;
ArrayList<Integer> as= new ArrayList<>();
as.add(cur);
sp.psf= as;
queue.addLast(sp);
while(!queue.isEmpty()){
Pair rp= queue.removeFirst();
if(prcd.containsKey(rp.vname)){
//return true;
continue;
}
prcd.put(rp.vname,true);
fu.add(cur);
Vertex v1= vt.get(rp.vname);
ArrayList<Integer> nbrs= new ArrayList<>();
for (int j = 0; j <nbrs.size() ; j++) {
int u= nbrs.get(j);
Pair np= new Pair();
np.vname= u;
queue.addLast(np);
}
}
ans.add(fu);
}
//return false;
return ans;
}
public boolean isBip(int src){ // only for connected graph
HashMap<Integer,Integer> clr= new HashMap<>();
// colors are 1 and -1
LinkedList<Integer>q = new LinkedList<Integer>();
clr.put(src,1);
q.add(src);
while(!q.isEmpty()){
int u = q.getFirst();
q.pop();
ArrayList<Integer> arr= new ArrayList<>(vt.keySet());
for (int i = 0; i <arr.size() ; ++i) {
int x= arr.get(i);
if(vt.get(u).nb.containsKey(x) && !clr.containsKey(x)){
if(clr.get(u)==1){
clr.put(x,-1);
}
else{
clr.put(x,1);
}
q.push(x);
}
else if(vt.get(u).nb.containsKey(x) && (clr.get(x)==clr.get(u))){
return false;
}
}
}
return true;
}
public static void printGr() {
ArrayList<Integer> arr= new ArrayList<>(vt.keySet());
for (int i = 0; i <arr.size() ; i++) {
int ver= arr.get(i);
Vertex v1= vt.get(ver);
ArrayList<Integer> arr1= new ArrayList<>(v1.nb.keySet());
for (int j = 0; j <arr1.size() ; j++) {
System.out.println(ver+"-"+arr1.get(j)+":"+v1.nb.get(arr1.get(j)));
}
}
}
}
static class Cus implements Comparable<Cus>{
int size;
int mon;
int num;
public Cus(int size,int mon,int num){
this.size=size;
this.mon=mon;
this.num=num;
}
@Override
public int compareTo(Cus o){
if(this.mon!=o.mon){
return this.mon-o.mon;}
else{
return o.size-this.size;
}
}
}
static class Table implements Comparable<Table>{
int size;
int num;
public Table(int size,int num){
this.size=size;
this.num=num;
}
@Override
public int compareTo(Table o){
return this.size-o.size;
}
}
public static void main(String[] args) throws IOException {
Reader.init(System.in);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int n= Reader.nextInt();
char arr[]= Reader.next().toCharArray();
int ans=0;
for (int i = 1; i <n ; i++) {
if(arr[i]!=arr[i-1]){
ans++;
}
}
out.append(Math.min(ans+3,n)+"");
out.flush();
out.close();
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd1(long a, long b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
//count++;
if (a > b){
return gcd(a-b, b);
}
return gcd(a, b-a);
}
// method to return LCM of two numbers
static long lcm(long a, long b)
{
return (a*b)/gcd(a, b);
}
// Driver method
static int partition(long arr[],long arr1[], int low, int high)
{
long pivot = arr[high];
int i = (low-1); // index of smaller element
for (int j=low; j<high; j++)
{
// If current element is smaller than the pivot
if (arr[j] < pivot)
{
i++;
// swap arr[i] and arr[j]
long temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
long temp1=arr1[i];
arr1[i]=arr1[j];
arr1[j]=temp1;
}
}
// swap arr[i+1] and arr[high] (or pivot)
long temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
long temp1= arr1[i+1];
arr1[i+1]=arr1[high];
arr1[high]= temp1;
return i+1;
}
/* The main function that implements QuickSort()
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
static void sort(long arr[],long arr1[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[pi] is
now at right place */
int pi = partition(arr,arr1, low, high);
// Recursively sort elements before
// partition and after partition
sort(arr,arr1, low, pi-1);
sort(arr,arr1, pi+1, high);
}
}
public static ArrayList<Integer> Sieve(int n) {
boolean arr[]= new boolean [n+1];
Arrays.fill(arr,true);
arr[0]=false;
arr[1]=false;
for (int i = 2; i*i <=n ; i++) {
if(arr[i]==true){
for (int j = 2; j <=n/i ; j++) {
int u= i*j;
arr[u]=false;
}}
}
ArrayList<Integer> ans= new ArrayList<>();
for (int i = 0; i <n+1 ; i++) {
if(arr[i]){
ans.add(i);
}
}
return ans;
}
} | JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int solve() {
int n, altern{0}, consec{0};
cin >> n;
string votes;
cin >> votes;
for (int i = 0; i < n - 1; ++i) {
if (votes[i] != votes[i + 1])
++altern;
else
++consec;
}
return 1 + altern + min(consec, 2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << solve() << 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 n;
string s;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> s;
int ans = 1, boz = 0, now = 1, cnt = 0;
for (int i = 1; i < s.length(); i++)
if (s[i] != s[i - 1])
ans++, boz = max(boz, now), now = 1;
else
now++;
boz = max(boz, now), now = 1;
for (int i = 1; i < s.length(); i++)
if (s[i] != s[i - 1]) {
if (now == boz) cnt++;
now = 1;
} else
now++;
if (now == boz) cnt++;
if ((boz == 2 && cnt > 1) || (boz > 2))
cout << ans + 2 << endl;
else if (boz == 2 and cnt == 1)
cout << ans + 1 << endl;
else if (boz == 1)
cout << ans << endl;
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
string s = "";
long long arr[1000005] = {0};
long long solve(string st) {
long long sz = st.size();
long long t = 1;
long long x = st[0] - '0';
for (int i = 1; i < sz; i++) {
if (st[i] == 1 - x + '0') {
t++;
x = 1 - x;
}
}
return t;
}
int main() {
ios ::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, m, k;
cin >> n;
cin >> s;
int sz = s.size();
if (sz == 1) {
cout << 1 << endl;
return 0;
}
if (sz == 2) {
cout << 2 << endl;
return 0;
}
long long ans = 0;
long long t = 0;
ans = solve(s);
for (int i = 1; i < int(s.size() - 1); i++) {
if (s[i - 1] == '0' && s[i] == '0' && s[i + 1] == '0') {
cout << ans + 2 << endl;
return 0;
}
if (s[i - 1] == '1' && s[i] == '1' && s[i + 1] == '1') {
cout << ans + 2 << endl;
return 0;
}
}
int x = 0;
for (int i = 3; i < sz; i++) {
if (s[i - 3] == '0' && s[i - 2] == '0' && s[i - 1] == '1' && s[i] == '1') {
cout << ans + 2 << endl;
return 0;
}
if (s[i - 3] == '1' && s[i - 2] == '1' && s[i - 1] == '0' && s[i] == '0') {
cout << ans + 2 << endl;
return 0;
}
if ((s[i - 3] == '1' && s[i - 2] == '0' && s[i - 1] == '0' &&
s[i] == '1') ||
(s[i - 3] == '0' && s[i - 2] == '1' && s[i - 1] == '1' &&
s[i] == '0')) {
x++;
if (x == 2) {
cout << ans + 2 << endl;
return 0;
}
}
}
if (((s[0] == '0' && s[1] == '0') || (s[0] == '1' && s[1] == '1')) &&
((s[sz - 2] == '0' && s[sz - 1] == '0') ||
(s[sz - 2] == '1' && s[sz - 1] == '1'))) {
cout << ans + 2 << endl;
return 0;
}
if ((s[0] == '0' && s[1] == '0') || (s[0] == '1' && s[1] == '1') ||
(s[sz - 2] == '0' && s[sz - 1] == '0') ||
(s[sz - 2] == '1' && s[sz - 1] == '1')) {
cout << ans + 1 + x << endl;
return 0;
}
cout << ans + x << 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() {
int n;
cin >> n;
string s;
cin >> s;
int t = 1;
for (int i = 1; i < n; i++) {
if (s[i] != s[i - 1]) t += 1;
}
cout << min(t + 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() {
long long int n, m, i, j, c = 0, d = 0, ans = 0;
string s;
cin >> n;
cin >> s;
c = 0;
for (i = 0; i < n; i++) {
if (s[i] == '1' && c % 2 == 0) {
c++;
}
if (s[i] == '0' && c % 2 == 1) c++;
}
for (i = 0; i < n; i++) {
if (s[i] == '0' && d % 2 == 0) {
d++;
}
if (s[i] == '1' && d % 2 == 1) {
d++;
}
}
ans = max(c, d);
ans = min(ans + 2, n);
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 | #include <bits/stdc++.h>
using namespace std;
int main() {
string str;
int n;
cin >> n >> str;
int a[n];
for (int i = 0; i < n; ++i) {
a[i] = str[i] - '0';
}
int f[2][2][3];
memset(f, 0, sizeof(f));
auto update = [](int &a, int b) -> void {
if (a < b) a = b;
};
int u = 0, v = 1;
for (int i = 0; i < n; ++i) {
swap(u, v);
memcpy(f[u], f[v], sizeof(f[u]));
update(f[u][a[i]][0], f[v][a[i] ^ 1][0] + 1);
update(f[u][a[i] ^ 1][1], max(f[v][a[i]][1], f[v][a[i]][0]) + 1);
update(f[u][a[i]][2], max(f[v][a[i] ^ 1][2], f[v][a[i] ^ 1][1]) + 1);
}
int ans = 0;
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 3; ++j) ans = max(ans, f[u][i][j]);
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())]
dp = [0,0]
dp[s[0]]=1
for i in range(1,n):
j = s[i]
if j==0:
x = max(dp[1]+1, dp[0])
y = dp[1]
else:
x = dp[0]
y = max(dp[0]+1, dp[1])
#print(x,y)
dp[0],dp[1] = x,y
ans = max(dp[0],dp[1])
print(min(n, ans+2)) | PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
cin.ignore();
string s;
cin >> s;
int las = 1;
for (int i = 1; i < n; i++) {
las += s[i] != s[i - 1];
}
int ans = min(n, las + 2);
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 | import java.io.*;
import java.util.*;
import java.math.*;
public class Div2_334_C {
public static int getLongestConsec(char [] c) {
int curr = 1;
int max = 0;
for(int i = 1;i < c.length;i++) {
if(c[i] == c[i-1]) {
curr++;
max++;
} else {
curr = 1;
}
}
return max;
}
public static int getLongestAlternating(char [] c) {
int max0s = 1, max1s = 1;
if(c[0] == '1')
max0s = 0;
else
max1s = 0;
for (int i = 1; i < c.length; i++) {
if(c[i] == '1') {
max1s = Math.max(max1s, max0s+1);
} else {
max0s = Math.max(max1s+1, max0s);
}
}
return Math.max(max1s, max0s);
}
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(bis));
int n = Integer.parseInt(br.readLine().trim());
String s = br.readLine().trim();
char [] c = s.toCharArray();
int lConsec = getLongestConsec(c);
int lAlt = getLongestAlternating(c);
if(lConsec >= 2)
lAlt += 2;
else if(lAlt != n)
lAlt++;
System.out.println(lAlt);
}
} | 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;
using ll = long long;
using llu = unsigned long long;
using ld = long double;
const double PI = acos(-1.0), g = 9.80665;
const ll inf = 1e18 + 505;
ll i, j, k, n, m, cnt;
bool is;
void solve(void) {
cin >> n;
string s;
cin >> s;
ll len = 1;
for (i = 1; i < n; i++)
if (s[i] != s[i - 1]) len++;
cout << min(len + 2ll, n);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int testcase = 1;
while (testcase--) {
solve();
}
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int N, res = 1;
string S;
int main() {
cin >> N >> S;
for (int i = 1; i < N; i++) {
res += (S[i] != S[i - 1]);
}
cout << min(res + 2, N) << '\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 n, r = 1;
string s;
int main() {
cin >> n >> s;
for (int i = 1; i < n; i++) {
r += s[i] != s[i - 1];
}
cout << min(r + 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 |
n= int(raw_input())
x = raw_input()
f = 0
last = x[0]
ans = 1
for i in x[1:]:
if last!=i:
ans+=1
last=i
else:
f=min(f+1,2)
ans = f+ans
print ans
| 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 sys
def answer(n, s):
ctr = 0
cstates = 1
for i in range(1, n):
if s[i] == s[i-1]:
ctr += 1
else:
cstates += 1
if ctr >= 2:
cstates += 2
elif ctr == 1:
cstates += 1
return cstates
def main():
n = int(sys.stdin.readline())
s = sys.stdin.readline().rstrip()
print(answer(n, s))
return
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 | n, a = input(), raw_input()
print min(a.count("10") + a.count("01") + 3, n)
| PYTHON |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.StringTokenizer;
public class A {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
String s = in.nextLine();
int res = 1;
for (int i = 1;i < s.length(); i++)
res += (s.charAt(i) != s.charAt(i - 1) ? 1 : 0);
System.out.println(Math.min(n, res + 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(input())
l=input()
cnt =''
i=0
from itertools import groupby
for k,g in groupby(l):
cnt+=k
cnt=len(cnt)
print(min(cnt+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 | n = int(raw_input())
s = raw_input().strip()
l = []
for i in range(len(s)):
l.append(0)
l[0]=1
for i in range(1,len(s)):
if s[i]==s[i-1]:
l[i]=l[i-1]
else:
l[i] = l[i-1] + 1
print min(l[-1]+2,n)
| PYTHON |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | /*
Keep solving problems.
*/
import java.util.*;
import java.io.*;
public class CFA {
BufferedReader br;
PrintWriter out;
StringTokenizer st;
boolean eof;
final long MOD = 1000L * 1000L * 1000L + 7;
int[] dx = {0, -1, 0, 1};
int[] dy = {1, 0, -1, 0};
void solve() throws IOException {
int n = nextInt();
char[] arr = nextString().toCharArray();
int num0 = 0;
int num1 = 0;
int cnt = 0;
for (int i = 1; i < n; i++) {
if (arr[i] == '0' && arr[i - 1] == '0') {
num0++;
}
else if (arr[i] == '1' && arr[i - 1] == '1') {
num1++;
}
else {
cnt++;
}
}
int res = 1 + cnt + Math.min(2, num0 + num1);
out(res);
}
void shuffle(long[] a) {
int n = a.length;
for(int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n - i));
long tmp = a[i];
a[i] = a[r];
a[r] = tmp;
}
}
long gcd(long a, long b) {
while(a != 0 && b != 0) {
long c = b;
b = a % b;
a = c;
}
return a + b;
}
private void outln(Object o) {
out.println(o);
}
private void out(Object o) {
out.print(o);
}
public CFA() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
public static void main(String[] args) throws IOException {
new CFA();
}
public long[] nextLongArr(int n) throws IOException{
long[] res = new long[n];
for(int i = 0; i < n; i++)
res[i] = nextLong();
return res;
}
public int[] nextIntArr(int n) throws IOException {
int[] res = new int[n];
for(int i = 0; i < n; i++)
res[i] = nextInt();
return res;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return null;
}
}
return st.nextToken();
}
public String nextString() {
try {
return br.readLine();
} catch (IOException e) {
eof = true;
return null;
}
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(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 | x = int(input())
y = input()
if x <= 3:
print(x)
quit()
num1 = 0
num0 = 0
for i in y:
if i == '1':
num1 = max(num1, num0+1)
else:
num0 = max(num0, num1+1)
maxx = max(num1, num0)
if '11' not in y and '00' not in y:
print(maxx)
quit()
print(min(maxx+2, x)) | PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void checkMax(T &a, T b) {
a = a > b ? a : b;
}
template <typename T>
inline void checkMin(T &a, T b) {
a = a < b ? a : b;
}
const double PI = acos(-1.0);
const double eps = 1e-8;
int n;
char str[100005];
int from[100005][2], to[100005][2];
vector<int> vec;
void work() {
scanf("%s", str + 1);
vec.clear();
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (i == 1) {
cnt++;
} else {
if (str[i] == str[i - 1]) {
cnt++;
} else {
vec.push_back(cnt);
cnt = 1;
}
}
}
if (cnt) {
vec.push_back(cnt);
}
if (n == 1) {
cout << 1 << endl;
return;
}
int ans = vec.size();
int two = 0;
for (int i = 0; i < vec.size(); i++) {
if (vec[i] > 2) {
cout << vec.size() + 2 << endl;
return;
}
if (vec[i] >= 2) {
two++;
}
}
if (two > 1) {
cout << vec.size() + 2 << endl;
return;
}
if (two == 1) {
cout << vec.size() + 1 << endl;
return;
}
cout << vec.size() << endl;
}
int main() {
while (scanf("%d", &n) != EOF) {
work();
}
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()
k1, k2, k1l, k2l = 0, 0, '0', '1'
for i in s:
if k1l != i:
k1 += 1
k1l = i
if k2l != i:
k2 += 1
k2l = i
print(min(len(s), max(k1, k2) + 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 dp[3][100003];
int main() {
int n;
cin >> n;
string s;
cin >> s;
dp[0][0] = dp[1][0] = 1;
for (int i = 1; i < n; i++) {
if (s[i] == s[i - 1]) {
dp[0][i] = dp[0][i - 1];
dp[1][i] = max(dp[0][i - 1] + 1, dp[1][i - 1]);
dp[2][i] = max(dp[2][i - 1], 1 + dp[1][i - 1]);
} else {
dp[0][i] = 1 + dp[0][i - 1];
dp[1][i] = max(dp[0][i - 1], dp[1][i - 1] + 1);
dp[2][i] = max(dp[2][i - 1] + 1, dp[1][i - 1]);
}
}
cout << max(dp[2][n - 1], dp[1][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 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Agostinho Junior (junior94)
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
char[] str = in.next().toCharArray();
int[][] dp = new int[2][3];
for (int i = 0; i < n; i++) {
int[][] temp = new int[2][3];
int set = str[i] - '0';
temp[set][0] = 1;
for (int curFlip = 0; curFlip < 3; curFlip++) {
for (int prevFlip = Math.max(0, curFlip - 1); prevFlip <= curFlip; prevFlip++) {
int expected = curFlip == prevFlip ? set ^ 1 : set;
if (dp[expected][prevFlip] > 0) {
temp[set][curFlip] = Math.max(temp[set][curFlip], 1 + dp[expected][prevFlip]);
}
}
}
for (int bit = 0; bit < 2; bit++) {
for (int flip = 0; flip < 3; flip++) {
dp[bit][flip] = Math.max(dp[bit][flip], temp[bit][flip]);
}
}
}
int ans = 0;
for (int[] row : dp) {
for (int v : row) {
ans = Math.max(ans, v);
}
}
out.println(ans);
}
}
static class OutputWriter {
private PrintWriter output;
public OutputWriter() {
this(System.out);
}
public OutputWriter(OutputStream out) {
output = new PrintWriter(out);
}
public OutputWriter(Writer writer) {
output = new PrintWriter(writer);
}
public void println(Object o) {
output.println(o);
}
public void close() {
output.close();
}
}
static class InputReader {
private BufferedReader input;
private StringTokenizer line = new StringTokenizer("");
public InputReader() {
this(System.in);
}
public InputReader(InputStream in) {
input = new BufferedReader(new InputStreamReader(in));
}
public InputReader(String s) {
try {
input = new BufferedReader(new FileReader(s));
} catch (IOException io) {
io.printStackTrace();
System.exit(0);
}
}
public void fill() {
try {
if (!line.hasMoreTokens()) {
line = new StringTokenizer(input.readLine());
}
} catch (IOException io) {
io.printStackTrace();
System.exit(0);
}
}
public String next() {
fill();
return line.nextToken();
}
public int readInt() {
fill();
return Integer.parseInt(line.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;
const int N = 2e5 + 15;
const long long int inf = 1e18;
void solve() {
long long int n;
cin >> n;
string s;
long long int ans = 1;
cin >> s;
for (long long int i = 1; i < n; i++) ans += (s[i] != s[i - 1]);
cout << min(n, ans + 2);
}
void debug(long long int tt) {}
signed main() {
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 | n = int(input())
s = input()
cnt = 1
intervals = [1]
for i in range(1, n):
if s[i] != s[i - 1]:
intervals.append(1)
else:
intervals[-1] += 1
has_three = False
for interval in intervals:
if interval >= 3:
has_three = True
break
if has_three:
print(len(intervals) + 2)
else:
twos = intervals.count(2)
if twos >= 2:
print(len(intervals) + 2)
elif twos == 1:
print(len(intervals) + 1)
else:
print(len(intervals))
| 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 answer(n,A):
count=1
for i in range(1,n):
if A[i]!=A[i-1]:
count+=1
return min(count+2,n)
n=int(input())
s=input()
print(answer(n,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 |
import java.io.*;
import java.util.*;
public class A {
InputStream is;
int __t__ = 1;
int __f__ = 0;
int __FILE_DEBUG_FLAG__ = __f__;
String __DEBUG_FILE_NAME__ = "src/T";
FastScanner in;
PrintWriter out;
public void solve() {
int n = in.nextInt();
char[] s = in.next().toCharArray();
int sum1 = 0;
int now = 0;
for (int i = 0; i < n; i++) {
if (now == s[i] - '0') {
now = now ^ 1;
sum1++;
}
}
int sum2 = 0;
now = 1;
for (int i = 0; i < n; i++) {
if (now == s[i] - '0') {
now = now ^ 1;
sum2++;
}
}
int res = Math.max(sum1, sum2);
int cnt = 0;
int left = 0;
for (int i = 1; i < n; i++) {
if (s[i] == s[i-1]) {
left = i;
cnt++;
break;
}
}
for (int i = left; i < n - 1; i++) {
if (s[i] == s[i+1]) {
cnt++;
break;
}
}
System.out.println(res + cnt);
}
public void run() {
if (__FILE_DEBUG_FLAG__ == __t__) {
try {
is = new FileInputStream(__DEBUG_FILE_NAME__);
} catch (FileNotFoundException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
System.out.println("FILE_INPUT!");
} else {
is = System.in;
}
in = new FastScanner(is);
out = new PrintWriter(System.out);
solve();
}
public static void main(String[] args) {
new A().run();
}
public void mapDebug(int[][] a) {
System.out.println("--------map display---------");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.printf("%3d ", a[i][j]);
}
System.out.println();
}
System.out.println("----------------------------");
System.out.println();
}
public void debug(Object... obj) {
System.out.println(Arrays.deepToString(obj));
}
class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream) {
this.stream = stream;
//stream = new FileInputStream(new File("dec.in"));
}
int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = nextInt();
return array;
}
int[][] nextIntMap(int n, int m) {
int[][] map = new int[n][m];
for (int i = 0; i < n; i++) {
map[i] = in.nextIntArray(m);
}
return map;
}
long nextLong() {
return Long.parseLong(next());
}
long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; i++)
array[i] = nextLong();
return array;
}
long[][] nextLongMap(int n, int m) {
long[][] map = new long[n][m];
for (int i = 0; i < n; i++) {
map[i] = in.nextLongArray(m);
}
return map;
}
double nextDouble() {
return Double.parseDouble(next());
}
double[] nextDoubleArray(int n) {
double[] array = new double[n];
for (int i = 0; i < n; i++)
array[i] = nextDouble();
return array;
}
double[][] nextDoubleMap(int n, int m) {
double[][] map = new double[n][m];
for (int i = 0; i < n; i++) {
map[i] = in.nextDoubleArray(m);
}
return map;
}
String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
String[] nextStringArray(int n) {
String[] array = new String[n];
for (int i = 0; i < n; i++)
array[i] = next();
return array;
}
String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | n=int(input())
s=input()
mem=s[0]
streak=1
amount=0
res=0
score=1
for l in range(1,n):
if s[l]==mem:
streak+=1
else:
mem=s[l]
streak=1
score+=1
if streak>2:
res=2
elif streak==2:
amount+=1
if amount>1:
res=2
if amount+max(res-1,0)==1:
res=1
print(score+res) | PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.nextLine();
String line = scan.nextLine();
int l = line.charAt(0) - '0';
int count = 1;
boolean[] arr = new boolean[n];
arr[0] = true;
for (int i = 1; i < n; i++) {
if (line.charAt(i) - '0' != l) {
l = 1 - l;
count++;
arr[i] = true;
}
}
int max = -1;
int sum = 0;
for (int i = 1; i < n; i++) {
if (!arr[i]) {
sum++;
} else {
max = Math.max(max, sum);
sum = 0;
}
}
max = Math.max(max, sum);
System.out.println(Math.min(count + 2, n));
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n;
char s[100005];
int main() {
scanf("%d %s", &n, s);
int ans = 1;
for (int i = 1; i < n; i++) {
if (s[i] != s[i - 1]) ans++;
}
printf("%d\n", min(n, ans + 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 | '''input
56
10101011010101010101010101010101010101011010101010101010
'''
from sys import stdin
def get_count(c):
count = 0
flag = False
i = 1
while i < n + 2:
if arr[i] == arr[i -1] and arr[i] == c:
if i == n + 1 or i == n:
flag = True
count += 1
i += 2
continue
i += 1
return count, flag
# main starts
n = int(stdin.readline().strip())
arr = list(map(int, list(stdin.readline().strip())))
aux = [0]* n
aux[0] = 1
for i in range(1,n):
aux[i] = aux[i - 1]
if arr[i] != arr[i - 1]:
aux[i] += 1
count = 0
for i in range(0, n - 1):
if arr[i] == arr[i + 1]:
count += 1
count = min(count, 2)
print(aux[-1] + count)
| PYTHON3 |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | n = int(input())
ch = str(input())
if n == 1:
print(1)
else:
B = [0 for _ in range(n)]
B[0] = 1
c = ch[0]
for k in range(1,n):
if ch[k] == c:
B[k] = B[k-1]
else:
B[k] = B[k-1] + 1
c = ch[k]
T = [0 for _ in range(n)]
F = [0 for _ in range(n)]
T[1] = B[0]+1
if ch[1] == ch[0]:
F[1] = 1
for i in range(1):
for j in range(i+2,n):
c = 0
if ch[j] == ch[j-1] and not(F[j-1]):
temp = max([T[j-1], B[j-1] + 1])
if temp == 1 + B[j-1]:
c = 1
else:
temp = T[j-1] + 1
if F[j-1] and ch[j] != ch[j-1]:
c = 1
T[j] = temp
F[j] = c
print(T[n-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 | n = int(input())
s = raw_input()
res = 1
for r in range(n-1):
if (s[r] != s[r+1]):
res += 1
if (res == n):
print res
elif (res == n-1):
print res+1
else:
print res + 2 | PYTHON |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int n, len, dp0, dp1;
char a[maxn];
int main() {
cin >> n;
scanf("%s", a);
len = strlen(a);
int ans = 0;
if (a[0] == '0')
dp0 = 1;
else
dp1 = 1;
for (int i = 1; i < len; i++) {
if (a[i] == '0')
dp0 = dp1 + 1;
else
dp1 = dp0 + 1;
}
ans = max(dp0, dp1);
int cnt0 = 0, cnt1 = 0;
for (int i = 0; i < len; i++) {
if (a[i] == '0' && a[i + 1] == '0')
cnt0++;
else if (a[i] == '1' && a[i + 1] == '1')
cnt1++;
}
ans += min(2, cnt1 + cnt0);
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.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
public class A {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
sc.nextInt();
char s[] = sc.next().toCharArray();
int res = get_res(s);
int freq_11 = 0;
int freq_00 = 0;
for (int i = 0; i < s.length-1; i++) {
if(s[i] == s[i+1]) {
if(s[i]=='0')
freq_00++;
else
freq_11++;
}
}
out.println(res + Math.min(2, freq_00+freq_11));
out.flush();
}
private static int get_res(char s[]) {
int cnt = 1;
for (int i = 0; i < s.length - 1; i++) {
if (s[i] != s[i + 1])
cnt++;
}
return cnt;
}
private static class Scanner {
public BufferedReader reader;
public StringTokenizer st;
public Scanner(InputStream file) {
reader = new BufferedReader(new InputStreamReader(file));
st = null;
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
String line = reader.readLine();
if (line == null)
return null;
st = new StringTokenizer(line);
} catch (Exception e) {
throw (new RuntimeException());
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
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 | ans=0
z=0
a=raw_input()
b=raw_input()
for i in range(1,int(a)):
if b[i]==b[i-1] and z<2:
ans+=1
z+=1
if b[i]!=b[i-1]:
ans+=1
print ans+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>
int main() {
int i, n, df = 0, eq = 0;
char s[100000 + 1];
scanf("%d\n", &n);
gets(s);
for (i = 1; i < n; i++) s[i] == s[i - 1] ? eq++ : df++;
df++;
if (eq > 1)
df += 2;
else if (eq == 1)
df++;
printf("%d", df);
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 MAXN = 1e5 + 10;
const int INF = 1e9;
int n, a[MAXN], dp[MAXN][3], last0 = -1, last1 = -1;
string s;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> s;
dp[0][0] = 1;
dp[0][1] = 1;
dp[0][2] = 1;
if (s[0] == '0') {
last0 = 0;
} else {
last1 = 0;
}
int ans = 1;
for (int i = 1; i < n; i++) {
if (s[i] == '0') {
if (last0 != -1) {
dp[i][0] = max(dp[i][0], dp[last0][1]);
dp[i][1] = max(dp[i][1], dp[last0][2]);
}
if (last1 != -1) {
dp[i][0] = max(dp[i][0], dp[last1][0]);
dp[i][1] = max(dp[i][1], dp[last1][1]);
dp[i][2] = max(dp[i][2], dp[last1][2]);
}
last0 = i;
} else {
if (last1 != -1) {
dp[i][0] = max(dp[i][0], dp[last1][1]);
dp[i][1] = max(dp[i][1], dp[last1][2]);
}
if (last0 != -1) {
dp[i][0] = max(dp[i][0], dp[last0][0]);
dp[i][1] = max(dp[i][1], dp[last0][1]);
dp[i][2] = max(dp[i][2], dp[last0][2]);
}
last1 = i;
}
dp[i][0]++;
dp[i][1]++;
dp[i][2]++;
ans = max({ans, dp[i][0], dp[i][1], dp[i][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.util.*;
import java.io.*;
public class B {
FastScanner in;
PrintWriter out;
public void solve() throws IOException {
int n = in.nextInt();
String s = in.next();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = s.charAt(i) - '0';
}
int x1 = -1;
int x2 = 2 * n;
for (int i = 1; i < n; i++) {
if (a[i] == a[i - 1]) {
if (x1 == -1) {
x1 = i;
} else {
x2 = i;
}
}
}
for (int i = 0; i < n; i++) {
if (i >= x1 && i < x2) {
a[i] = 1 - a[i];
}
}
int cur = a[0];
int ans = 1;
for (int i = 0; i < n; i++) {
if (a[i] != cur) {
cur = a[i];
ans++;
}
}
out.println(ans);
}
public void run() {
try {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
public static void main(String[] arg) {
new B().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 | n, tab, changes = int(input()), str(input()), 1
for i in range(1, n):
changes += 1 if tab[i] != tab[i - 1] else 0
print(min(changes + 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 | import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String str=sc.next();
int[][] dp=new int[n][3];
for(int i=0;i<n;i++){
if(i==0){
dp[i][0]=dp[i][1]=dp[i][2]=1;
continue;
}
if(str.charAt(i)==str.charAt(i-1)){
dp[i][0]=dp[i-1][0];
dp[i][1]=Math.max(dp[i-1][0]+1,dp[i-1][1]);
dp[i][2]=Math.max(dp[i-1][1]+1,dp[i-1][2]);
}else{
dp[i][0]=dp[i-1][0]+1;
dp[i][1]=Math.max(dp[i-1][0],dp[i-1][1]+1);
dp[i][2]=Math.max(dp[i-1][1],dp[i-1][2]+1);
}
}
pl(Math.max(dp[n-1][1],dp[n-1][2]));
}
static void pl(Object o){
System.out.println(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 | def solve(compStr, compLen):
# print len(compStr)
# print compLen
ret = len(compStr)
for v in compLen:
if v >= 3:
return ret + 2
cnt = 0
for i in range(len(compLen)):
if compLen[i] >= 2:
cnt += 1
if cnt >= 2:
return ret + 2
elif cnt == 1:
return ret + 1
else:
return ret
n = int(raw_input())
s = raw_input()
compStr = ''
compStr += s[0]
compLen = [1]
for i in range(1, len(s)):
if s[i-1] != s[i]:
compStr += s[i]
compLen.append(1)
else:
compLen[-1] += 1
#print 's = ' + compStr
#print compLen
print solve(compStr, compLen)
| 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.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class SetNumbers {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s = sc.next();
int res=1;
for (int i = 1; i < n; i++) {
res += (s.charAt(i) != s.charAt(i-1) ? 1 : 0);
}
res=Math.min(res+2, n);
System.out.println( res);
sc.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>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
const double Pi = acos(-1);
using namespace std;
int debug = 01;
int leader[110][110];
int find_leader(int c, int i) {
if (leader[c][i] == i) return i;
return leader[c][i] = find_leader(c, leader[c][i]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
string s;
cin >> n >> s;
int c = 1;
for (int i = 1; i < n; i++)
if (s[i] != s[i - 1]) c++;
int extra = n - c;
if (extra > 2) extra = 2;
cout << c + extra;
}
| 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()
cur, arr = 1, []
for i in range(1, n):
if s[i] == s[i - 1]:
cur += 1
else:
arr.append(cur)
cur = 1
arr.append(cur)
cur = 0
ans = len(arr)
for i in arr:
if i > 2:
exit(print(ans + 2))
if i == 2:
cur += 1
print(ans + min(2, cur)) | 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 MOD = 1000 * 1000 * 1000 + 7;
const double PI = 2 * acos(0);
char A[100002];
int main() {
int N;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
int ans = 1;
for (int i = 1; i < N; i++) {
ans += (A[i] != A[i - 1]);
}
cout << min(N, ans + 2) << "\n";
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* Created by Kamil Khadiev on 11.08.2015.
*/
public class khadiev_345_c {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
init(in);
in.close();
solve();
PrintWriter out = new PrintWriter(System.out);
print(out);
out.close();
}
public static int n;
public static int[] m,w,x, h;
public static long ans;
public static int INF = Integer.MAX_VALUE/2;
static String s;
private static void init(BufferedReader in) throws IOException {
n = Integer.parseInt(in.readLine().trim());
s = in.readLine().trim();
}
private static void solve() {
ans = 1;
char c = s.charAt(0);
for (int i = 1; i < n; i++){
if (c!= s.charAt(i)){
ans++;
c = s.charAt(i);
}
}
int q = 1;
c = s.charAt(0);
boolean found = false;
for (int i = 1; i < n; i++){
if (c!= s.charAt(i)){
if (q>=3){
found = true;
break;
}
q = 1;
c = s.charAt(i);
}
else
{
q++;
}
}
if (found || q>=3){
ans +=2;
}
else
{
int q1 = 0;
q = 1;
c = s.charAt(0);
found = false;
for (int i = 1; i < n; i++){
if (c!= s.charAt(i)){
if (q1>=2 && q>=2){
found = true;
break;
}
q1 = q;
q = 1;
c = s.charAt(i);
}
else
{
q++;
}
}
if (found || (q1>=2 && q>=2)){
ans +=2;
}
else
{
q1 = 0;
q = 1;
c = s.charAt(0);
found = false;
for (int i = 1; i < n; i++){
if (c!= s.charAt(i)){
if (q>=2) q1++;
if (q1>=2){
q = 0;
found = true;
break;
}
q = 1;
c = s.charAt(i);
}
else
{
q++;
}
}
if (q>=2) q1++;
if (found || (q1>=2)){
ans +=2;
}
else
{
if (q1>0) ans++;
}
}
}
}
private static void print(PrintWriter out) throws IOException {
out.print(ans);
}
}
| JAVA |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
char inp[100005];
int f1[100005][2];
int f2[100005][2];
int flip[100005][2];
int main() {
int n;
int ans = 0;
scanf("%d", &n);
scanf("%s", inp);
memset(f1, 0, sizeof f1);
memset(f2, 0, sizeof f2);
memset(flip, 0, sizeof flip);
if (inp[0] == '0')
f1[0][0] = 1, f1[0][1] = 0;
else
f1[0][0] = 0, f1[0][1] = 1;
for (int i = 1; i <= n - 1; i++) {
if (inp[i] == '0') f1[i][0] = f1[i - 1][1] + 1, f1[i][1] = f1[i - 1][1];
if (inp[i] == '1') f1[i][0] = f1[i - 1][0], f1[i][1] = f1[i - 1][0] + 1;
f1[i][0] = max(f1[i - 1][0], f1[i][0]);
f1[i][1] = max(f1[i - 1][1], f1[i][1]);
}
if (inp[n - 1] == '0')
f2[n - 1][0] = 1, f2[n - 1][1] = 0;
else
f2[n - 1][0] = 0, f2[n - 1][1] = 1;
for (int i = n - 2; i >= 0; i--) {
if (inp[i] == '0') f2[i][0] = f2[i + 1][1] + 1, f2[i][1] = f2[i + 1][1];
if (inp[i] == '1') f2[i][0] = f2[i + 1][0], f2[i][1] = f2[i + 1][0] + 1;
f2[i][0] = max(f2[i + 1][0], f2[i][0]);
f2[i][1] = max(f2[i + 1][1], f2[i][1]);
}
if (inp[n - 1] == '0') flip[n - 1][0] = 0, flip[n - 1][1] = 1;
if (inp[n - 1] == '1') flip[n - 1][0] = 1, flip[n - 1][1] = 0;
ans = 1;
if (n - 2 > 0) ans = max(ans, flip[n - 1][0] + f1[n - 2][1]);
if (n - 2 > 0) ans = max(ans, flip[n - 1][1] + f1[n - 2][0]);
for (int i = n - 2; i >= 0; i--) {
flip[i][0] = f2[i + 1][0];
if (inp[i] == '1') flip[i][0] = max(flip[i][0], f2[i + 1][1] + 1);
flip[i][0] = max(flip[i][0], flip[i + 1][0]);
if (inp[i] == '1') flip[i][0] = max(flip[i][0], flip[i + 1][1] + 1);
flip[i][1] = f2[i + 1][1];
if (inp[i] == '0') flip[i][1] = max(flip[i][1], f2[i + 1][0] + 1);
flip[i][1] = max(flip[i][1], flip[i + 1][1]);
if (inp[i] == '0') flip[i][1] = max(flip[i][1], flip[i + 1][0] + 1);
ans = max(ans, flip[i][0]);
if (i > 0) ans = max(ans, flip[i][0] + f1[i - 1][1]);
ans = max(ans, flip[i][1]);
if (i > 0) ans = max(ans, flip[i][1] + f1[i - 1][0]);
}
cout << ans << endl;
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string a;
cin >> a;
if (a.size() == 1) {
return cout << 1, 0;
}
char f = a[0], s = !(a[0] - '0') + '0';
bool b = true, find = false;
int c = 1;
for (int i = 1; i < n; i++) {
if (a[i] != a[i - 1]) c++;
}
cout << min(c + 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[110000];
int n, f[110000], num[110000];
int bit1[110000], bit2[110000];
int query1(int x) {
int ans = 0;
while (x) {
ans = max(ans, bit1[x]);
x -= ((x) & (-(x)));
}
return ans;
}
void update1(int x, int newv) {
while (x <= n) {
bit1[x] = max(bit1[x], newv);
x += ((x) & (-(x)));
}
}
int query2(int x) {
int ans = 0;
while (x) {
ans = max(ans, bit2[x]);
x -= ((x) & (-(x)));
}
return ans;
}
void update2(int x, int newv) {
while (x <= n) {
bit2[x] = max(bit2[x], newv);
x += ((x) & (-(x)));
}
}
int main() {
scanf("%d", &n);
scanf("%s", s + 1);
for (int i = 1; i <= n; i++) num[i] = s[i] - '0';
f[1] = 1;
if (num[1])
update2(1, 1);
else
update1(1, 1);
for (int i = 2; i <= n; i++) {
if (num[i]) {
f[i] = query1(i - 1) + 1;
update2(i, f[i]);
} else {
f[i] = query2(i - 1) + 1;
update1(i, f[i]);
}
}
int maxlen = 0;
for (int i = 1; i <= n; i++) maxlen = max(maxlen, f[i]);
printf("%d\n", min(n, maxlen + 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(raw_input())
str=raw_input()
v=(0,0)
v_changing=(0,0)
v_changed=(0,0)
best=0
for ch in str:
nv= (v[1]+1, v[1]) if ch=='0' else (v[0], v[0]+1)
if ch=='0':
nv_changing=(0, max(v[0], v_changing[0])+1)
else:
nv_changing=(max(v[1], v_changing[1])+1, 0)
if ch=='0':
nv_changed=(max(v_changed[1], v_changing[1])+1, v_changed[1])
else:
nv_changed=(v_changed[0], max(v_changed[0], v_changing[0])+1)
v, v_changing, v_changed=nv, nv_changing, nv_changed
best=max(best, max(v+v_changing+v_changed))
print best
| 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 double pi = acos(-1);
int n;
string s;
int main() {
cin >> n;
cin >> s;
int ans = 1;
for (int i = 1; i < n; ++i) ans += s[i] != s[i - 1];
ans = min(n, ans + 2);
cout << ans << endl;
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
char s[100100];
int n, ans = 1, tmp;
int main() {
scanf("%d %s", &n, s);
for (int i = 1; i < n; i++)
if (s[i] != s[i - 1])
ans++;
else
tmp++;
if (tmp) ans++;
if (tmp > 1) ans++;
printf("%d\n", ans);
return 0;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 |
def main():
n = int(raw_input())
S = list(raw_input())
k = 0
for i in xrange(n-1):
if S[i] != S[i+1]:
k += 1
if k >= n-2:
print n
else:
print k+3
if __name__ == "__main__":
main()
| 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 long long OO = 1e8;
const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }
int n;
string s;
int dp[100009][2][3];
int solveDP(int i, int prev, int op) {
if (i == n) return op == 3;
int& ret = dp[i][prev][op];
if (ret != -1) return ret;
ret = solveDP(i + 1, prev, op);
if (op != 1 && (s[i] - '0') != prev)
ret = max(ret, 1 + solveDP(i + 1, (s[i] - '0'), op));
if (op == 1 && (s[i] - '0') == prev)
ret = max(ret, 1 + solveDP(i + 1, prev ^ 1, op));
if (op <= 1) ret = max(ret, solveDP(i, prev, op + 1));
return ret;
}
int main() {
cin >> n;
cin >> s;
memset(dp, -1, sizeof(dp));
cout << max(solveDP(0, 0, 0), solveDP(0, 1, 0)) << endl;
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | n = int(input())
s = input()
dp = [1]*n
for i in range(1,n):
if s[i]!=s[i-1]:
dp[i] = max(dp[i], dp[i-1] + 1)
else:
dp[i] = dp[i-1]
print(min(n, max(dp)+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() {
int n;
cin >> n;
string s;
cin >> s;
int cnt = 1;
for (int i = 0; i < n - 1; i++) {
if (s[i] != s[i + 1]) cnt++;
}
cout << min(n, cnt + 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;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long N, ans = 1;
cin >> N;
string S;
cin >> S;
for (long long i = 1; i < N; i++) ans += (S[i] != S[i - 1]);
cout << min(ans + 2, N);
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
char s[100005];
int dp[100005][3];
int main() {
int n;
scanf("%d", &n);
scanf("%s", s);
int a[2], b[2], c[2];
a[0] = a[1] = b[0] = b[1] = c[0] = c[1] = 0;
for (int i = 1; i <= n; i++) {
int x = s[i - 1] - '0';
dp[i][2] = max(dp[i][2], c[1 - x] + 1);
dp[i][2] = max(dp[i][2], b[x] + 1);
dp[i][2] = max(dp[i][2], a[1 - x] + 1);
c[x] = max(c[x], dp[i][2]);
dp[i][1] = max(dp[i][1], b[1 - x] + 1);
dp[i][1] = max(dp[i][1], a[x] + 1);
b[x] = max(b[x], dp[i][1]);
dp[i][0] = max(dp[i][0], a[1 - x] + 1);
a[x] = max(a[x], dp[i][0]);
}
int ans = max(a[0], a[1]);
ans = max(ans, max(b[0], b[1]));
ans = max(ans, max(c[0], c[1]));
printf("%d\n", ans);
}
| CPP |
603_A. Alternative Thinking | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n, a[100010], f[100010], i, dx, ans, t;
char c;
int main() {
scanf("%d", &n);
dx = 0;
ans = 0;
f[1] = 1;
t = 0;
for (i = 1; i <= n; i++) {
scanf(" %c", &c);
if (c == '1')
a[i] = 1;
else
a[i] = 0;
if (i > 1) {
if (a[i] == a[i - 1])
f[i] = f[i - 1] + 1;
else {
f[i] = 1;
ans++;
}
if (f[i] > 1) t++;
}
}
ans++;
if (t == 1) dx = 1;
if (t > 1) dx = 2;
printf("%d\n", ans + dx);
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().strip()
ret = 1
nb2 = 0
for i in range(1,n):
if s[i-1] != s[i]:
ret += 1
else:
nb2 += 1
if nb2 >= 2:
ret += 2
else:
ret += nb2
print(ret)
| PYTHON3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.