Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int n; char S[100005]; int main() { scanf("%d", &n); scanf("%s", S + 1); int as = 1, bj = 0; for (int i = (2); i <= (n); ++i) { if (S[i] != S[i - 1]) ++as; else bj++; } printf("%d\n", as + min(bj, 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> const double PI = 3.141592653589793238460; using namespace std; long long pows(long long a, long long n, long long m) { long long res = 1; while (n) { if (n % 2 != 0) { res = (res * a) % m; n--; } else { a = (a * a) % m; n = n / 2; } } return res % m; } long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } bool isprime(long long n) { if (n == 1) { return false; } for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } bool istrue(string s) { int i = 0; int j = s.size() - 1; while (i < j) { if (s[i] == s[j]) { i++; j--; } else { return false; } } return true; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; string s; cin >> s; int cnt = 0; for (int i = 1; i < s.size(); i++) { if (s[i] == s[i - 1]) { cnt++; } } cout << min(s.size(), s.size() - cnt + 2) << 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() b = [] k = 1 a0 = s[0] ma = 0 for i in range(1,n): if a0 == s[i] and i == n - 1: k += 1 ma = max(ma, k) b.append(k) elif a0 == s[i]: k += 1 elif a0 != s[i] and i == n - 1: ma = max(ma, k) b.append(k) b.append(1) else: ma = max(ma, k) b.append(k) a0 = s[i] k = 1 key = 0 if n == 1: key = 1 print(1) if ma > 2: key = 1 print(len(b) + 2) elif key == 0: i = 0 k = 0 while i < len(b): if b[i] > 1: k += 1 if k > 1: print(len(b) + 2) key = 1 break i += 1 if key == 0: if k > 0: print(len(b) + 1) else: print(len(b))
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; string s; cin >> n >> s; char curr = s[0]; int cnt = 1; for (int i = 0; i < s.length(); ++i) if (s[i] != curr) { ++cnt; curr = s[i]; } 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
//package DPContest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class CodeForces_603A { public static void main(String[] args) throws IOException { BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); bf.readLine(); String input=(bf.readLine()); int counter=1; char last=input.charAt(0); for (int i = 1; i < input.length(); i++) { if(last != input.charAt(i) ){ counter++; last=input.charAt(i); } } System.out.println(Math.min(input.length(), (counter+2))); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int N, dp[4][(int)(1e5 + 5)]; bitset<(int)(1e5 + 5)> A; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> N; string s; cin >> s; for (int i = 0; i < N; i++) { A[i] = (s.at(i) == '1'); } for (int i = 0; i < 3; i++) { dp[i][1] = 1; } for (int i = 0; i + 1 < N; i++) { bool b = A[i] == A[i + 1]; dp[0][i + 2] = dp[0][i + 1] + !b; dp[1][i + 2] = max(dp[0][i + 1] + b, dp[1][i + 1] + !b); dp[2][i + 2] = max(dp[1][i + 1] + b, dp[2][i + 1] + !b); } cout << max(dp[1][N], dp[2][N]) << "\n"; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n; char a[N]; int G[N][2], P[N][2]; int main() { scanf("%d\n", &n); scanf("%s", a + 1); G[1][a[1] - '0'] = 1; for (int i = 2; i <= n; ++i) { if (a[i] == '0') { G[i][0] = max(G[i - 1][1] + 1, G[i - 1][0]); G[i][1] = G[i - 1][1]; } else { G[i][0] = G[i - 1][0]; G[i][1] = max(G[i - 1][0] + 1, G[i - 1][1]); } } P[n][a[n] - '0'] = 1; for (int i = n - 1; i > 0; --i) { if (a[i] == '0') { P[i][0] = max(P[i + 1][1] + 1, P[i + 1][0]); P[i][1] = P[i + 1][1]; } else { P[i][0] = P[i + 1][0]; P[i][1] = max(P[i + 1][0] + 1, P[i + 1][1]); } } int res = max(P[1][0], P[1][1]); for (int i = 1; i <= n;) { if (a[i] == '1') { res = max(res, P[i + 1][1] + G[i - 1][1] + 1); } else { res = max(res, P[i + 1][0] + G[i - 1][0] + 1); } int j = i; while (j < n && a[j] != a[j + 1]) j++; res = max(res, G[i - 1][a[i] - '0'] + j - i + 1 + P[j + 1][a[j] - '0']); i = j + 1; } cout << res; 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.Arrays; import java.util.Scanner; public class AlternativeThinking { static int n , memo[][][]; static String s; //0 --> not toggled //1 --> in the toggled //2 --> after toggling static int solve(int idx , int last , int state) { if(idx == n) return 1; if(memo[idx][last][state] != -1) return memo[idx][last][state]; int cur = s.charAt(idx)-'0'; int ans = 0; if(idx == 0) { ans = Math.max(solve(idx+1,cur,0), solve(idx+1,cur,1)); } else { if(state == 0) { int tog = ((1-cur == last)?0:1)+solve(idx+1,1-cur,1); int not = ((cur == last)?0:1) +solve(idx+1,cur,0); ans = Math.max(tog, not); } if(state == 1) { int con = ((last == (1-cur))?0:1)+solve(idx+1,1-cur,1); int stop = ((last == (cur))?0:1)+solve(idx+1,cur,2); ans = Math.max(con, stop); } if(state == 2) { ans = ((last == cur)?0:1)+solve(idx+1,cur,2); } } return memo[idx][last][state] = ans; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); s = sc.next(); memo = new int[n+2][3][3]; for (int i = 0; i < memo.length; i++) { for(int j=0;j<memo[i].length;j++) Arrays.fill(memo[i][j], -1); } System.out.println(solve(0,0,0)); 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
n = int(input()) t = [int(i) for i in input()] ch = 0 sm = 0 for i in range(n-1): if t[i] == t[i+1]: sm+=1 else: ch+=1 print(ch + min(sm,2) + 1)
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const int base = 100003; const long long MM = 1ll * 1000000007 * 1000000007; const int maxc = 2e9; string s; int n; void Solve() { cin >> n >> s; int ans = 1; s = " " + s; for (int i = 2; i <= n; ++i) ans += (s[i] != s[i - 1]); cout << min(n, ans + 2); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int test = 1; while (test--) 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
from sys import stdin,stdout nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int,stdin.readline().split())) for _ in range(1):#nmbr()): n=nmbr() a=[int(ch) for ch in input()] n=len(a) dp=[[[0 for _ in range(2)]for _ in range(3)]for _ in range(n)] dp[0][0][a[0]]=1 dp[0][0][1^a[0]]=0 dp[0][1][a[0]]=1 dp[0][1][1^a[0]]=1 dp[0][2][a[0]]=1 dp[0][2][1^a[0]]=1 for i in range(1,n): dp[i][0][a[i]] = max(1+dp[i-1][0][1^a[i]],dp[i-1][0][a[i]]) dp[i][0][1 ^ a[i]] = 0#dp[i-1][0][1^a[i]] dp[i][1][a[i]] = max(dp[i-1][0][a[i]],1+dp[i-1][0][1^a[i]])#max(dp[i-1][1][a[i]],dp[i-1][0][a[i]],1+dp[i-1][0][1^a[i]]) dp[i][1][1 ^ a[i]] = max(dp[i-1][1][a[i]]+1,dp[i-1][0][1^a[i]]) dp[i][2][a[i]] =dp[i-1][2][1^a[i]]+1 dp[i][2][1 ^ a[i]] = dp[i-1][1][a[i]]+1 ans=max(dp[n-1][2][0],dp[n-1][2][1]) # # print(*dp,sep='\n') # for i in range(3): # for j in range(2): # ans=max(ans,dp[n-1][i][j]) print(ans)
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
N = input() store = raw_input() curletter = store[0] rep = 1 maxrep = 0 alter = 1 twicerep = 0 for letter in store[1:]: if letter == curletter: rep += 1 if rep >= 2: twicerep += 1 maxrep = max(rep,maxrep) else: alter += 1 curletter = letter if maxrep >= 3: print alter + 2 elif twicerep >= 2: print alter + 2 elif twicerep == 1: print alter + 1 else: print alter
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
#! /usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=utf-8 # # Copyright © 2016 missingdays <missingdays@missingdays> # # Distributed under terms of the MIT license. """ """ n = int(input()) s = input() res = 1 for i in range(1, n): if s[i] != s[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
n = int(input()) a = input() ans = min(n, 3 + a.count('01') + a.count('10')) print(ans)
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; char c[120001]; int n, i, kol, ans; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (i = 1; i <= n; i++) cin >> c[i]; c[n + 1] = '#'; for (i = 1; i <= n; i++) if (c[i] != c[i + 1]) ans++; else kol++; ans += min(kol, 2); cout << ans << '\n'; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.*; import java.util.*; public class Main implements Runnable { static boolean use_n_tests = false; static int stack_size = 1 << 27; void solve(FastScanner in, PrintWriter out, int testNumber) { int n = in.nextInt(); char[] s = in.next().toCharArray(); int mx = 1; int s1 = s[0]; int c = 0; for (int i = 1; i < n; i++) { if (s[i] != s1) { s1 = s[i]; mx++; } if (s[i] == s[i - 1]) { c++; } } out.println(mx + Math.min(c, 2)); } // ****************************** template code *********** class Coeff { long mod; long[][] C; long[] fact; boolean cycleWay = false; Coeff(int n, long mod) { this.mod = mod; fact = new long[n + 1]; fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = i; fact[i] %= mod; fact[i] *= fact[i - 1]; fact[i] %= mod; } } Coeff(int n, int m, long mod) { // n > m cycleWay = true; this.mod = mod; C = new long[n + 1][m + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= Math.min(i, m); j++) { if (j == 0 || j == i) { C[i][j] = 1; } else { C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; C[i][j] %= mod; } } } } public long C(int n, int m) { if (cycleWay) { return C[n][m]; } return fC(n, m); } private long fC(int n, int m) { return (fact[n] * inv(fact[n - m] * fact[m] % mod)) % mod; } private long inv(long r) { if (r == 1) return 1; return ((mod - mod / r) * inv(mod % r)) % mod; } } class Pair { int first; int second; public int getFirst() { return first; } public int getSecond() { return second; } } class MultisetTree<T> { int size = 0; TreeMap<T, Integer> mp = new TreeMap<>(); void add(T x) { mp.merge(x, 1, Integer::sum); size++; } void remove(T x) { if (mp.containsKey(x)) { mp.merge(x, -1, Integer::sum); if (mp.get(x) == 0) { mp.remove(x); } size--; } } T greatest() { return mp.lastKey(); } int size() { return size; } int diffSize() { return mp.size(); } } class Multiset<T> { int size = 0; Map<T, Integer> mp = new HashMap<>(); void add(T x) { mp.merge(x, 1, Integer::sum); size++; } void remove(T x) { if (mp.containsKey(x)) { mp.merge(x, -1, Integer::sum); if (mp.get(x) == 0) { mp.remove(x); } size--; } } int size() { return size; } int diffSize() { return mp.size(); } } static class Range { int l, r; int id; public int getL() { return l; } public int getR() { return r; } public Range(int l, int r, int id) { this.l = l; this.r = r; this.id = id; } } static class Array { static Range[] readRanges(int n, FastScanner in) { Range[] result = new Range[n]; for (int i = 0; i < n; i++) { result[i] = new Range(in.nextInt(), in.nextInt(), i); } return result; } static public Integer[] read(int n, FastScanner in) { Integer[] out = new Integer[n]; for (int i = 0; i < out.length; i++) { out[i] = in.nextInt(); } return out; } static public int[] readint(int n, FastScanner in) { int[] out = new int[n]; for (int i = 0; i < out.length; i++) { out[i] = in.nextInt(); } return out; } } class Graph { List<List<Integer>> create(int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } return graph; } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream io) { br = new BufferedReader(new InputStreamReader(io)); } public String line() { String result = ""; try { result = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return result; } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } void run_t_tests() { int t = in.nextInt(); int i = 0; while (t-- > 0) { solve(in, out, i++); } } void run_one() { solve(in, out, -1); } @Override public void run() { in = new FastScanner(System.in); out = new PrintWriter(System.out); if (use_n_tests) { run_t_tests(); } else { run_one(); } out.close(); } static FastScanner in; static PrintWriter out; public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(null, new Main(), "", stack_size); thread.start(); thread.join(); } }
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
# -*- coding: utf-8 -*- """ Created on Tue Dec 1 16:12:23 2015 @author: matt """ bb = input() aa = input() seq = [int(aa[x]) for x in range(len(aa))] seq_rev = [(~x % 2) for x in seq] count = 0 switches = 0 current_seq = seq current_seq_state = True # true : original, False : alt state = seq_rev[0] for i in range(len(seq)): if current_seq[i] != state: count += 1 elif switches < 2: switches += 1 if current_seq_state: current_seq = seq_rev else: current_seq = seq current_seq_state = not current_seq_state count += 1 state = current_seq[i] print(count)
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
from collections import Counter _ = input() s = input().strip() sequences = Counter() cur_seq_len = 1 for p, n in zip(s, s[1:] + '*'): if p == n: cur_seq_len += 1 else: if cur_seq_len > 3: cur_seq_len = 3 sequences[cur_seq_len] += 1 cur_seq_len = 1 basic = sum(sequences.values()) if 3 in sequences or sequences[2] >= 2: print(basic + 2) elif sequences[2] == 1: print(basic + 1) else: print(basic)
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.math.*; import java.util.*; public class Main { static final long MOD = 998244353; //static final long MOD = 1000000007; static boolean[] visited; public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int N = sc.nextInt(); String s = sc.next(); int val = 1; char last = s.charAt(0); for (int i = 1; i < N; i++) { if (s.charAt(i) != last) { val++; last = s.charAt(i); } } System.out.println(Math.min(N,val+2)); } public static long power(long x, long y, long p) { // Initialize result long res = 1; // Update x if it is more // than or equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if((y & 1)==1) res = (res * x) % p; // y must be even now // y = y / 2 y = y >> 1; x = (x * x) % p; } return res; } public static long dist(int[] point, int[] point2) { return (long)(Math.pow((point2[1]-point[1]),2)+Math.pow((point2[0]-point[0]),2)); } public static long gcd(long a, long b) { if (b == 0) return a; else return gcd(b,a%b); } public static int[][] sort(int[][] array) { //Sort an array (immune to quicksort TLE) Random rgen = new Random(); for (int i = 0; i < array.length; i++) { int randomPosition = rgen.nextInt(array.length); int[] temp = array[i]; array[i] = array[randomPosition]; array[randomPosition] = temp; } Arrays.sort(array, new Comparator<int[]>() { @Override public int compare(int[] arr1, int[] arr2) { return arr1[0]-arr2[0]; } }); return array; } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } class Node { public HashSet<Node> children; public int n; public Node(int n) { this.n = n; children = new HashSet<Node>(); } public void addChild(Node node) { children.add(node); } public void removeChild(Node node) { children.remove(node); } /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override public int hashCode() { return n; } @Override public boolean equals(Object obj) { if (! (obj instanceof Node)) { return false; } else { Node node = (Node) obj; return (n == node.n); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.lang.Math; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; public class Main implements Runnable { public static void main(String[] args) { new Thread(new Main()).start(); } @Override public void run() { BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(System.in, "ISO-8859-1")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } try { int n = Integer.parseInt(in.readLine()); String kResult = in.readLine(); char c = '?', current; boolean flipped = true; int cntr = 0; for (int i = 0; i < kResult.length(); i++) { current = kResult.charAt(i); if (c == current) { flipped = false; } else { flipped = true; } if (flipped) { cntr++; } c = current; } System.out.print(Math.min(n, cntr + 2)); } catch (IOException e) { e.printStackTrace(); } } }
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 A { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); String s = in.next(); int len = 1; for (int i = 1; i < n; ++i) { if (s.charAt(i) != s.charAt(i - 1)) { len++; } } System.out.println(Math.min(n, len + 2)); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import sys n = int(sys.stdin.readline()) s = sys.stdin.readline() s = list(s) a = [] k=1 for i in range(n): if i == n-1: a.append(k) break if s[i]==s[i+1]: k+=1 else: a.append(k) k = 1 add_amount = 0 count = 0 for i in range(n-1): if s[i]==s[i+1]: count+=1 add_amount=min(2,count) print(len(a) + add_amount)
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 = input() s = raw_input() ans = 1 for i in range(1, n): ans += 1 if s[i] != s[i-1] else 0 print min(ans+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
#include <bits/stdc++.h> using namespace std; int32_t main() { long long n; cin >> n; string s; cin >> s; long long c = 0, j = 0; while (j < n) { long long i = j; while (i + 1 < n && s[i] == s[i + 1]) i++; j = i + 1; c++; } cout << min(n, c + 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> int main() { int n, result, count = 0, i; scanf("%d", &n); char arr[n]; for (i = 0; i < n; i++) { scanf("\n%c", &arr[i]); } i = 0; for (i = 0; i < n; i++) { if (arr[i] != arr[i - 1]) { count++; } } if (count + 2 < n) { result = count + 2; } if (count + 2 >= n) { result = n; } printf("%d", result); }
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().strip()) arr=list(raw_input().strip()) dp=[1 for i in xrange(n)] last1,last0=-1,-1 for i in xrange(n): if arr[i]=='0': if last1!=-1: dp[i]=dp[last1]+1 last0=i else: if last0!=-1: dp[i]=dp[last0]+1 last1=i print min(max(dp)+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
#include <bits/stdc++.h> using namespace std; const int N = 1E5 + 5; int n, f1, f2, f[N], g[N], p[N], last, ans; char s[N]; int main() { scanf("%d\n%s", &n, s + 1); for (int i = 1; i <= n; i++) if (s[i] == '0') { f1 = f[i] = f2 + 1; } else f2 = f[i] = f1 + 1; f1 = f2 = 0; for (int i = n; i; i--) if (s[i] == '0') { f1 = g[i] = f2 + 1; } else f2 = g[i] = f1 + 1; for (int i = n; i; i--) { p[i] = last ? last : i + 1; if (s[i] == s[i + 1]) last = i + 1; } for (int i = 1; i <= n; i++) ans = max(ans, f[i] + g[p[i]] + p[i] - i - 1); cout << ans; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n = int(input()) s = raw_input() if (n == 1): print 1 elif (n == 2): print 2 else: 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 N = 2e5; const int INF = 1e9 + 9; int n; char a[N]; int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cin >> n; bool flag = 0; int cnt = 0; vector<int> v; for (int i = int(1); i <= int(n); ++i) { cin >> a[i]; if (a[i] != a[i - 1]) { if (i > 1) v.push_back(cnt); cnt = 1; } else ++cnt; } v.push_back(cnt); int ans = v.size(); cnt = 0; for (int i = int(0); i <= int(ans - 1); ++i) { if (v[i] >= 3) flag = 1; if (v[i] >= 2) ++cnt; } if (cnt >= 2) flag = 1; if (flag) cout << ans + 2 << "\n"; else cout << ans + min(1, cnt) << "\n"; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const int MXN = 100100; const int inf = 1e9; const int mod = 1000000007; char s[MXN]; int main() { int n; scanf("%d", &n); scanf("%s", s); int cur = s[0] - '0'; int res = 1; int state = 0; for (int i = (1); i < (n); i++) { s[i] -= '0'; if (s[i] == 1 - cur) { cur = 1 - cur; res++; if (state == 1) state = 2; } else { if (state == 2) continue; else state = 1; res++; cur = 1 - cur; } } cout << res; 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
/** * Created by ProDota2 on 16.12.2015. **/ import java.util.*; import java.io.*; public class Main { public static void main(String argc[]){ Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); String s; int n; n = in.nextInt(); s = in.next(); int ans = 0; for(int i = 0;i < s.length() - 1;i++) ans += (s.charAt(i) != s.charAt(i + 1) ? 1 : 0); out.print(Math.min(ans + 3 , n)); out.close(); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.*; import java.net.Socket; import java.security.spec.ECField; import java.util.HashMap; import java.util.StringTokenizer; import static java.util.Arrays.sort; public class Main { static int a[]; static int l[]; static int ans[]; static HashMap<Integer, Boolean> map = new HashMap<>(); public static void main (String [] args) throws Exception { InputReader reader = new InputReader(); PrintWriter out = new PrintWriter(System.out); int n, k; String str; n = reader.nextInt(); str=reader.next(); int ans=1; for(int i = 1 ; i < str.length();i++) { if(str.charAt(i)!=str.charAt(i-1))ans++; } out.print(Math.min(n,ans+2)); out.flush(); } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; InputReader() { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { e.printStackTrace(); } } return tokenizer.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; string S; cin >> S; vector<vector<int>> dp(N, vector<int>(3)); dp[0][0] = 1; dp[0][1] = 1; dp[0][2] = 1; for (int i = 0; i < N - 1; i++) { if (S[i + 1] == S[i]) { dp[i + 1][0] = dp[i][0]; dp[i + 1][1] = max(dp[i][0] + 1, dp[i][1]); dp[i + 1][2] = max(dp[i][1] + 1, dp[i][2]); } else { dp[i + 1][0] = dp[i][0] + 1; dp[i + 1][1] = max(dp[i][0], dp[i][1] + 1); dp[i + 1][2] = max(dp[i][1], dp[i][2] + 1); } } cout << *max_element(dp[N - 1].begin(), dp[N - 1].end()) << 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; char s[100004]; int main() { scanf("%d%s", &n, s + 1); int a = 3; for (int i = 2; i <= n; i++) a += (s[i] != s[i - 1]); printf("%d\n", (a > n) ? n : a); 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.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author George Marcus */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.readInt(); String S = in.readString(); int[] A = new int[N]; for (int i = 0; i < N; i++) { A[i] = S.charAt(i) - '0'; } int ans = 0; int cnt = 0; boolean hasSpecial = false; for (int i = 0; i < N; i++) { int j = i; while (j < N && A[j] == A[i]) { j++; } j--; if (j != i) { cnt++; } if (j - i > 1) { hasSpecial = true; } i = j; ans++; } int extra = Math.min(2, cnt); if (hasSpecial) { extra = 2; } ans += extra; out.println(ans); } } 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 interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class C { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); char[] s = br.readLine().toCharArray(); int[] a = new int[s.length]; for(int i = 0; i < n; i++) { a[i] = s[i] - '0'; } br.close(); int[][][] dp = new int[n][3][2];// position; 0 - original, 1 - once flipped, 2 - twice flipped; 0/1 dp[0][0][a[0]] = 1; dp[0][1][(a[0] + 1) % 2] = 1; for(int i = 1; i < n; i++) { int other = (a[i] + 1) % 2; dp[i][0][a[i]] = Math.max(dp[i][0][a[i]], Math.max(dp[i - 1][0][a[i]], dp[i - 1][0][other] + 1)); // dp[i][1][a[i]] = Math.max(dp[i - 1][0][a[i]], Math.max(dp[i - 1][0][other] + 1, Math.max(dp[i - 1][1][a[i]], dp[i - 1][1][other] + 1))); dp[i][1][other] = Math.max(dp[i][1][other], Math.max(dp[i - 1][0][other], Math.max(dp[i - 1][0][a[i]] +1, Math.max(dp[i - 1][1][a[i]] + 1, dp[i - 1][1][other])))); dp[i][2][a[i]] = Math.max(dp[i][2][a[i]], Math.max(dp[i - 1][1][a[i]], Math.max(dp[i - 1][1][other] + 1, Math.max(dp[i - 1][2][a[i]], dp[i - 1][2][other] + 1)))); } int best = 1; for(int i = 0; i < 3; i++) { best = Math.max(best, Math.max(dp[n - 1][i][0], dp[n - 1][i][1])); } System.out.println(best); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStream; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, in, out); out.close(); } static class Task { public int solve(int testNumber, InputReader in, PrintWriter out) { int n,x; String s; n=in.nextInt(); s=in.next(); x=1; for(int i=1;i<n;i++) if(s.charAt(i)!=s.charAt(i-1)) x++; if(x+2 <= n) x=x+2; else if(x+1 <= n) x++; out.println(x); return 0; } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int lf[100005], rt[100005], rsp, alf[100005], art[100005]; int main() { int n, i, cnt, cnt2; string s; scanf("%d", &n); cin >> s; rsp = 0; cnt = 0; cnt2 = 0; for (i = 0; i < n; i++) { if (i && s[i] == s[i - 1]) cnt2 = 1; else cnt2++; if (i == 0 || s[i] != s[i - 1]) cnt++; lf[i] = cnt; alf[i] = cnt2; rsp = max(rsp, cnt); } cnt = 0; cnt2 = 0; for (i = n - 1; i >= 0; i--) { if (i < n - 1 && s[i] == s[i + 1]) cnt2 = 1; else cnt2++; if (i == n - 1 || s[i] != s[i + 1]) cnt++; rt[i] = cnt; art[i] = cnt2; } int aux; for (i = 0; i < n; i++) { aux = alf[i]; aux += i - alf[i] >= 0 ? lf[i - alf[i]] : 0; aux += i + art[i] < n ? rt[i + art[i]] : 0; rsp = max(rsp, aux); } printf("%d\n", rsp); }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
from sys import stdin inp = stdin.readline t = int(inp().strip()) string = list(inp().strip()) longestSub = 1 prev = string[0] l = 0 d = 0 maxGap = -1 for i in range(1,t): if string[i] != prev: longestSub += 1 prev = string[i] print(min(len(string) , longestSub + 2))
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int n, ans = 1, k = 1; string s, p; bool ok = false; cin >> n >> s; p = s; for (int i = 1; i < s.length() - 1; i++) { if (s[i - 1] == s[i] && s[i] == s[i + 1]) { ok = true; if (s[i] == '1') s[i] = '0'; else s[i] = '1'; break; } } if (!ok) { for (int i = 1; i < s.length(); i++) { if (s[i] == s[i - 1]) { ok = true; } if (ok) { if (s[i] == '1') s[i] = '0'; else s[i] = '1'; if (s[i] != s[i + 1]) break; } } } for (int i = 1; i < s.length(); i++) { if (s[i] != s[i - 1]) ans++; } if (p.length() > 1 && p[0] == p[1]) { if (p[0] == '1') p[0] = '0'; else p[0] = '1'; } swap(p, s); for (int i = 1; i < s.length(); i++) { if (s[i] != s[i - 1]) k++; } if (ans < k) { swap(ans, k); } else swap(s, p); cout << ans; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.util.Scanner; public class rus { public static void main(String args[]) { Scanner in = new Scanner(System.in); int n =in.nextInt(); String s = in.next(); int res =1; for(int i=1;i < n;i++){ if(s.charAt(i)==s.charAt(i-1)) res += 0; else res += 1; } 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
def ii(): return int(input()) def si(): return input() def mi(): return map(int,input().split()) def li(): return list(mi()) n=ii() s=si() if n==1: print(1) else: cnt=1 st=s[0] for i in range(1,n): if s[i]!=st: cnt+=1 st=s[i] 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
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); string s; int n; cin >> n >> s; int e0 = 0, e1 = 0; bool b0 = true, b1 = true; for (int i = 0; i < n; i++) { if ((b0 && s[i] == '0') || (!b0 && s[i] == '1')) { b0 = !b0; e0++; } if ((b1 && s[i] == '1') || (!b1 && s[i] == '0')) { b1 = !b1; e1++; } } cout << min(n, max(e1, e0) + 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; template <typename T> inline T abs(T t) { return t < 0 ? -t : t; } const long long modn = 1000000007; inline long long mod(long long x) { return x % modn; } int a[100009]; int main() { int i, j, n; char c; scanf("%d", &n); for (i = 0; i < n; i++) { scanf(" %c", &c); a[i] = c - '0'; } int p = a[0]; int sz = 1, empty = 0; for (i = 1; i < n; i++) { if (a[i] != p) { p = !p; sz++; } else { empty++; } } if (sz == n) { printf("%d\n", n); return 0; } printf("%d\n", sz + 1 + (empty > 1)); }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int OneD(int row, int col, int t_n_o_r) { return row * t_n_o_r + col; } long long int MOD(long long int a, long long int b) { return a >= 0 ? a % b : (b - abs(a % b)) % b; } void MN(long long int &a, long long int b) { if (a > b) a = b; } void MX(long long int &a, long long int b) { if (a < b) a = b; } int X[] = {1, 0, -1, 0, 1, -1, -1, 1}; int Y[] = {0, 1, 0, -1, 1, 1, -1, -1}; int main() { int n, t = 0, last, nw; scanf("%d", &n); string str; cin >> str; t++; last = str[0] - '0'; for (int i = 1; i < n; i++) { nw = str[i] - '0'; if (nw != last) { t++; last ^= 1; } } printf("%d\n", (((t + 2 < n)) ? (t + 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 c[200000]; int f[200000][2]; int b[200000][2]; int d[200000], mx[200000]; int main() { int N; scanf("%d", &N); scanf("%s", c); for (int Ni = 0; Ni < N; Ni++) { if (Ni != 0) f[Ni][0] = f[Ni - 1][0], f[Ni][1] = f[Ni - 1][1]; int bit = c[Ni] - '0'; f[Ni][bit] = f[Ni - 1][bit ^ 1] + 1; } for (int Ni = N - 1; Ni >= 0; Ni--) { if (Ni != N - 1) b[Ni][0] = b[Ni + 1][0], b[Ni][1] = b[Ni + 1][1]; int bit = c[Ni] - '0'; b[Ni][bit] = b[Ni + 1][bit ^ 1] + 1; } for (int Ni = N; Ni > 0; Ni--) { int bit = c[Ni - 1] - '0'; d[Ni] = b[Ni][bit] - b[Ni][1 - bit]; } for (int Ni = N - 1; Ni > 0; Ni--) mx[Ni] = max(mx[Ni + 1], d[Ni]); int ans = 0; for (int Ni = 0; Ni < N; Ni++) for (int i = 0; i < 2; i++) ans = max(ans, f[Ni][i] + b[Ni + 1][i] + mx[Ni + 2]); 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 doty[100010][2][2][2], sumy[100010], n; bool vis[100010][2][2][2]; char s[100010]; int solve(int pos, bool lf, bool f, bool fd) { if (pos > n) { if (fd || f) return 0; else return -1000000; } if (vis[pos][lf][f][fd]) { return doty[pos][lf][f][fd]; } int ans = 0; if (!fd) { bool nlf = (!sumy[pos] == lf) ? !lf : lf; ans = max(ans, (!sumy[pos] == lf) + solve(pos + 1, nlf, 1, 0)); bool nlf2 = (sumy[pos] == lf) ? !lf : lf; if (f) { ans = max(ans, (sumy[pos] == lf) + solve(pos + 1, nlf2, 0, 1)); } } if (!f) { bool nlf = (sumy[pos] == lf) ? !lf : lf; ans = max(ans, (sumy[pos] == lf) + solve(pos + 1, nlf, 0, fd)); } vis[pos][lf][f][fd] = 1; return doty[pos][lf][f][fd] = ans; } int main() { scanf("%d", &n); scanf("%s", &s[1]); for (int kk = 1; kk <= n; kk++) sumy[kk] = s[kk] - '0'; printf("%d\n", max(solve(1, 1, 0, 0), solve(1, 0, 0, 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; using ll = long long; int solve() { int n, ans{0}, n_flip{0}; cin >> n; if (n <= 0) return ans; char a, b, c; cin >> a; --n; ++ans; if (n == 0) return ans; cin >> b; if (b != a) ++ans; else ++n_flip; --n; bool flip{true}; while (n--) { cin >> c; if (c != b) { ++ans; flip = true; } else if (a == b) n_flip = 2; else if (flip && n_flip < 2) { ++n_flip; flip = false; } a = b; b = c; } return ans + n_flip; } 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; const int MAXN = 110000; char s[MAXN]; int main() { int n; scanf("%d", &n); scanf("%s", s); char tmp = s[0]; int ans = 0; int cnt = 0; for (int i = 1; i < n; i++) { if (tmp != s[i]) { ans++; } else { cnt++; } tmp = s[i]; } if (cnt > 2) cnt = 2; cout << ans + cnt + 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
#include <bits/stdc++.h> using namespace std; char s[100100]; int n; int solve() { int rlt = 1; int a = 0; for (int i = 0; i < n - 1; i++) { if (s[i] == s[i + 1]) a++; if (s[i + 1] != s[i]) rlt++; } if (a > 2) a = 2; return rlt + a; } int main() { scanf("%d", &n); scanf("%s", s); printf("%d\n", 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; long long invmod(long long a, long long n, long long mod) { long long result = 1; while (n > 0) { if (n % 2 == 1) { result = (result * a) % mod; } a = (a * a) % mod; n = n / 2; } return result; } long long fact(long long a, long long mod) { if (a == 0) return 1; return (a * fact(a - 1, mod)) % mod; } long long choose(long long n, long long r, long long mod) { if (n < r) return 0; long long num = fact(n, mod); long long den = (fact(r, mod) * fact(n - r, mod)) % mod; return (num * invmod(den, mod - 2, mod)) % mod; } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(0); long long n, i, j; cin >> n; string s; cin >> s; long long dp[n][2][2][2]; if (s[0] == '0') { dp[0][0][0][0] = 1; dp[0][1][1][1] = 1; dp[0][0][0][1] = 0; dp[0][0][1][1] = 0; dp[0][1][0][1] = 0; dp[0][1][0][0] = 0; } else { dp[0][1][0][0] = 1; dp[0][0][1][1] = 1; dp[0][0][0][1] = 0; dp[0][0][0][0] = 0; dp[0][1][0][1] = 0; dp[0][1][1][1] = 0; } for (i = 1; i < n; i++) { if (s[i] == '0') { dp[i][0][0][0] = max(1 + dp[i - 1][1][0][0], dp[i - 1][0][0][0]); dp[i][0][0][1] = max(1 + dp[i - 1][1][1][1], 1 + dp[i - 1][1][0][1]); dp[i][1][1][1] = max(max(1 + dp[i - 1][0][1][1], dp[i - 1][1][1][1]), 1 + dp[i - 1][0][0][0]); dp[i][1][0][0] = dp[i - 1][1][0][0]; dp[i][1][0][1] = dp[i - 1][1][0][1]; dp[i][0][1][1] = dp[i - 1][0][1][1]; } else { dp[i][1][0][0] = max(1 + dp[i - 1][0][0][0], dp[i - 1][1][0][0]); dp[i][1][0][1] = max(1 + dp[i - 1][0][1][1], 1 + dp[i - 1][0][0][1]); dp[i][0][1][1] = max(max(1 + dp[i - 1][1][1][1], dp[i - 1][0][1][1]), 1 + dp[i - 1][1][0][0]); dp[i][1][1][1] = dp[i - 1][1][1][1]; dp[i][0][0][1] = dp[i - 1][0][0][1]; dp[i][0][0][0] = dp[i - 1][0][0][0]; } } cout << max(max(max(dp[n - 1][0][0][0], dp[n - 1][1][0][0]), max(dp[n - 1][0][0][1], dp[n - 1][1][0][1])), max(dp[n - 1][0][1][1], dp[n - 1][1][1][1])); }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n=int(raw_input()) s=raw_input() ctr=1 for i in range(1,n): if(s[i]!=s[i-1]): ctr+=1 print min(n,ctr+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.util.*; import java.lang.*; public class Rextester{ public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); char[] array = br.readLine().toCharArray(); boolean flag = false; int tones = 0,tzero = 0; for(int i=1;i<n;i++){ if(array[i]==array[i-1]){ if(array[i]=='1'){ tones++; } else{ tzero++; } } } int one=0,zero=0; for(int i=0;i<n;i++){ if(array[i]=='1'){ one = zero+1; } else{ zero = one+1; } } int z = Math.max(one,zero); System.out.println(Math.min(2,tones+tzero)+z); } }
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() ans=1 s=raw_input() for i in xrange(1,n): if s[i]!=s[i-1]: ans+=1 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
import sys def main(): n = int(input()) tab = sys.stdin.readline() s = 1 t = tab[0] e = 0 for i in range(1,len(tab)-1): if t != tab[i]: s+=1 t = tab[i] if tab[i] == tab[i-1] == '0' or tab[i] == tab[i-1] == '1': e+=1 e = min(e,2) print(s+e) main()
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; template <class T> inline bool getmin(T *a, const T &b) { if (b < *a) { *a = b; return true; } return false; } template <class T> inline bool getmax(T *a, const T &b) { if (b > *a) { *a = b; return true; } return false; } template <class T> inline void read(T *a) { char c; while (isspace(c = getchar())) { } bool flag = 0; if (c == '-') flag = 1, *a = 0; else *a = c - 48; while (isdigit(c = getchar())) *a = *a * 10 + c - 48; if (flag) *a = -*a; } const int mo = 1000000007; template <class T> T pow(T a, T b, int c = mo) { T res = 1; for (T i = 1; i <= b; i <<= 1, a = 1LL * a * a % c) if (b & i) res = 1LL * res * a % c; return res; } const int N = 200000; int f[N], g[N]; int s[N], a[N][2], b[N][2]; int n; int main() { cin >> n; for (int i = (1); i <= (n); ++i) { char c; while (isspace(c = getchar())) ; if (c == '0') s[i] = 0; else s[i] = 1; } f[1] = 1; for (int i = (2); i <= (n); ++i) { a[i][0] = a[i - 1][0]; a[i][1] = a[i - 1][1]; a[i][s[i - 1]] = i - 1; f[i] = f[a[i][s[i] ^ 1]] + 1; } g[n] = 1; for (int i = (n - 1); i >= (1); --i) { b[i][0] = b[i + 1][0]; b[i][1] = b[i + 1][1]; b[i][s[i + 1]] = i + 1; g[i] = g[b[i][s[i] ^ 1]] + 1; } int ans = 0; for (int i = (1); i <= (n); ++i) getmax(&ans, f[a[i][s[i]]] + g[b[i][s[i]]] + 1); ans = f[n]; cout << min(ans + 2, n) << endl; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.LinkedList; public class a { public static void main(String[] args) throws IOException { //BufferedReader input = new BufferedReader(new FileReader("input.txt")); BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String s[] = input.readLine().split(" "); int n = Integer.parseInt(s[0]); int[] b = new int [n]; int[] c = new int [n]; //System.out.println(score.length()); String score = input.readLine(); //System.out.println(score); for (int i = 0; i < n; i++) { b[i] = Character.getNumericValue(score.charAt(i)); //System.out.println(b[i] +" "); } ArrayList list = new ArrayList<Integer>(); list.add(b[0]); c[0]= 1; //int cnt= 1; for (int i = 1; i < b.length; i++) { if(b[i] != (int)list.get(list.size()-1) ){ list.add(b[i]); } c[i] = list.size(); } // for (int i = 0; i < c.length; i++) { // System.out.print(c[i] +" "); // } // for (int i = 0; i < list.size(); i++) { // System.out.print(list.get(i) + " "); // } if(list.size() >= n-1){ System.out.println(n); }else System.out.println(list.size() +2); // System.out.println(list.size()); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n, s, ans = int(input()), list(input()), 1 flag = 0 for i in range(1, n): if s[i] == s[i - 1]: s[i] = str(int(s[i]) ^ 1) flag = 1 elif flag: break # print(''.join(s)) last = s[0] for i in s[1:]: if i != last: last = i 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
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const long long INF = 1000 * 1000 * 1000 + 7; const long long LINF = INF * (long long)INF; const int MOD = 1000 * 1000 * 1000 + 7; const int MAX = 1000 * 100 + 47; int A[MAX]; int C[MAX]; int sz = 0; string s; int solve(string s) { int cnt = 0; sz = 0; for (int i = (0); i < ((int)s.size() + 1); i++) { if (i == (int)s.size() || (i && s[i] != s[i - 1])) { A[sz] = s[i - 1] - '0'; C[sz] = cnt; sz++; cnt = 1; continue; } cnt++; } int cnt0 = 0, cnt1 = 0; bool is = false; for (int i = (0); i < (sz); i++) { if (A[i] == 0 && C[i] > 1) cnt0++; if (A[i] == 1 && C[i] > 1) cnt1++; if (C[i] > 2) is = true; } if ((cnt0 > 0 && cnt1 > 0) || is || cnt0 > 1 || cnt1 > 1) { return sz + 2; } if (cnt0 > 0 || cnt1 > 0) { return sz + 1; } return sz; } int brute(string s) { int ans = 0; for (int i = (0); i < ((int)s.size()); i++) { for (int j = (i); j < ((int)s.size()); j++) { for (int k = (i); k < (j + 1); k++) { s[k] = s[k] == '0' ? '1' : '0'; } char last = '2'; int cur = 0; for (int k = (0); k < ((int)s.size()); k++) { if (last != s[k]) { last = s[k]; cur++; } } ans = max(ans, cur); for (int k = (i); k < (j + 1); k++) { s[k] = s[k] == '0' ? '1' : '0'; } } } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n >> s; cout << solve(s) << 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
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n = sc.nextInt(); String temp = sc.next(); int[] resultadosArray = new int[n]; for (int i = 0; i < n; i++) { resultadosArray[i] = temp.charAt(i) - '0'; } int alternantes=1,iguales=0; for(int i = 1; i<n;i++) { if(resultadosArray[i]!=resultadosArray[i-1]) alternantes++; else iguales++; } System.out.println(alternantes+Math.min(2,iguales)); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; void flash() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } void solve() { long long n; cin >> n; string s; cin >> s; long long dp[n][2][2][2]; memset(dp, -1, sizeof(dp)); function<long long(long long, long long, long long, long long)> dfs = [&](long long i, long long top, long long flip, long long lastFlip) -> long long { if (i == n) return 0; long long &ans = dp[i][top][flip][lastFlip]; if (ans != -1) return ans; long long tans = dfs(i + 1, top, flip, lastFlip); if (!flip) { tans = max(tans, dfs(i + 1, top, 0, 0)); tans = max(tans, dfs(i + 1, top, 1, 1)); } if (s[i] == '1') { if (!top) tans = max(tans, dfs(i + 1, 1, flip, 0) + 1); if (top && (lastFlip || !flip)) tans = max(tans, dfs(i + 1, 0, 1, 1) + 1); } else { if (top) tans = max(tans, dfs(i + 1, 0, flip, 0) + 1); if (!top && (lastFlip || !flip)) tans = max(tans, dfs(i + 1, 1, 1, 1) + 1); } return ans = tans; }; cout << max(dfs(0, 0, 0, 0), dfs(0, 1, 0, 0)) << "\n"; } int32_t main() { flash(); long long t; t = 1; while (t--) { solve(); } return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; char s[maxn]; int main(void) { int n, x, y; cin >> n >> s; x = 0, y = 1; for (int i = 1; i < strlen(s); i++) { if (s[i] != s[i - 1]) y++; else x++; } cout << y + min(x, 2) << endl; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n,line=int(input()),input() ans=1+sum(line[i]!=line[i-1] for i in range(1,n)) print(min(ans+2,n))
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const long long MX = 1e9; const long long INF = 1e9; void print_a(vector<int> v) { if (v.size()) cout << v[0]; for (int i = 1; i < v.size(); ++i) cout << ' ' << v[i]; cout << '\n'; } vector<vector<int> > init_vvi(int n, int m, int val) { return vector<vector<int> >(n, vector<int>(m, val)); } vector<vector<long long> > init_vvl(int n, int m, long long val) { return vector<vector<long long> >(n, vector<long long>(m, val)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; string s; cin >> s; vector<int> v; v.push_back(1); int c = 0; for (int i = 1; i < n; ++i) { if (s[i] == s[i - 1]) ++v[c]; else { v.push_back(1); ++c; } } int f1 = 0, f2 = 0; for (int i = 0; i < c + 1; ++i) { if (i & 1) { if (v[i] > 2) f2 = 2; else if (v[i] > 1) ++f2; if (f2 == 2) break; } else { if (v[i] > 2) f1 = 2; else if (v[i] > 1) ++f1; if (f1 == 2) break; } } int ans = v.size(); if (f1 == 2 || f2 == 2 || f1 + f2 == 2) ans += 2; else if (f1 || f2) ++ans; cout << ans << '\n'; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); String s = in.next(); int cnt = 0; for (int i = 0; i + 1 < s.length(); ++i) if (s.charAt(i) == s.charAt(i + 1)) ++cnt; cnt -= 2; cnt = Math.max(cnt, 0); out.println(s.length() - cnt); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; long long int n; string s; int main() { cin >> n; cin >> s; long long int rs = 1; for (long long int i = 1; i <= n - 1; i++) { rs += (s[i] != s[i - 1]); } cout << min(rs + 2, n) << "\n"; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n=int(raw_input()) s=raw_input() counter=1 for i in xrange(n-1): if s[i]!=s[i+1]: counter +=1 print min(n,counter+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; char str[100020]; int len[3] = {0}; int num[100020]; void zhuanhuan(int x) { if (x >= 3) { len[2]++; } else if (x == 2) { len[1]++; } else { len[0]++; } } int main() { int n, kuan = 1; scanf("%d", &n); scanf("%*c"); for (int i = 0; i < n; i = i + 1) scanf("%c", &str[i]); scanf("%*c"); for (int i = 0; i < n; i = i + 1) num[i] = str[i] - '0'; int kuanlen = 1; for (int i = 1; i < n; i++) { if (num[i] == num[i - 1]) { kuanlen++; } else { kuan++; zhuanhuan(kuanlen); kuanlen = 1; } } zhuanhuan(kuanlen); if (len[2] > 0) { printf("%d\n", kuan + 2); } else if (len[1] > 1) { printf("%d\n", kuan + 2); } else { if (len[1] == 1) printf("%d\n", kuan + 1); else printf("%d\n", kuan); } 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 = 1500; const long long inf = 1e15 + 10; const int MOD = 1e9 + 7; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); string s; int ans = 0, n; cin >> n >> s; for (int i = 0; i < n; i++) if (s[i] != s[i - 1]) ans++; cout << min(ans + 2, n) << '\n'; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.util.Scanner; public class AlternativeThinking { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input =new Scanner(System.in); int n = input.nextInt(); String s = input.next(); int total=1; for(int i=0; i<s.length()-1;i++){ if(s.charAt(i)!=s.charAt(i+1))total++; } System.out.println(Math.min(n,total+2)); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.util.*; import java.io.*; import java.math.*; public class Main { public static long mod= (long) (1e9 +7); public static void main(String args[]) { InputReader s= new InputReader(System.in); OutputStream outputStream= System.out; PrintWriter out= new PrintWriter(outputStream); int n= s.nextInt(); String str= s.next(); int count=1,index=0; boolean flag = false; if(str.charAt(0)=='1') flag=true; for(int i=1;i<n;i++){ if(flag && str.charAt(i)=='1'){ index++; continue; } else if(flag && str.charAt(i)!='1'){ break; } else if(!flag && str.charAt(i)=='0'){ index++; continue; } else if(!flag && str.charAt(i)=='1'){ break; } } if(str.charAt(index)=='1') flag=true; else flag=false; for(int i=index;i<n-1;i++){ if(flag && str.charAt(i+1)=='0'){ continue; } else if(flag && str.charAt(i+1)=='1'){ flag=false; count++; } else if(!flag && str.charAt(i+1)=='1'){ continue; } else if(!flag && str.charAt(i+1)=='0'){ flag=true; count++; } } if(flag && str.charAt(n-1)=='0')count++; if(!flag && str.charAt(n-1)=='1') count++; if(count==n || count==n-1) out.println(n); else if(count<n-1) out.println(count+2); //out.println(count); out.close(); } static long combinations(long n,long r){ // O(r) r= Math.min(r, n-r); long ans=n; // nC1=n; for(int i=1;i<r;i++){ ans= ans*(n-i); ans= ans/(i+1); } return ans; } static void catalan_numbers(int n) { long catalan[]= new long[n+1]; catalan[1] = 1; for (int i = 2; i <= n; i++) { for (int j = 1; j <= i - 1; j++) { catalan[i] = catalan[i] + ((catalan[j]) * catalan[i - j]); } } } static ArrayList<Integer> primeFactors(int n) // O(sqrt(n)) { // Print the number of 2s that divide n ArrayList<Integer> arr= new ArrayList<>(); while (n%2 == 0) { arr.add(2); n = n/2; } // n must be odd at this point. So we can skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i = i+2) { // While i divides n, print i and divide n while (n%i == 0) { arr.add(i); n = n/i; } } // This condition is to handle the case when n is a prime number // greater than 2 if (n > 2) arr.add(n); return arr; } public static long expo(long a, long b){ if (b==0) return 1%mod; if (b==1) return a%mod; if (b==2) return ((a%mod)*(a%mod))%mod; if (b%2==0){ return expo(expo(a%mod,(b%mod)/2)%mod,2%mod)%mod; } else{ return (a%mod)*(expo(expo(a%mod,(b-1)%mod/2)%mod,2%mod)%mod)%mod; } } static class Pair implements Comparable<Pair> { long f; String s; Pair(long ii, String cc) { f=ii; s=cc; } public int compareTo(Pair o) { return Long.compare(this.f, o.f); } } public static int[] sieve(int N){ // O(n*log(logn)) int arr[]= new int[N+1]; for(int i=2;i<Math.sqrt(N);i++){ if(arr[i]==0){ for(int j= i*i;j<= N;j= j+i){ arr[j]=1; } } } return arr; // All the i for which arr[i]==0 are prime numbers. } static long gcd(long a,long b){ // O(logn) if(b==0) return a; return gcd(b,a%b); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream inputstream) { reader = new BufferedReader(new InputStreamReader(inputstream)); tokenizer = null; } public String nextLine(){ String fullLine=null; while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { fullLine=reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return fullLine; } return fullLine; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.util.Scanner; public class R334_3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); //sc.nextLine(); String str = sc.nextLine(); str = sc.nextLine(); int res = 1; for(int i = 1; i < n; i++) { if (str.charAt(i) == str.charAt(i-1)) { res += 0; } else res +=1; } 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
#include <bits/stdc++.h> using namespace std; int dp[1 << 17][3][2]; int s[1 << 17]; void solve(int i, int state) { if (i == 0) return; int &z = dp[i][state][0], &o = dp[i][state][1]; if (z != -1) return; if (state == 0) { solve(i - 1, 0); if (s[i] == 1) { z = dp[i - 1][state][0]; o = max(dp[i - 1][state][1], dp[i - 1][state][0] + 1); } else { o = dp[i - 1][state][1]; z = max(dp[i - 1][state][0], dp[i - 1][state][1] + 1); } return; } if (state == 1) { solve(i - 1, 1); solve(i - 1, 0); if (s[i] == 1) { z = max(dp[i - 1][0][0], max(dp[i - 1][1][0], dp[i - 1][1][1] + 1)); o = max(dp[i - 1][1][1], max(dp[i - 1][0][1], dp[i - 1][0][0] + 1)); } else { o = max(dp[i - 1][0][1], max(dp[i - 1][1][1], dp[i - 1][1][0] + 1)); z = max(dp[i - 1][1][0], max(dp[i - 1][0][0], dp[i - 1][0][1] + 1)); } return; } solve(i - 1, 2); solve(i - 1, 1); if (s[i] == 1) { z = max(dp[i - 1][2][0], max(dp[i - 1][1][0], dp[i - 1][1][1] + 1)); o = max(dp[i - 1][1][1], max(dp[i - 1][2][1], dp[i - 1][2][0] + 1)); } else { o = max(dp[i - 1][2][1], max(dp[i - 1][1][1], dp[i - 1][1][0] + 1)); z = max(dp[i - 1][1][0], max(dp[i - 1][2][0], dp[i - 1][2][1] + 1)); } return; } int main() { int n; while (cin >> n) { memset(dp, -1, sizeof dp); for (int i = 0; i < 3; ++i) dp[0][i][0] = dp[0][i][1] = 0; for (int i = 1; i <= n; ++i) scanf("%1d", s + i); solve(n, 2); cout << max(dp[n][2][0], dp[n][2][1]) << 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() { int n, i, x, y; string s; cin >> n >> s; x = 1; for (i = 1; i < n; i++) { if (s[0] != s[i]) x++; s[0] = s[i]; } y = min(2, n - x); cout << x + y; }
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() b=raw_input() a,v = 1,0 l = b[0] for i in range(1,n): if b[i] != l: l = b[i] a += 1 if b[i] == b[i-1]: v = 1 print min(a+v*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
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const long long INF = 1e9 + 19; int n; char s[N]; void read() { scanf("%d", &n); scanf("%s", s); int cnt = 0; int cntE = 0; for (int i = 0; i < n - 1; i++) { if (s[i] == s[i + 1]) cntE++; else cnt++; } cnt += min(2, cntE); printf("%d\n", cnt + 1); } void solve() {} void printAns() {} void stress() {} int main() { if (1) { int k = 1; for (int tt = 0; tt < k; tt++) { read(); solve(); printAns(); } } else { stress(); } 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.InputStreamReader; 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.List; import java.util.Map; import java.util.Set; public class test { private static void maxScore(String s) { int len = 1; for(int i=1; i<s.length(); i++) len += (s.charAt(i)!=s.charAt(i-1)) ? 1 : 0; System.out.println(Math.min(len+2, s.length())); } public static void main(String[] args) throws InterruptedException { //new careercup().run(); //new CC().run(); try{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br.readLine(); String s = br.readLine(); maxScore(s); }catch(IOException io){ io.printStackTrace(); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const long long linf = 1000000000000000043; const int inf = 1000000043; void pause(bool a = true) {} template <class T> inline T prod(pair<T, T> a, pair<T, T> b, pair<T, T> c = make_pair(0, 0)) { return (a.first - c.first) * (b.second - c.second) - (a.second - c.second) * (b.first - c.first); } template <class T> inline bool intriangle(pair<T, T> a, pair<T, T> b, pair<T, T> c, pair<T, T> d) { return (abs(prod(a, b, c)) == abs(prod(a, b, d)) + abs(prod(a, c, d)) + abs(prod(b, c, d))); } template <class T> inline int sg(T a) { return (a < 0 ? -1 : a ? 1 : 0); } long long rng(long long a, long long b) { return (((((long long)rand()) << 16) + rand()) % (b - a + 1)) + a; } bool inConvPoly(vector<pair<long long, long long>> &a, pair<long long, long long> b) { long long l = 1; long long r = a.size() - 1; if (sg(prod(a[l], b, a[0])) * sg(prod(a[r], b, a[0])) >= 0) return false; if (intriangle(a[l], a[r], a[0], b)) return true; while (l != r - 1) (prod(a[(l + r) / 2], b, a[0]) < 0) ? l = (l + r) / 2 : r = (l + r) / 2; return sg(prod(b, a[r], a[l])) * sg(prod(b, a[0], a[l])) < 0; } long long gcd(long long a, long long b) { while (b) { a %= b; swap(a, b); } return a; } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } template <class T> const T sqr(const T x) { return abs(x * x); } unsigned long long powi(unsigned long long a, unsigned long long b, long long mod = 1000000007) { if (b < 2) return b ? a : 1; return (powi((a * a) % mod, b / 2, mod) * (b % 2 ? a : 1)) % mod; } bool isn(char c) { return ('0' <= c && c <= '9'); } template <class T> bool read(T &out) { int c = getchar(), sign = 1; T num = 0; for (; !isn(c) && c != '-'; c = getchar()) if (c == EOF) return false; if (c == '-') { sign = -1; c = getchar(); } if (c == EOF) return false; for (; isn(c); c = getchar()) num = num * 10 + c - '0'; out = sign * num; return true; } long long time_limit = 1000000; long long start = clock(); bool time_break() { int g = clock(); return (clock() - start > time_limit * 0.8); } long long fact(long long n, long long mod = linf) { long long g = 1; for (int i = 0; i < (n - 1); i++) g *= (i + 1); return g; } void init(long long tl = 1000000) { ios::sync_with_stdio(false); srand(time(0)); time_limit = tl; } template <class T> void maxz(T &a, T b) { a = max(a, b); } template <class T> void minz(T &a, T b) { a = min(a, b); } long long mod = 1000000007; bool isprime(long long n) { if (n < 2) return 0; for (long long i = 2; i * i <= n; i++) if (n % i == 0) return 0; return 1; } long long randLL() { return (((long long)rand()) << 47) + (((long long)rand()) << 31) + (((long long)rand()) << 15) + (((long long)rand()) >> 1); } bool isprimeL(long long n, int it = 50) { if (n < 2) return 0; if (n == 2) return 1; if (n % 2 == 0) return 0; for (int i = 0; i < (it); i++) { long long x = randLL() % (n - 1) + 1; if (gcd(x, n) != 1) return 0; if (powi(x, n - 1, n) != 1) return 0; } return 1; } bool check(const vector<long long> &a, long long s, long long k) { vector<long long> b(k, s); multiset<long long> st; for (int i = 0; i < (a.size()); i++) st.insert(-a[i]); for (int i = 0; i < (k); i++) { if (st.empty()) break; b[i] += *st.begin(); st.erase(st.begin()); if (st.empty()) break; if (st.lower_bound(-b[i]) != st.end()) { int g = b[i]; if (b[i] >= -(*st.lower_bound(-b[i]))) { b[i] += *st.lower_bound(-b[i]); st.erase(st.lower_bound(-g)); } } } return st.empty(); } int main() { init(); long long n; cin >> n; vector<long long> a(n); for (int i = 0; i < (n); i++) { char c; cin >> c; a[i] = c - '0'; } long long ans = 1; for (int i = 0; i < (n - 1); i++) { if (a[i] != a[i + 1]) ans++; } long long cnt = 0; for (int i = 0; i < (n - 1); i++) cnt += (a[i] == a[i + 1]); if (cnt >= 2) ans += 2; if (cnt == 1) ans += 1; cout << ans << endl; pause(); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int n, ans = 1; string s; int main() { cin >> n; cin >> s; for (int i = 1; i < s.size(); i++) if (s[i - 1] != s[i]) ans++; 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; const int N = (int)2e5 + 10; const int INF = (int)1e9; const int mod = (int)1e9 + 7; const long long LLINF = (long long)1e18; int n; string s; int main() { cin >> n >> s; int len = 1; for (int i = 1; i < n; ++i) { if (s[i] == s[i - 1]) continue; ++len; } len = min(n, len + 2); cout << len; 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
l, s = int(input()), input() x, y = 0, 1 for i in range(l-1): if s[i] == s[i+1]: x += 1 else: y += 1 print(y + min(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
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.io.ObjectInputStream.GetField; 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.Set; import java.util.StringTokenizer; import java.util.TimeZone; import java.util.TreeMap; import java.text.*; public class Main { static int n ; static int memo[][][][]=new int[100000][2][2][2]; static char arr[]; public static int solve(int idx , int prev,int flag,int change) { if(idx==n) return 0 ; if(memo[idx][prev][flag][change]!=-1) return memo[idx][prev][flag][change]; int max=Integer.MIN_VALUE; if((arr[idx]-'0')!=prev) max=1+solve(idx+1,arr[idx]-'0',flag,0); else { int max1=solve(idx+1,prev,flag,0),max2=Integer.MIN_VALUE; if(flag==0||change==1) max2=(arr[idx]-'0'==prev?1:0)+solve(idx+1,(1^(arr[idx]-'0')),1,1); return memo[idx][prev][flag][change]=Math.max(max1,max2); } return memo[idx][prev][flag][change]=max; } public static void main(String[] args) throws IOException { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); n=in.nextInt(); arr=in.next().toCharArray(); for(int i=0;i<memo.length;i++) for(int j=0;j<memo[i].length;j++) for(int k=0;k<memo[i][j].length;k++) for(int f=0;f<memo[i][j][k].length;f++) memo[i][j][k][f]=-1; int res=1+solve(1,arr[0]-'0',0,0); out.println(res); out.close(); } } 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()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(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
def func(a): l=[] for i in a: l.append(int(i)) inc=1 dec=1 for i in range(1,len(l)): if l[i]>l[i-1]: inc=dec+1 if l[i]<l[i-1]: dec=inc+1 return max(inc,dec) n=int(input()) l=list(input()) if len(l)==2: print(2) else: ans=0 f=0 for i in range(len(l)-1): if l[i]==l[i+1]: f=1 break if f==1: st=i+1 while st+1<len(l) and l[st]!=l[st+1]: st+=1 range1=[i+1,st] for p in range(i+1,st+1): l[p]=1-int(l[p]) #print(l) print(func(l))
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.IOException; import java.util.InputMismatchException; public class AlternativeThinking { public static void main(String[] args) { FasterScanner sc = new FasterScanner(); int N = sc.nextInt(); char[] S = sc.nextLine().toCharArray(); int[] nf0 = new int[N + 1]; int[] nf1 = new int[N + 1]; int[] if0 = new int[N + 1]; int[] if1 = new int[N + 1]; int[] df0 = new int[N + 1]; int[] df1 = new int[N + 1]; for (int i = 1; i <= N; i++) { char c = S[i - 1]; if (c == '0') { nf0[i] = Math.max(nf0[i - 1], nf1[i - 1] + 1); nf1[i] = nf1[i - 1]; if0[i] = if0[i - 1]; if1[i] = Math.max(Math.max(nf0[i - 1] + 1, nf1[i - 1]), Math.max(if0[i - 1] + 1, if1[i - 1])); df0[i] = Math.max(Math.max(df0[i - 1], df1[i - 1] + 1), Math.max(if0[i - 1], if1[i - 1] + 1)); df1[i] = df1[i - 1]; } else { nf0[i] = nf0[i - 1]; nf1[i] = Math.max(nf1[i - 1], nf0[i - 1] + 1); if0[i] = Math.max(Math.max(nf1[i - 1] + 1, nf0[i - 1]), Math.max(if1[i - 1] + 1, if0[i - 1])); if1[i] = if1[i - 1]; df1[i] = df0[i - 1]; df1[i] = Math.max(Math.max(df1[i - 1], df0[i - 1] + 1), Math.max(if1[i - 1], if0[i - 1] + 1)); } } System.out.println(Math.max(Math.max(Math.max(Math.max(Math.max(nf0[N], nf1[N]), if0[N]), if1[N]), df0[N]), df1[N])); } public static class FasterScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { 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 int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
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() + '2' ans = cur = 1 Max = j = 0 flag = False cnt2 = 0 for i in range(1, n + 1): if s[i] != s[j]: j = i if s[i] != '2': ans += 1 if cur >= 3: flag = True elif cur >= 2: cnt2 += 1 cur = 0 cur += 1 if flag or cnt2 >= 2: ans += 2 elif cnt2 == 1: 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 java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.ObjectInputStream.GetField; 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.StringTokenizer; import java.util.TreeMap; public class Main { public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int n =in.nextInt(),cnt=0,res=1; String s =in.next(); for(int i=0;i<n-1;i++) if(s.charAt(i)==s.charAt(i+1)) cnt++; int now=s.charAt(0); for(int i=1;i<n;i++) if(s.charAt(i)!=now) { res++; now=s.charAt(i); } if(cnt>=2) System.out.println(res+2); else if(cnt==1) System.out.println(res+1); else System.out.println(res); out.close(); } } 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()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(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; void pv(vector<long long int> arr, long long int size) { for (long long int i = 0; i < size; i++) { cout << arr[i] << ' '; } } void gv(vector<long long int> &arr) { long long int size = arr.size(); for (long long int i = 0; i < size; i++) { cin >> arr[i]; } } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n; cin >> n; string s; cin >> s; long long int c = 0; for (long long int i = 0; i < n - 1; i++) { if (s[i] != s[i + 1]) c++; } cout << min(n, c + 3); 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 BinarySearch(int *p, int key, int size) { int l = 0, r = size - 1; if (p[(r - l) / 2] == key) return (r - l) / 2; else if (p[(r - l) / 2] > key) { r = (r - l) / 2 - 1; } else l = (r - l) / 2 + 1; } char change(char p) { if (p == '1') return '0'; else return '1'; } int main(int argc, char **argv) { int n, otv = 0; cin >> n; string str; char next; cin >> str; next = str[0]; for (int i = 0; i < n; i++) { if (str[i] == next) { otv++; next = change(next); } } int counter = 0; for (int i = 0, j = 0; (str.find("00", j) != string::npos) || (str.find("11", i) != string::npos);) { if (str.find("11", i) != string::npos) { counter++; i = str.find("11", i) + 1; } if (counter == 2) break; if (str.find("00", j) != string::npos) { counter++; j = str.find("00", j) + 1; } if (counter == 2) break; } cout << otv + fmin(2, counter); 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.IOException; import java.io.InputStream; import java.util.InputMismatchException; public class A { static int[] array; static int[] one; static int[] zero; static Integer[][] memo; public static void main(String[] args) { FinalScanner sc = new FinalScanner(); int N = sc.nextInt(); memo = new Integer[N][3]; String str = sc.next(); array= new int[str.length()]; one = new int[str.length()]; zero = new int[str.length()]; for(int a=0;a<N;a++){ if(str.charAt(a)=='1')array[a]=1; } int O = N; int Z = N; for(int a=N-1;a>=0;a--){ one[a]=O; zero[a]=Z; if(array[a]==1){ O=a; } else{ Z=a; } } System.out.println(solve(0,0)); } private static int solve(int i, int magic) { if(i==array.length)return 0; if(memo[i][magic]!=null)return memo[i][magic]; int ans = 1; if(array[i]==1) ans = Math.max(ans,1+solve(zero[i],magic)); else ans = Math.max(ans,1+solve(one[i],magic)); if(magic==0||magic==1){ if(array[i]==1) ans = Math.max(ans,1+solve(one[i],magic+1)); else ans = Math.max(ans,1+solve(zero[i],magic+1)); } return memo[i][magic]=Math.max(ans,solve(i+1,magic)); } static class FinalScanner{ private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FinalScanner(){ stream = System.in; //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[] nextInt(int v){ int[] temp = new int[v]; for(int a=0;a<v;a++)temp[a]=nextInt(); return temp; } long nextLong(){ return Long.parseLong(next()); } long[] nextLong(int v){ long[] temp = new long[v]; for(int a=0;a<v;a++)temp[a]=nextLong(); return temp; } double nextDouble(){ return Double.parseDouble(next()); } double[] nextDouble(int v){ double[] temp = new double[v]; for(int a=0;a<v;a++)temp[a]=nextDouble(); return temp; } 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[] next(int v){ String[] temp = new String[v]; for(int a=0;a<v;a++)temp[a]=next(); return temp; } 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(); } String[] nextLine(int v){ String[] temp = new String[v]; for(int a=0;a<v;a++)temp[a]=nextLine(); return temp; } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.*; import java.util.*; public class Solution { static void solve(InputReader in, OutputWriter out) { int n = in.nextInt(); String s = in.next(); int cnt = 0; char prev = '2'; for (int i = 0; i < n; i++) if (s.charAt(i) != prev) { cnt++; prev = s.charAt(i); } out.print(Math.min(n, cnt + 2)); } public static void main(String[] args) { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); solve(in, out); in.close(); out.close(); } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); st = null; } String nextLine() { String line = null; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return line; } String next() { while (st == null || !st.hasMoreTokens()) { String line = nextLine(); if (line == null) return null; st = new StringTokenizer(line); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } static class OutputWriter { BufferedWriter bw; OutputWriter(OutputStream os) { bw = new BufferedWriter(new OutputStreamWriter(os)); } void print(int i) { print(Integer.toString(i)); } void println(int i) { println(Integer.toString(i)); } void print(long l) { print(Long.toString(l)); } void println(long l) { println(Long.toString(l)); } void print(double d) { print(Double.toString(d)); } void println(double d) { println(Double.toString(d)); } void print(boolean b) { print(Boolean.toString(b)); } void println(boolean b) { println(Boolean.toString(b)); } void print(char c) { try { bw.write(c); } catch (IOException e) { e.printStackTrace(); } } void println(char c) { println(Character.toString(c)); } void print(String s) { try { bw.write(s); } catch (IOException e) { e.printStackTrace(); } } void println(String s) { print(s); print('\n'); } void close() { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
if __name__ == "__main__": n = int( input().strip() ) s = input().strip() segs = [] start = 0 i = 1 while i <= n: if i == n or s[i] != s[start]: segs.append( i - start ) start = i i = start + 1 else: i += 1 res = len(segs) segs.sort() if segs[-1] >= 3: res += 2 elif len(segs) >= 2 and segs[-1] >= 2 and segs[-2] >= 2: res += 2 elif segs[-1] >= 2: res += 1 print( 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.*; import java.io.*; public class Alternative_Thinking { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw=new PrintWriter(System.out); int t=1; while(t-->0) { int n=Integer.parseInt(br.readLine()); char a[]=br.readLine().trim().toCharArray(); int dp[][]=new int[n+1][3]; //// 1010101010101....... for(int i=1;i<=n;i++) { if(a[i-1]=='0') { dp[i][0]=dp[i-1][0]%2==1?dp[i-1][0]+1:dp[i-1][0]; int x=Math.max(dp[i-1][0],dp[i-1][1]); dp[i][1]=x%2==0?x+1:x; x=Math.max(x,dp[i-1][2]); dp[i][2]=x%2==1?x+1:x; } else { dp[i][0]=dp[i-1][0]%2==0?dp[i-1][0]+1:dp[i-1][0]; int x=Math.max(dp[i-1][0],dp[i-1][1]); dp[i][1]=x%2==1?x+1:x; x=Math.max(x,dp[i-1][2]); dp[i][2]=x%2==0?x+1:x; } } int ans1=Math.max(dp[n][0],Math.max(dp[n][1],dp[n][2])); dp=new int[n+1][3]; //// 010101010101....... for(int i=1;i<=n;i++) { if(a[i-1]=='1') { dp[i][0]=dp[i-1][0]%2==1?dp[i-1][0]+1:dp[i-1][0]; int x=Math.max(dp[i-1][0],dp[i-1][1]); dp[i][1]=x%2==0?x+1:x; x=Math.max(x,dp[i-1][2]); dp[i][2]=x%2==1?x+1:x; } else { dp[i][0]=dp[i-1][0]%2==0?dp[i-1][0]+1:dp[i-1][0]; int x=Math.max(dp[i-1][0],dp[i-1][1]); dp[i][1]=x%2==1?x+1:x; x=Math.max(x,dp[i-1][2]); dp[i][2]=x%2==0?x+1:x; } } int ans2=Math.max(dp[n][0],Math.max(dp[n][1],dp[n][2])); pw.println(Math.max(ans2,ans1)); } pw.flush(); } }
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() { long long n, i, c, k = 0, l = 0, t; string s; vector<long long> v; cin >> n >> s; c = 1; for (i = 0; i < n - 1; i++) { if (s[i] != s[i + 1]) { v.push_back(c); c = 1; } else { c++; } } v.push_back(c); t = v.size(); for (i = 0; i < t; i++) { if (v[i] > 2) { k = 1; break; } } if (k == 1) { cout << t + 2; return 0; } for (i = 0; i < t; i++) { if (v[i] == 2) { l++; } } if (l >= 2) { cout << t + 2; } else if (l == 1) { cout << t + 1; } else { cout << t; } }
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.InputStream; import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.IOException; import java.util.InputMismatchException; import java.io.PrintWriter; import java.io.OutputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Alex */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(), res = 0; char[] input = in.next().toCharArray(); int[][] initial = new int[n + 1][2], flipped = new int[n + 1][2], done = new int[n + 1][2]; for(int i = 1; i <= n; i++) { char c = input[i - 1]; if(c == '1') initial[i][1] = Math.max(initial[i - 1][0] + 1, initial[i - 1][1]); else initial[i][0] = Math.max(initial[i - 1][1] + 1, initial[i - 1][0]); if(c == '1') flipped[i][1] = Math.max(initial[i - 1][1] + 1, flipped[i - 1][1]); else flipped[i][0] = Math.max(initial[i - 1][0] + 1, flipped[i - 1][0]); if(c == '1') flipped[i][1] = Math.max(flipped[i][1], flipped[i - 1][0] + 1); else flipped[i][0] = Math.max(flipped[i][0], flipped[i - 1][1] + 1); if(c == '1') done[i][1] = Math.max(flipped[i - 1][1] + 1, done[i - 1][1]); else done[i][0] = Math.max(flipped[i - 1][0] + 1, done[i - 1][0]); if(c == '1') done[i][1] = Math.max(done[i][1], done[i - 1][0] + 1); else done[i][0] = Math.max(done[i][0], done[i - 1][1] + 1); } out.printLine(Math.max(done[n][0], done[n][1])); } } 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 printLine(int i) { writer.println(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
var input = readline().split(' ').map(Number), n = input[0]; var str = readline(); var counter = 0; for (var i = 1; i < str.length; i++) { if (str[i] != str[i-1]) counter++; } write(Math.min(counter + 3, 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
n=int(input()) a=input() p=[1]*4 for i in range(1,n): for j in range(2,-1,-1): p[j+1]=max(p[j+1],p[j]+(a[i-1]==a[i])) p[j]+=a[i-1]!=a[i] print(max(p[0:3]))
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> int main() { int n, ans; char a[100000]; scanf("%d", &n); scanf("%s", a); ans = 1; char cur = a[0]; for (int i = 1; i < n; i++) if (a[i] != cur) { ans++; cur = a[i]; } if (ans >= n - 1) ans = n; else ans += 2; printf("%d\n", ans); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.*; //PrintWriter import java.math.*; //BigInteger, BigDecimal import java.util.*; //StringTokenizer, ArrayList public class R334_Div2_C { FastReader in; PrintWriter out; public static void main(String[] args) { new R334_Div2_C().run(); } void run() { in = new FastReader(System.in); out = new PrintWriter(System.out); solve(); out.close(); } void solve() { int n = in.nextInt(); String s = in.next(); int cnt = 1, doub = 0, contig = 1; boolean triple = false; for (int i = 1; i < n; i++) { if (s.charAt(i) != s.charAt(i-1)) { if (contig >= 3) triple = true; else if (contig == 2) doub++; contig = 0; cnt++; } contig++; } if (contig >= 3) triple = true; else if (contig == 2) doub++; if (triple || doub >= 2) out.println(cnt + 2); else if (doub == 1) out.println(cnt + 1); else out.println(cnt); } //----------------------------------------------------- void runWithFiles() { in = new FastReader(new File("input.txt")); try { out = new PrintWriter(new File("output.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } solve(); out.close(); } class FastReader { BufferedReader br; StringTokenizer tokenizer; public FastReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public FastReader(File f) { try { br = new BufferedReader(new FileReader(f)); tokenizer = null; } catch (FileNotFoundException e) { e.printStackTrace(); } } private String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) try { tokenizer = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } return tokenizer.nextToken(); } public String nextLine() { try { return br.readLine(); } catch(Exception e) { throw(new RuntimeException()); } } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } BigInteger nextBigInteger() { return new BigInteger(next()); } BigDecimal nextBigDecimal() { return new BigDecimal(next()); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int ii[100005], in[100005], ni[100005], nn[100005]; char text[100005]; int arr[100005]; int main() { int n; cin >> n; cin >> text; for (int i = 0; i < n; i++) arr[i] = text[i] - '0'; ii[0] = 0; in[0] = 1; ni[0] = 0; nn[0] = 1; for (int i = 1; i < n; i++) { ii[i] = max(ii[i - 1], in[i - 1]); in[i] = nn[i - 1]; nn[i] = nn[i - 1]; if ((1 - arr[i]) != (1 - arr[i - 1])) { ii[i]++; } if ((1 - arr[i]) != (arr[i - 1])) { in[i]++; } if (arr[i] != (1 - arr[i - 1])) { ni[i] = max(max(ii[i - 1], in[i - 1]) + 1, ni[i - 1]); } if (arr[i] != arr[i - 1]) { ni[i] = max(max(ii[i - 1], in[i - 1]), ni[i - 1] + 1); nn[i]++; } } int maxi = max(ii[n - 1], in[n - 1]); maxi = max(maxi, ni[n - 1]); maxi = max(maxi, nn[n - 1]); cout << maxi << '\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
# -*- coding: utf-8 -*- """ Created on Thu Sep 10 11:34:34 2020 @author: mahedi """ res=1 n=int(input()) s=input() for i in range(1,n): res=res+(s[i]!=s[i-1]) k=min(n,res+2) print(k)
PYTHON3