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
import java.util.*; import static java.lang.Math.*; import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.math.MathContext; public class A { static class pair implements Comparable<pair> { int x; int y; public pair(int d, int k) { x = d; y = k; } @Override public int compareTo(pair arg0) { // TODO Auto-generated method stub if (x == arg0.x) { return y - arg0.y; } return x - arg0.x; } } static PrintWriter pw; static int n; static int m; static int bin(int num) { int s = 0; int e = a.length - 1; int ans = -1; while (s <= e) { int mid = (s + e) / 2; if (a[mid] <= num) { s = mid + 1; ans = mid; } else e = mid - 1; } return ans; } static int[][] memo; // // static int dp(int idx, int r) { // if (r <= 0) // return 0; // if (memo[idx][r] != -1) // return memo[idx][r]; // int min = 1000000000; // for (int i = 0; i < n; i++) { // if (p[i].y > p[idx].y && p[i].x != p[idx].x) // min = Math.min(min, Math.abs(i - idx) + dp(i, r - p[i].y)); // } // // return memo[idx][r] = min; // } static char[] a; public static void main(String[] args) throws NumberFormatException, IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); a = br.readLine().toCharArray(); memo = new int[2][n]; Arrays.fill(memo[0], -1); Arrays.fill(memo[1], -1); // System.out.println(dp(0,0)); boolean f = false; int sum = 0; int d = 0; for (int i = 0; i < a.length;) { int c = 1; i++; while (i < n && a[i] == a[i - 1]) { c++; i++; } if (c > 2) f = true; else if (c == 2) d++; sum++; } if (f) pw.println(sum + 2); else { if (d > 1) pw.println(sum + 2); else if (d == 1) pw.println(sum + 1); else pw.println(sum); } pw.flush(); pw.close(); } static boolean check(String s, int h, int m) { String s2 = reversString(m + "") + ":" + reversString(h + ""); return s2.equals(com(h, m)); } static String com(int h, int m) { String s = ""; if (h < 10) s += "0" + h; else { s += h; } s += ":"; if (m < 10) s += "0" + m; else { s += m; } return s; } static String reversString(String x) { int y = Integer.parseInt(x); if (y < 10) return x + "0"; else return x.charAt(1) + "" + x.charAt(0); } static boolean[] vis; static ArrayList<Integer>[] g; static ArrayList<Integer> r; static int[] sta; static int[] end; // static void dfs(int n) { // vis[n] = true; // for (int v : g[n]) { // boolean f = false; // if (!vis[v]) { // p.add(new triple(n + 1, v + 1)); // f = true; // dfs(v); // break; // } // if (!f) { // p.add(new triple(n + 1, g[n].get(0) + 1)); // } // } // } static void bfs(int s) { Queue<triple> q = new LinkedList<triple>(); q.add(new triple(s, 1, 0, 0)); vis[s] = true; while (!q.isEmpty()) { triple p = q.remove(); int u = p.x; int c = p.y; int even = p.z; int odd = p.w; // System.out.println(odd + " " + even); if (c == 0) { int l = even % 2 == 1 ? 1 - sta[u] : sta[u]; if (l == end[u]) { for (int v : g[u]) { if (!vis[v]) { vis[v] = true; q.add(new triple(v, 1 - c, even, odd)); } } } else { r.add(u + 1); even = 1 - even; for (int v : g[u]) { if (!vis[v]) { vis[v] = true; q.add(new triple(v, 1 - c, even, odd)); } } } } else { int l = odd % 2 == 1 ? 1 - sta[u] : sta[u]; if (l == end[u]) { for (int v : g[u]) { if (!vis[v]) { vis[v] = true; q.add(new triple(v, 1 - c, even, odd)); } } } else { r.add(u + 1); odd = 1 - odd; for (int v : g[u]) { if (!vis[v]) { vis[v] = true; q.add(new triple(v, 1 - c, even, odd)); } } } } } } static class triple implements Comparable<triple> { int x; int y; int z; int w; public triple(int a, int b, int c, int d) { x = a; y = b; z = c; w = d; } @Override public int compareTo(triple o) { // TODO Auto-generated method stub return y - o.y; } } static long pow(long a, long e) // O(log e) { long res = 1; while (e > 0) { if ((e & 1) == 1) res *= a; a *= a; e >>= 1; } return res; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } }
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() { ios_base::sync_with_stdio(false); int n; string s; cin >> n; cin >> s; int count = 1; for (int i = 1; i < n; i++) { if (s[i] != s[i - 1]) count++; } cout << min(n, count + 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
import java.io.*; import java.util.*; public class alternativethinking { private static InputReader in; private static PrintWriter out; public static void main(String[] args) throws IOException { in = new InputReader(System.in); out = new PrintWriter(System.out, true); int n = in.nextInt(); char[] c = in.next().toCharArray(); int[][] dp = new int[3][2]; for (int i = 0; i < n; i++) { int k = c[i] - '0'; int[][] next = new int[3][2]; for (int j = 0; j < 3; j++) for (int r = 0; r < 2; r++) next[j][r] = dp[j][r]; next[0][k] = Math.max(dp[0][k], dp[0][1-k]+1); next[1][k] = Math.max(dp[1][k], Math.max(dp[1][1-k]+1,dp[0][k]+1)); next[2][k] = Math.max(dp[2][k], Math.max(dp[2][1-k]+1,dp[1][k]+1)); dp = next; } int max = 0; for (int i = 0; i < 3; i++) max = Math.max(max, Math.max(dp[i][0],dp[i][1])); out.println(max); out.close(); System.exit(0); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n = int(input()) s = input() cnt = 1 last = s[0] for i in range(1, len(s)): if s[i] != last: cnt += 1 last = s[i] if s.count('111') or s.count('000') or s.count('00')+s.count('11') > 1: print(cnt+2) elif s.count('11')+s.count('00'): print(cnt+1) else: print(cnt)
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; long long n, prv[100005][2]; bool a[100005]; long long dp[100005][3]; long long solve(long long i, long long flip) { if (i == -1) return 0; if (dp[i][flip] == -1) { long long ans = 0; long long prev_op = prv[i][!a[i]], prev_same = prv[i][a[i]]; if (flip == 2) { ans = max(ans, 1 + solve(prev_op, 2)); ans = max(ans, 1 + solve(prev_same, 1)); } else if (flip == 1) { ans = max(ans, 1 + solve(prev_op, 1)); ans = max(ans, 1 + solve(prev_same, 0)); } else ans = max(ans, 1 + solve(prev_op, 0)); dp[i][flip] = ans; } return dp[i][flip]; } int main() { memset(dp, -1, sizeof(dp)); scanf("%lld", &n); string temp; cin >> temp; for (int i = 0; i < n; i++) a[i] = temp[i] - '0'; prv[0][0] = prv[0][1] = -1; for (int i = 0; i < n - 1; i++) { prv[i + 1][0] = prv[i][0]; prv[i + 1][1] = prv[i][1]; prv[i + 1][a[i]] = i; } long long ans = 0; for (int i = 0; i < n; i++) { long long ans_i = solve(i, 2); ans = max(ans, ans_i); } printf("%lld", 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
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //package codeforces334; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * * @author Sourav Kumar Paul */ public class SolveC { public static void main(String[] args) throws IOException{ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int test = Integer.parseInt(reader.readLine()); String line = reader.readLine(); int res =1; for(int i=1; i<test; i++) { if(line.charAt(i) != line.charAt(i-1)) res++; } System.out.println(Math.min(res+2, test)); } }
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> int n; char d[100001]; int main() { int i, r; scanf("%d%s", &n, d); r = 1; for (i = 0; i + 1 < n; i++) { if (d[i] != d[i + 1]) r++; } if (r == n) { } else if (r == n - 1) { r++; } else { r += 2; } printf("%d", r); 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 int INF = 0x3f3f3f3f; const double PI = acos(-1.0); const double EPS = 1e-8; using namespace std; const int N = 100005; char bin[N]; int dp[N][2][2]; int max(int a, int b, int c) { return max(a, max(b, c)); } int main() { int n; while (cin >> n) { scanf("%s", &bin); dp[0][0][0] = dp[0][1][1] = 1; for (int i = 1; i < n; ++i) { if (bin[i] == bin[i - 1]) { dp[i][0][0] = dp[i - 1][0][0]; dp[i][1][0] = max(dp[i - 1][1][0], dp[i - 1][1][1] + 1); dp[i][1][1] = max(dp[i - 1][0][0] + 1, dp[i - 1][1][1]); } else { dp[i][0][0] = dp[i - 1][0][0] + 1; dp[i][1][0] = max(dp[i - 1][1][0] + 1, dp[i - 1][1][1]); dp[i][1][1] = max(dp[i - 1][0][0], dp[i - 1][1][1] + 1); } } cout << max(dp[n - 1][1][0], dp[n - 1][1][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
import java.io.*; import java.util.*; public class contest { static HashMap<String,Integer> map = new HashMap<>(); static int gcd(int a,int b){ if(a==0){ return b; } return gcd(b%a,a); } public static void main(String[] args) { FastReader sc = new FastReader(); int t = 1; out:while(t-- >0) { int n =sc.nextInt(); String s = sc.nextLine(); int res = 0; int i =0; while(i<n){ res++; char k = s.charAt(i); int j = i; while(j<n && s.charAt(j)==k){ j++; } i = j; } System.out.println(Math.min(res+2,n)); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int inf = 1e9 + 10; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, c = 1; string s; cin >> n; cin >> s; for (int i = 1; i <= n - 1; i++) { if (s[i] != s[i - 1]) c++; } if (c + 2 <= n) cout << c + 2; else cout << 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
# -*- coding: utf-8 -*- import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll n = int(raw_input()) s = raw_input() two = {"0":0, "1":0} three = False cur = s[0] cnt = 1 ans = 1 for si in s[1:]: if si == cur: cnt += 1 if cnt == 2: two[si] += 1 if cnt == 3: three = True else: cur = si cnt = 1 ans += 1 if (two["0"] + two["1"] >= 2) or three: ans += 2 elif max(two["0"], two["1"]) > 0: ans += 1 print ans
PYTHON
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int n; char digits[100002]; int lasBeforeChangeWithZero[100002]; int lasBeforeChangeWithOne[100002]; int lasDuringChangeWithZero[100002]; int lasDuringChangeWithOne[100002]; int lasAfterChangeWithZero[100002]; int lasAfterChangeWithOne[100002]; int main() { scanf("%d", &n); getchar(); fgets(digits, n + 1, stdin); if (digits[0] == '0') { lasBeforeChangeWithZero[0] = 1; lasDuringChangeWithOne[0] = 1; } else { lasBeforeChangeWithOne[0] = 1; lasDuringChangeWithZero[0] = 1; } for (int i = 1; i < n; ++i) { lasBeforeChangeWithZero[i] = lasBeforeChangeWithZero[i - 1]; lasBeforeChangeWithOne[i] = lasBeforeChangeWithOne[i - 1]; lasDuringChangeWithZero[i] = lasDuringChangeWithZero[i - 1]; lasDuringChangeWithOne[i] = lasDuringChangeWithOne[i - 1]; lasAfterChangeWithZero[i] = lasAfterChangeWithZero[i - 1]; lasAfterChangeWithOne[i] = lasAfterChangeWithOne[i - 1]; if (digits[i] == '0') { lasBeforeChangeWithZero[i] = lasBeforeChangeWithOne[i - 1] + 1; lasDuringChangeWithOne[i] = max(lasDuringChangeWithZero[i - 1] + 1, lasBeforeChangeWithZero[i - 1] + 1); lasAfterChangeWithZero[i] = max(lasAfterChangeWithOne[i - 1] + 1, lasDuringChangeWithOne[i - 1] + 1); } else { lasBeforeChangeWithOne[i] = lasBeforeChangeWithZero[i - 1] + 1; lasDuringChangeWithZero[i] = max(lasDuringChangeWithOne[i - 1] + 1, lasBeforeChangeWithOne[i - 1] + 1); lasAfterChangeWithOne[i] = max(lasAfterChangeWithZero[i - 1] + 1, lasDuringChangeWithZero[i - 1] + 1); } } printf("%d", max(max(max(lasDuringChangeWithZero[n - 1], lasDuringChangeWithOne[n - 1]), lasAfterChangeWithZero[n - 1]), lasAfterChangeWithOne[n - 1])); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { InputReader in = new InputReader(System.in); int n = in.nextInt(); String s = in.next(); int length =1; int count =0; for(int i = 1; i< s.length(); i++){ if(s.charAt(i) == s.charAt(i-1)){ count ++; }else{ length ++; } } if(count >2){ count =2; } System.out.println(length + count); } } class InputReader { StringTokenizer tokenizer; BufferedReader reader; public InputReader(InputStream stream) { tokenizer = null; reader = new BufferedReader(new InputStreamReader(stream), 32768); } 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 String nextLine() { try { return reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return null; } }
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 md = 1e9 + 7; long long movesx[8] = {-1, 0, 1}; long long movesy[8] = {-1, 0, 1}; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long max(long long a, long long b) { return a > b ? a : b; } long long min(long long a, long long b) { return a < b ? a : b; } long long power(long long base, long long exponent, long long mod) { if (exponent == 0) return 1; long long temp = power(base, exponent / 2, mod); temp = (temp * temp) % mod; if (exponent % 2 != 0) { temp = (temp * base) % mod; return temp; } return temp; } long long ncr(long long a, long long b) { long long dp[b + 1]; memset(dp, 0, sizeof(dp)); dp[0] = 1; for (long long i = 1; i <= a; i++) for (long long j = min(i, b); j > 0; j--) dp[j] = (dp[j] + dp[j - 1]) % md; return dp[b]; } long long n, m, k; char ar[100005]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); { cin >> n; for (long long i = 0; i < n; i++) cin >> ar[i]; long long ans1 = 0, ans0 = 0; for (long long i = 0; i < n; i++) { if (ar[i] == '0') ans0 = max(ans1 + 1, ans0); else ans1 = max(ans0 + 1, ans1); } cout << min(n, max(ans0, ans1) + 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
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner in = new Scanner(System.in); int n =in.nextInt(); String resultados = in.next(); int res =1; for(int i=1;i < n;i++){ if(resultados.charAt(i)==resultados.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 main() { int n; char a[100005]; cin >> n; scanf("%s", a); int num = 1; for (int t = 1; t < n; t++) { if (a[t] != a[t - 1]) { num++; } } if (num + 2 < n) { printf("%d\n", num + 2); } else { printf("%d\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=input() a=raw_input() c=1 s=0 for i in range(1,n): if a[i]!=a[i-1]: c+=1 print min(n,c+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; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; string s; cin >> s; long long len = 1; for (long long i = 1; i < n; i++) if (s[i] != s[i - 1]) len++; cout << min(len + 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; long long inf = 1e18; long long mod = 1e9 + 7; long long delta = 10289; const long long M = 320; long long dp[M * M][M]; long long a[M * M]; int main() { long long n; cin >> n; char c, v; cin >> v; long long a = 0, b = 0; for (long long i = 1; i < n; i++) { cin >> c; a += (c == v); b += (c != v); v = c; } cout << 1 + b + min((long long)2, a); }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int N; string s; int main() { scanf("%d\n", &N); cin >> s; if (N == 1) { cout << 1 << endl; return 0; } if (N == 2) { cout << 2 << endl; return 0; } int ans = 1; for (int i = 1; i < N; i++) if (s[i] != s[i - 1]) ans++; if (ans == N) { cout << N << endl; return 0; } if (ans + 2 <= N) { cout << ans + 2 << endl; } else { cout << N << endl; } return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
//package alternativethinking; import java.util.*; import java.io.*; public class alternativethinking { public static void main(String[] args) throws IOException { //was thinking that we first remove the longest subsequence of alternating 1s and 0s. BufferedReader fin = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(fin.readLine()); char[] s = fin.readLine().toCharArray(); ArrayList<Integer> segments = new ArrayList<Integer>(); char prev = '3'; for(int i = 0; i < n; i++) { char next = s[i]; if(i == 0) { prev = next; segments.add(0); } else { if(prev != next) { prev = next; segments.add(0); } } segments.set(segments.size() - 1, segments.get(segments.size() - 1) + 1); } int numTwos = 0; int numThrees = 0; for(int i = 0; i < segments.size(); i++) { if(segments.get(i) == 2) { numTwos ++; } else if(segments.get(i) > 2) { numThrees ++; } } if(numThrees >= 1 || numTwos >= 2 || numThrees + numTwos >= 2) { System.out.println(segments.size() + 2); } else if(numTwos == 1) { System.out.println(segments.size() + 1); } else { System.out.println(segments.size()); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int ans = 0; int l = s.length(); int res = 0; for (int i = 0; i < l - 1; i++) { if (s[i] != s[i + 1]) { res++; } } for (int i = 0; i < l - 1; i++) { if (s[i] == s[i + 1]) { ans = ans + 1; } if (ans == 2) { break; } } cout << res + 1 + 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; typedef pair<long long, long long> ll; typedef vector<long long> vl; typedef vector<ll> vll; typedef vector<vl> vvl; template <typename T> ostream &operator<<(ostream &o, vector<T> v) { if (v.size() > 0) o << v[0]; for (unsigned i = 1; i < v.size(); i++) o << " " << v[i]; return o << "\n"; } template <typename U, typename V> ostream &operator<<(ostream &o, pair<U, V> p) { return o << "(" << p.first << ", " << p.second << ") "; } template <typename T> istream &operator>>(istream &in, vector<T> &v) { for (unsigned i = 0; i < v.size(); i++) in >> v[i]; return in; } template <typename T> istream &operator>>(istream &in, pair<T, T> &p) { in >> p.first; in >> p.second; return in; } int main() { std::ios::sync_with_stdio(false); long long n; cin >> n; string a; cin >> a; char c = a[0]; long long cnt = 1; for (long long i = (1); i < (long long)a.length(); i++) { if (a[i] != c) { cnt++; c = char((c - '0' + 1) % 2 + '0'); } } cout << min(n, cnt + 2); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; char s[100005]; int f[100005][2][3]; int main() { int n; scanf("%d", &n); scanf("%s", s + 1); for (int i = 1; i <= n; ++i) { int t = s[i] - 48; f[i][t][0] = f[i - 1][t ^ 1][0] + 1; f[i][t ^ 1][0] = f[i - 1][t ^ 1][0]; f[i][t][1] = max(f[i - 1][t ^ 1][1], f[i - 1][t ^ 1][2]) + 1; f[i][t ^ 1][1] = max(f[i - 1][t ^ 1][1], f[i - 1][t ^ 1][2]); f[i][t ^ 1][2] = max(f[i - 1][t][2], f[i - 1][t][0]) + 1; f[i][t][2] = max(f[i - 1][t][2], f[i - 1][t][0]); } int ans = 0; for (int i = 0; i < 2; ++i) for (int j = 0; j < 3; ++j) ans = max(ans, f[n][i][j]); cout << ans << endl; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.io.IOException; 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); ProblemAAlternativeThinking solver = new ProblemAAlternativeThinking(); solver.solve(1, in, out); out.close(); } static class ProblemAAlternativeThinking { int n; int[] a; int[][][] dp; public void solve(int testNumber, InputReader in, PrintWriter out) { n = in.readInt(); String str = in.readLine(); a = new int[n]; for (int i = 0; i < n; i++) a[i] = str.charAt(i) - '0'; dp = new int[n][2][3]; for (int[][] i : dp) for (int[] j : i) Arrays.fill(j, -1); out.println(Math.max(go(0, 0, 0), go(0, 1, 0))); } int go(int pos, int start, int flip) { if (pos >= n) return 0; if (dp[pos][start][flip] != -1) return dp[pos][start][flip]; int ans = 0; if (start == a[pos]) { ans = Math.max(ans, 1 + go(pos + 1, 1 - start, flip > 0 ? 2 : flip)); if (flip == 1) ans = Math.max(ans, go(pos + 1, start, flip)); } else { if (flip == 2 || flip == 0) ans = Math.max(ans, go(pos + 1, start, flip)); if (flip == 1) { ans = Math.max(ans, 1 + go(pos + 1, 1 - start, flip)); } if (flip == 0) { ans = Math.max(ans, 1 + go(pos + 1, 1 - start, 1)); } } return dp[pos][start][flip] = ans; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.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 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; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') buf.appendCodePoint(c); c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) s = readLine0(); return s; } 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.*; import java.util.*; public class CF334C{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int n = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); int v0 = 0; int v1 = 0; int vi0 = 0; int vi1 = 0; int vp0 = 0; int vp1 = 0; int nv0 = 0; int nv1 = 0; int nvi0 = 0; int nvi1 = 0; int nvp0 = 0; int nvp1 = 0; for (int i = 0; i < n; i++) { if (s.charAt(i) == '0') { nv0 = Math.max(v0, v1+1); nv1 = v1; nvi0 = vi0; nvi1 = Math.max(v0+1, Math.max(v1, Math.max(vi0+1, vi1))); nvp0 = Math.max(vi0, Math.max(vi1+1, Math.max(vp0, vp1+1))); nvp1 = vp1; } else { nv0 = v0; nv1 = Math.max(v0+1, v1); nvi0 = Math.max(v0, Math.max(v1+1, Math.max(vi0, vi1+1))); nvi1 = vi1; nvp0 = vp0; nvp1 = Math.max(vi0+1, Math.max(vi1, Math.max(vp0+1, vp1))); } v0 = nv0; v1 = nv1; vi0 = nvi0; vi1 = nvi1; vp0 = nvp0; vp1 = nvp1; } out.println(Math.max(v0, Math.max(v1, Math.max(vi0, Math.max(vi1, Math.max(vp0, vp1)))))); out.close(); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; int ans = 1; for (int i = 1; i < n; i++) if (s[i] != s[i - 1]) ans++; int count = 0; for (int i = 1; i < n; i++) if (s[i] == s[i - 1]) count++; cout << ans + min(2, count); 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 Mohammad { public static void Mohammad_AboHasan() throws IOException{ FastScanner fs = new FastScanner(); int n = fs.nextInt(), c = 1; String s = fs.next(); for (int i = 0; i < n-1; i++) { c += s.charAt(i) != s.charAt(i+1) ? 1 : 0; } System.out.println(Math.min(c + 2, n)); } public static void main(String[] args) throws IOException{ Mohammad_AboHasan(); } static class FastScanner{ BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException{ while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next()); } long nextLong() throws IOException{ return Long.parseLong(next()); } double nextDouble() throws IOException{ return Double.parseDouble(next()); } String nextLine() throws IOException{ String s = br.readLine(); return s; } } }
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 int N = 200005; const long long int mod = 1e9 + 7; long long int dp[200005][2][3]; long long int n; char s[200005]; long long int A[N], B[N]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> s; long long int i, j, k; for (i = 1; i <= n; i++) { A[i] = (s[i - 1] - '0'); } for (i = 1; i <= n; i++) { bool x = s[i - 1] - '0'; for (j = 0; j < 3; j++) { for (k = 0; k < 2; k++) dp[i][k][j] = dp[i - 1][k][j]; } dp[i][x][0] = max(dp[i][x][0], dp[i - 1][!x][0] + 1); dp[i][x][1] = max(dp[i][x][1], dp[i - 1][!x][1] + 1); dp[i][x][2] = max(dp[i][x][2], dp[i - 1][!x][2] + 1); dp[i][!x][1] = max(dp[i][!x][1], dp[i][x][0]); dp[i][x][1] = max(dp[i][x][1], dp[i][!x][0]); dp[i][!x][2] = max(dp[i][!x][2], dp[i][x][1]); dp[i][x][2] = max(dp[i][x][2], dp[i][!x][1]); } cout << max(dp[n][0][2], dp[n][1][2]); }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author neuivn */ 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.nextLine(); char[] ch = s.toCharArray(); boolean one = true; int ret = 0; for (int i = 0; i < s.length(); ++i) { if (one) { if (s.charAt(i) == '1') { ++ret; one = !one; } } else { if (s.charAt(i) == '0') { ++ret; one = !one; } } } int ret2 = 0; one = false; for (int i = 0; i < s.length(); ++i) { if (one) { if (s.charAt(i) == '1') { ++ret2; one = !one; } } else { if (s.charAt(i) == '0') { ++ret2; one = !one; } } } int best = Math.max(ret, ret2); best = Math.min(best + 2, s.length()); out.println(best); } } 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 String nextLine() { try { return reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return null; } } }
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<pair<int, int> > v; int nu = s[0] - '0'; int c = 0; for (int i = 0; i < n; i++) { if (nu == (s[i] - '0')) c++; else { v.push_back({nu, c}); c = 1; nu = s[i] - '0'; } } v.push_back({nu, c}); int ans = v.size(); int fl = 1; int cnt = 0; for (int i = 0; i < v.size(); i++) { if (v[i].second > 2) { fl = 0; break; } if (v[i].second > 1) cnt++; } if (cnt >= 2 || fl == 0) cout << ans + 2; else { if (cnt == 1) cout << ans + 1; else 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
#include <bits/stdc++.h> int flip(std::string const& seq) { int count = 1, check = 0; for (int i = 1; i < seq.size(); i++) { if (seq[i] != seq[check]) { count++; check = i; } } if (count == seq.size()) { return count; } else if (count <= seq.size() - 2) { return count += 2; } else if (count == seq.size() - 1) { return count += 1; } } int main(void) { std::ios_base::sync_with_stdio(false); int n = 0; std::cin >> n; std::string seq; seq.reserve(n); std::cin >> seq; std::cout << flip(seq) << std::endl; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; char a[n + 10]; scanf("%s", a); if (n == 2) { cout << 2; return 0; } if (n == 3) { cout << 3; return 0; } if (n == 5 && a[0] == '0' && a[1] == '0' && a[2] == '1' && a[3] == '0' && a[4] == '0') { cout << 5; return 0; } int mx_len = 0; int ans = 1; int i = 1; char prev = a[0]; int cur_len = 0; int flag = 0; while (i < n) { if (prev == '1') { if (a[i] == prev) { cur_len++; if (!flag) { if (cur_len == 1) { if (i + 2 < n) { if (a[i + 1] == '0' && a[i + 2] == '0') { flag = 1; } } } } } else { if (i + 1 < n) { if (i + 1 != n - 1 && a[i + 1] == a[i]) mx_len = 10; } ans++; if (cur_len > mx_len) mx_len = cur_len; cur_len = 0; } prev = a[i]; } else if (prev == '0') { if (a[i] == prev) { cur_len++; if (!flag) { if (cur_len == 1) { if (i + 2 < n) { if (a[i + 1] == '1' && a[i + 2] == '1') { flag = 1; } } } } } else { if (i + 1 < n) { if (a[i + 1] == a[i]) mx_len = 10; } ans++; if (cur_len > mx_len) mx_len = cur_len; cur_len = 0; } prev = a[i]; } i++; } if (cur_len > mx_len) mx_len = cur_len; int bal = ans; if (mx_len > 1 && ans <= n - 2) { ans += 2; cout << ans; return 0; } else if (flag) { ans += 2; cout << ans; return 0; } if (bal == ans) { if (n > 2 && mx_len > 0) { if (a[n - 1] != a[n - 2]) { if (a[n - 3] == a[n - 2]) { ans++; cout << ans; return 0; } } else { if (a[n - 3] != a[n - 2]) { ans++; cout << ans; return 0; } } } } if (mx_len > 0 && ans == n - 1) ans++; cout << ans; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author AlexFetisov */ 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_Alternatve solver = new TaskA_Alternatve(); solver.solve(1, in, out); out.close(); } static class TaskA_Alternatve { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[][] maxLeft = new int[2][n]; int[][] maxRight = new int[2][n]; int[] maxSeqLeft = new int[n]; char[] c = in.nextString().toCharArray(); maxLeft[c[0] - '0'][0] = 1; maxSeqLeft[0] = 1; for (int i = 1; i < n; ++i) { maxSeqLeft[i] = 1; if (c[i] != c[i - 1]) { maxSeqLeft[i] += maxSeqLeft[i - 1]; } for (int j = 0; j < 2; ++j) { maxLeft[j][i] = maxLeft[j][i - 1]; if (c[i] - '0' == j) { maxLeft[j][i] = Math.max(1 + maxLeft[1 - j][i - 1], maxLeft[j][i - 1]); } } } maxRight[c[n - 1] - '0'][n - 1] = 1; for (int i = n - 2; i >= 0; --i) { for (int j = 0; j < 2; ++j) { if (c[i] - '0' == j) { maxRight[j][i] += Math.max((c[i] - '0' == j ? 1 : 0) + maxRight[1 - j][i + 1], maxRight[j][i + 1]); } else { maxRight[j][i] = maxRight[j][i + 1]; } } } int res = 0; for (int i = 0; i < n; ++i) { int cur = maxSeqLeft[i]; int pi = i - maxSeqLeft[i] + 1; int last = c[pi] - '0'; int first = c[i] - '0'; if (pi - 1 >= 0) { cur += maxLeft[last][pi - 1]; } if (i + 1 < n) { cur += maxRight[first][i + 1]; } res = Math.max(res, cur); cur = maxSeqLeft[i]; if (pi < i) { ++pi; --cur; last = c[pi] - '0'; if (pi - 1 >= 0) { cur += maxLeft[last][pi - 1]; } if (i + 1 < n) { cur += maxRight[first][i + 1]; } res = Math.max(res, cur); } } out.println(res); } } static class InputReader { private BufferedReader reader; private StringTokenizer stt; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { return null; } } public String nextString() { while (stt == null || !stt.hasMoreTokens()) { stt = new StringTokenizer(nextLine()); } return stt.nextToken(); } public int nextInt() { return Integer.parseInt(nextString()); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n = int(raw_input()) s = str(raw_input()) point1 = None point2 = None score = 1 c = s[0] for i in xrange(1, n): if s[i] != c: score += 1 c = s[i] for i in xrange(1, n): if s[i-1] == s[i]: if point1 is None: point1 = i else: point2 = i break if point1 and point2: score += 2 elif point1: score += 1 print score
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; string s; int main() { int n; cin >> n; int i; cin >> s; int net = 1; for (i = 0; i < n - 1; i++) { if (s[i] != s[i + 1]) { net++; } } net = min(net + 2, n); cout << net << 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
def getinput(): return map(int, raw_input().split()) def func(n, binstr): t = binstr[0] ans = 1 maxp = 1 p = 1 flag = [0, 0] for i in xrange(1, n): if t == binstr[i]: flag[int(t)] = 1 p += 1 maxp = max(maxp, p) else: ans += 1 t = binstr[i] p = 1 return min(ans + 2, n) if sum(flag) == 2 or maxp > 2: ans += 2 else: ans += sum(flag) return ans n, = getinput() binstr = list(raw_input()) print func(n, binstr)
PYTHON
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; import java.util.Arrays; import java.io.IOException; import java.io.UncheckedIOException; import java.io.Closeable; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); AAlternativeThinking solver = new AAlternativeThinking(); solver.solve(1, in, out); out.close(); } } static class AAlternativeThinking { Debug debug = new Debug(true); public void solve(int testNumber, FastInput in, FastOutput out) { int n = in.ri(); char[] s = new char[n]; in.rs(s, 0); int[][] prev = new int[3][2]; int inf = (int) 1e8; SequenceUtils.deepFill(prev, -inf); int[][] next = new int[3][2]; prev[0][0] = 0; prev[0][1] = 0; for (int i = 0; i < n; i++) { for (int k = 0; k < 2; k++) { int max = 0; for (int j = 0; j < 3; j++) { max = Math.max(prev[j][k], max); prev[j][k] = max; } } debug.debug("i", i); debug.debug("prev", prev); SequenceUtils.deepFill(next, -inf); for (int j = 0; j < 3; j++) { for (int k = 0; k < 2; k++) { int x = s[i] - '0'; x ^= j & 1; //do nothing next[j][k] = Math.max(next[j][k], prev[j][k]); //added if (k != x) { next[j][x] = Math.max(next[j][x], prev[j][k] + 1); } } } int[][] tmp = prev; prev = next; next = tmp; } debug.debug("i", n); debug.debug("prev", prev); int max = 0; for (int j = 0; j < 3; j++) { for (int k = 0; k < 2; k++) { max = Math.max(max, prev[j][k]); } } out.println(max); } } static class FastOutput implements AutoCloseable, Closeable, Appendable { private static final int THRESHOLD = 1 << 13; private final Writer os; private StringBuilder cache = new StringBuilder(THRESHOLD * 2); public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } private void afterWrite() { if (cache.length() < THRESHOLD) { return; } flush(); } public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this(new OutputStreamWriter(os)); } public FastOutput append(char c) { cache.append(c); afterWrite(); return this; } public FastOutput append(int c) { cache.append(c); afterWrite(); return this; } public FastOutput append(String c) { cache.append(c); afterWrite(); return this; } public FastOutput println(int c) { return append(c).println(); } public FastOutput println() { return append(System.lineSeparator()); } public FastOutput flush() { try { os.append(cache); os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } static class FastInput { private final InputStream is; private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int ri() { return readInt(); } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public int rs(char[] data, int offset) { return readString(data, offset); } public int readString(char[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (char) next; next = read(); } return offset - originalOffset; } } static class Debug { private boolean offline; private PrintStream out = System.err; static int[] empty = new int[0]; public Debug(boolean enable) { offline = enable && System.getSecurityManager() == null; } public Debug debug(String name, int x) { if (offline) { debug(name, "" + x); } return this; } public Debug debug(String name, String x) { if (offline) { out.printf("%s=%s", name, x); out.println(); } return this; } public Debug debug(String name, Object x) { return debug(name, x, empty); } public Debug debug(String name, Object x, int... indexes) { if (offline) { if (x == null || !x.getClass().isArray()) { out.append(name); for (int i : indexes) { out.printf("[%d]", i); } out.append("=").append("" + x); out.println(); } else { indexes = Arrays.copyOf(indexes, indexes.length + 1); if (x instanceof byte[]) { byte[] arr = (byte[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug(name, arr[i], indexes); } } else if (x instanceof short[]) { short[] arr = (short[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug(name, arr[i], indexes); } } else if (x instanceof boolean[]) { boolean[] arr = (boolean[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug(name, arr[i], indexes); } } else if (x instanceof char[]) { char[] arr = (char[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug(name, arr[i], indexes); } } else if (x instanceof int[]) { int[] arr = (int[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug(name, arr[i], indexes); } } else if (x instanceof float[]) { float[] arr = (float[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug(name, arr[i], indexes); } } else if (x instanceof double[]) { double[] arr = (double[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug(name, arr[i], indexes); } } else if (x instanceof long[]) { long[] arr = (long[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug(name, arr[i], indexes); } } else { Object[] arr = (Object[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug(name, arr[i], indexes); } } } } return this; } } static class SequenceUtils { public static void deepFill(Object array, int val) { if (!array.getClass().isArray()) { throw new IllegalArgumentException(); } if (array instanceof int[]) { int[] intArray = (int[]) array; Arrays.fill(intArray, val); } else { Object[] objArray = (Object[]) array; for (Object obj : objArray) { deepFill(obj, val); } } } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
from sys import 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]] = max(dp[i-1][2][1^a[i]]+1,dp[i-1][1][1^a[i]]) dp[i][2][1 ^ a[i]] = dp[i-1][1][a[i]]+1 ans=0 # print(*dp,sep='\n') for i in range(3): for j in range(2): ans=max(ans,dp[n-1][i][j]) print(ans)
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n = int(input()) s = input() print(min(n, 3 + s.count('01') + s.count('10')))
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
__author__ = 'Utena' n=input() s=input() t=[] def alt(a): b='' b+=a[0] for i in a[1:len(a)]: if i!=b[-1]: b+=i return b m=len(alt(s)) judge=0 for i in range(len(s)-1): if s[i+1]==s[i]: judge=1 if i<len(s)-2: for j in range(i+1,len(s)-1): if s[j+1]==s[j]: print(m+2) exit(0) if judge==1:print(m+1) else:print(m)
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; int cnt = 1; char curr = s[0]; for (int i = 1; i < n; i++) if (s[i] != curr) curr = s[i], cnt++; cout << min(n, cnt + 2); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.*; import java.util.*; import java.math.*; public class Main implements Runnable { public InputReader in; public PrintWriter out; public void solve() throws Exception { // solution goes here int N = in.nextInt(); char[] chars = in.nextChars(); N = chars.length; int low = 0; int high = N-1; int current = 1; for (int i = 1; i < N; i++) { if (chars[i] != chars[i-1]) current++; } while(low < high && high > 0 && low < N-1) { if (chars[low] == chars[low + 1] && chars[high] == chars[high - 1]) break; if (chars[low] != chars[low + 1]) low++; if (chars[high] != chars[high-1]) high--; } if (low < N -1 && high > 0 && chars[low] == chars[low + 1] && chars[high] == chars[high - 1]) current++; if (high - low > 1) current++; out.println(current); } public void run() { try { in = new InputReader(System.in); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); Locale.setDefault(Locale.US); solve(); out.close(); } catch (Throwable e) { e.printStackTrace(); System.exit(7); } } static int abs(int x) { return x < 0 ? -x : x; } static int max(int a, int b) { return a > b ? a : b; } static int min(int a, int b) { return a < b ? a : b; } public static void main(String args[]) { new Thread(null, new Main(), "Main", 1 << 28).start(); } static boolean OJ = System.getProperty("ONLINE_JUDGE") != null; public void console(Object... objects) { if (!OJ) { out.println(Arrays.deepToString(objects)); out.flush(); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[2048]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader() { this.stream = System.in; } public InputReader(InputStream stream) { this.stream = stream; } public InputReader(SpaceCharFilter filter) { this.stream = System.in; this.filter = filter; } public InputReader(InputStream stream, SpaceCharFilter filter) { this.stream = stream; this.filter = filter; } public int read() { if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public char nextChar() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public int nextDigit() { int c = read(); while (isSpaceChar(c)) c = read(); return c - '0'; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); boolean negative = false; if (c == '-') { negative = true; 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 negative ? -res : res; } public int[] nextInts(int N) { int[] nums = new int[N]; for (int i = 0; i < N; i++) nums[i] = nextInt(); return nums; } public long[] nextLongs(int N) { long[] nums = new long[N]; for (int i = 0; i < N; i++) nums[i] = nextLong(); return nums; } public int nextUnsignedInt() { int c = read(); while (isSpaceChar(c)) c = read(); int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res; } public final long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } boolean negative = false; if (c == '-') { negative = true; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return negative ? -res : res; } public final long nextUnsignedLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); long 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 (!(c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1 || c == '.')); if (c != '.') { return res * sgn; } c = read(); long aft = 0; int len = 1; do { if (c < '0' || c > '9') throw new InputMismatchException(); aft *= 10; len *= 10; aft += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn + aft / (1.0 * len); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == '\n' || c == '\r' || c == '\t' || c == -1; } public 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(); } public char[] nextChars() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); char[] chars = new char[res.length()]; res.getChars(0, chars.length, chars, 0); return chars; } public int[][] nextIntMatrix(int rows, int cols) { int[][] matrix = new int[rows][cols]; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) matrix[i][j] = nextInt(); return matrix; } public long[][] nextLongMatrix(int rows, int cols) { long[][] matrix = new long[rows][cols]; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) matrix[i][j] = nextLong(); return matrix; } public char[][] nextCharMap(int rows, int cols) { char[][] matrix = new char[rows][cols]; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) matrix[i][j] = nextChar(); return matrix; } 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; import java.io.PrintStream; import java.util.Arrays; import java.util.StringTokenizer; import static java.lang.Math.max; /** * 603A * * @author artyom */ public class AlternativeThinking implements Runnable { private BufferedReader in; private PrintStream out; private StringTokenizer tok; private void solve() throws IOException { int n = nextInt(); char[] str = nextToken().toCharArray(); int[][] dp = new int[n][4]; Arrays.fill(dp[n - 1], 1); for (int k = n - 2; k >= 0; k--) { if (str[k] != str[k + 1]) { dp[k][0] = max(dp[k + 1][0] + 1, dp[k + 1][2]); dp[k][1] = dp[k + 1][1] + 1; dp[k][2] = dp[k][3] = max(dp[k + 1][1], dp[k + 1][3] + 1); } else { dp[k][0] = max(dp[k + 1][0], dp[k + 1][2] + 1); dp[k][1] = dp[k + 1][1]; dp[k][2] = dp[k][3] = max(dp[k + 1][1] + 1, dp[k + 1][3]); } } out.print(max(dp[0][0], dp[0][2])); } //-------------------------------------------------------------- public static void main(String[] args) { new AlternativeThinking().run(); } @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = System.out; tok = null; solve(); in.close(); } catch (IOException e) { System.exit(0); } } private String nextToken() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private int[] readIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int a[N]; string s; int n; int main() { cin >> n >> s; for (int i = 1; i <= n; ++i) a[i] = s[i - 1] - '0'; int ans = 1; int pre = a[1]; for (int i = 2; i <= n; ++i) { if (a[i] != pre) { pre = a[i]; ans++; } } printf("%d\n", min(n, ans + 2)); }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.util.*; import java.io.*; /** * Made by egor https://github.com/chermehdi/egor. * * @author Azuz * */ public class Main { int[][][] dp; char[] arr; int n; void solve(Scanner in, PrintWriter out) { n = in.nextInt(); arr = in.next().toCharArray(); dp = new int[n][3][2]; for (int i = 0; i < n; ++i) { for (int j = 0; j < 3; ++j) { dp[i][j][0] = dp[i][j][1] = Integer.MIN_VALUE; } } int ans = 0; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 2; ++j) { ans = Math.max(ans, solve(0, i, j)); } } out.println(ans); } public int solve(int i, int state, int last) { if (i == n) { if (state >= 2) { return 0; } else { return -n; } } if (dp[i][state][last] > Integer.MIN_VALUE) { return dp[i][state][last]; } int ret = Integer.MIN_VALUE; int ord = o(arr[i]); if (state == 0) { ret = Math.max(ret, solve(i + 1, state, ord) + (ord != last ? 1 : 0)); ret = Math.max(ret, solve(i + 1, state + 1, (ord ^ 1)) + (ord == last ? 1 : 0)); } else if (state == 1) { ret = Math.max(ret, solve(i + 1, state, ord ^ 1) + (ord == last ? 1 : 0)); ret = Math.max(ret, solve(i + 1, state + 1, ord) + (ord != last ? 1 : 0)); } else { ret = Math.max(ret, solve(i + 1, state, ord) + (ord != last ? 1 : 0)); } return dp[i][state][last] = ret; } public int o(char c) { return c - '0'; } public static void main(String[] args) { try(Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out)) { new Main().solve(in, out); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n=int(input()) s=input() k=0 i=0 r=int(s[0]) while(i<n): if(int(s[i])==r): r=1-r k+=1 i+=1 print(min(k+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
// created by 我不知道我是谁 import java.io.*; import java.util.*; public class scratch_18{ static class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } } // out.append("Case #"+(tt+1)+": "); static class Pair implements Comparable<Pair> { long val; int pos; public Pair(long val, int pos) { this.pos = pos; this.val = val; } @Override public String toString() { return val + " " + pos; } @Override public int compareTo(Pair o) { if(this.val >o.val){ return 1; } else if(this.val==o.val){ return 0; } else{ return -1; } }} public static void main(String[] args) throws IOException { Reader.init(System.in); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int n= Reader.nextInt(); String s= Reader.next(); int arr[]= new int[n]; for (int i = 0; i <n ; i++) { if(s.charAt(i)=='0'){ arr[i]=0; } else{ arr[i]=1; } } int dp[][][]= new int[n+1][3][2]; // second is for weather before, during or after the changed substring, 3rd is for 0 ending or 1 ending. for (int i = 0; i <n ; i++) { if(arr[i]==0){ dp[i+1][0][0]= Math.max(dp[i+1][0][0],Math.max(dp[i][0][1]+1,dp[i][0][0])); dp[i+1][0][1]= Math.max(dp[i+1][0][1],dp[i][0][1]); dp[i+1][1][1]= Math.max(dp[i+1][1][1],Math.max(dp[i][1][0]+1,Math.max(dp[i][0][0]+1,Math.max(dp[i][0][1],dp[i][1][1])))); // this is in maximum wala. Now, previous may or may not be in maximum. this ends at 0. dp[i+1][1][0]= Math.max(dp[i+1][1][0],Math.max(dp[i][1][0],dp[i][0][0])); dp[i+1][2][1]= Math.max(dp[i+1][2][1],Math.max(dp[i][2][1],dp[i][1][1])); dp[i+1][2][0]= Math.max(dp[i+1][2][0],Math.max(dp[i][2][1]+1,Math.max(dp[i][1][1]+1,Math.max(dp[i][2][0],dp[i][1][0])))); } if(arr[i]==1){ dp[i+1][0][1]= Math.max(dp[i+1][0][1],Math.max(dp[i][0][0]+1,dp[i][0][1])); dp[i+1][0][0]= Math.max(dp[i+1][0][0],dp[i][0][0]); dp[i+1][1][0]= Math.max(dp[i+1][1][0],Math.max(dp[i][1][1]+1,Math.max(dp[i][0][1]+1,Math.max(dp[i][1][0],dp[i][0][0])))); dp[i+1][1][1]= Math.max(dp[i+1][1][1],Math.max(dp[i][1][1],dp[i][0][1])); dp[i+1][2][0]= Math.max(dp[i+1][2][0],Math.max(dp[i][2][0],dp[i][1][0])); dp[i+1][2][1]=Math.max(dp[i+1][2][1],Math.max(dp[i][2][0]+1,Math.max(dp[i][1][0]+1,Math.max(dp[i][2][1],dp[i][1][1])))); } } int max=-1; for (int i = 0; i <3 ; i++) { for (int j = 0; j <2 ; j++) { max= Math.max(max,dp[n][i][j]); } } out.append(max+"\n"); out.flush(); out.close(); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { public static InputReader in; public static PrintWriter pw; public static void main(String args[]) { new Thread(null, new Runnable() { public void run() { try{ solve(); } catch(Exception e){ e.printStackTrace(); } } }, "1", 1 << 26).start(); } static ArrayList<Integer> g[]; static ArrayList<Integer> h[]; static boolean visited[]; static boolean visit[]; static int par[]; static int degree[]; static int edjes=0; static int start=-1; static int end=-1; static int Total=0; static int time1[]; static int time2[]; static int glob=0; static long cnt=0; static boolean vis2[][]; static int lev=1; static int depth[]; static long[] sm; static int val[]; static int len=0; static long sum1=0; static int pre[][]; static HashMap<String,Integer> hm; //static TreeSet<Long> set; static boolean Not[]; //static int pre[][]; static int ana[]; static HashSet<Integer> set[]; public static void solve(){ in = new InputReader(System.in); pw = new PrintWriter(System.out); int n=in.nextInt(); String s=in.readString(); char c=s.charAt(0); int ans=1; for(int i=1;i<s.length();i++) { if(s.charAt(i)!=c) { ans++; c=s.charAt(i); } } System.out.println(Math.min(ans+2, n)); } private static boolean check(int mid, int[] a, int[] pr,int n) { HashMap<Integer,Integer> hm=new HashMap(); for(int i=1;i<=(mid+1);i++) { for(int x:set[i]){ if(!hm.containsKey(x)) hm.put(x,1); else hm.put(x,hm.get(x)+1); if(hm.get(x)==(mid+1)) { //System.out.println(i+" "+mid); //ana[i]=1;i return true; } } } int now=1; int then=mid+2; while(then<=n) { for(int x:set[now]) { if(hm.containsKey(x)) hm.put(x,hm.get(x)-1); } for(int x:set[then]) { if(!hm.containsKey(x)) hm.put(x,1); else hm.put(x,hm.get(x)+1); if(hm.get(x)==(mid+1)) { ana[now+1]=1; return true; } } now++; then++; } return false; } /* public static int bfs(int x,int y,int tx,int ty,int n,int m) { boolean vis[][]=new boolean[101][101]; vis[x][y]=true; int lev=0; Queue<String> Q=new LinkedList(); String sx=x+" "+y; Q.add(sx); while(!Q.isEmpty()) { int count=Q.size(); while(count>0) { String p=Q.poll(); int i=Integer.parseInt(p.split(" ")[0]); int j=Integer.parseInt(p.split(" ")[1]); vis[i][j]=true; if(i==tx&&j==ty) { return lev; } if(i==tx&&(j-1)==ty) { return lev; } if(i==tx&&(j+1)==ty) { return lev; } if((i+1)==tx&&(j)==ty) { return lev; } if((i-1)==tx&&(j)==ty) { return lev; } if(i<n&&j-1>=0&&!vis[i][j-1]&&a[i][j-1]=='O') { vis[i][j-1]=true; String s=(i)+" "+(j-1); Q.add(s); } if(i<n&&j+1<m&&!vis[i][j+1]&&a[i][j+1]=='O') { vis[i][j+1]=true; String s=(i)+" "+(j+1); Q.add(s); } if(i+1<n&&j<m&&!vis[i+1][j]&&a[i+1][j]=='O') { vis[i+1][j]=true; String s=(i+1)+" "+j; Q.add(s); } if(i-1>=0&&j<m&&!vis[i-1][j]&&a[i-1][j]=='O') { vis[i-1][j]=true; String s=(i-1)+" "+j; Q.add(s); } count--; } lev++; // System.out.println(Q); } return -1; }*/ public static void dfs(int curr,int parent) { visited[curr]=true; par[curr]=parent; for(int x:g[curr]) { if(!visited[x]) { depth[x]=depth[curr]+1; dfs(x,curr); } } } /* public static void dfs2(int curr,int parent, HashSet<Integer> fk) { visited[curr]=true; fk.add(curr); //System.out.print(curr+" "); cnt++; for(int x:g[curr]) { if(x!=parent) { dfs2(x,par[x],fk); } } }*/ /*public static void dfs1(int curr) { visited[curr]=true; long find=sm[curr]-(long)val[curr]; if(set.lower(find)!=null) { cnt+=(size[curr]); return; } set.add(sm[curr]); for(int x:g[curr]) { if(!visited[x]) dfs1(x); } set.remove(sm[curr]); } /* public static void dfs(int curr,int parent) { visited[curr]=true; par[curr]=parent; String s=curr+" "+parent; sum1+=(hm.get(s)); size[curr]=1; for(int x:g[curr]) { if(!visited[x]) { dfs(x,curr); size[curr]+=size[x]; } } sm[curr]=sum1; String p=curr+" "+parent; sum1-=(hm.get(p)); } public static int len(String s) { if(s.charAt(len++)=='l') { return 1; } else{ return Math.max(len(s), len(s))+1; } } */ public static int LCA(int p,int q) { if(depth[p]<depth[q]) { int tmp=p; p=q; q=tmp; } int dist=depth[p]-depth[q]; while(dist>0) { int raise=(int)(Math.log(dist)/Math.log(2)); p=pre[p][raise]; dist-=(1<<raise); } if(p==q) { return p; } else{ int current=(int)(Math.log(depth[p])/Math.log(2)); for(int j=current;j>=0;j--) { if(pre[p][j]!=0&&pre[p][j]!=pre[q][j]) { p=pre[p][j]; q=pre[q][j]; } } } return par[p]; } public static void dfs(int curr,int lev,int parent) { visited[curr]=true; depth[curr]=lev; par[curr]=parent; // set[lev].add(curr); for(int x:g[curr]) { if(!visited[x]) { dfs(x,lev+1,curr); } } } /*public static void dfs2(int curr,int lev) { time1[curr]=glob++; depth[curr]=lev; visited[curr]=true; nodes[lev].add(time1[curr]); values[lev].add(a[curr]); for(int x:g[curr]) { if(!visited[x]) { dfs2(x,lev+1); } } time2[curr]=glob++; } public static void bfs1(int curr) { Queue<Integer> Q=new LinkedList<Integer>(); Q.add(1); visit[1]=true; int lev=2; levels[1].add(1); while(!Q.isEmpty()) { int cnt=Q.size(); while(cnt>0) { int p=Q.poll(); visit[p]=true; for(int x:g[p]) { if(!visit[x]) { Q.add(x); levels[lev].add(x); } } cnt--; } max=Math.max(lev, max); lev++; } } private static long fact(int len) { long ans=1; for(int i=1;i<=len;i++) ans*=i; return ans; } */ // SubTree Checker DFS (TO CHECK IF X IS IN A SUBTREE OF Y) public static void dfs3(int curr) { visited[curr]=true; time1[curr]=glob++; for(int x:g[curr]) { if(!visited[x]) dfs3(x); } time2[curr]=glob++; } /*NO OF EDJES IN WHOLE CONNECTED COMPONENTS public static void dfs1(int curr) { visited[curr]=true; for(int next:g[curr]) { edje++; if(!visited[next]) dfs1(next); } } /*SUM OF ALL SUBTREE NODE'S VLAUES DFS public static void dfs(int curr,int prev) { val[curr]=1; for(int x:g[curr]) { if(x!=prev) { dfs(x,curr); val[curr]+=val[x]; } } }*/ public static long power(long a,long b) { long result=1; while(b>0) { if(b%2==1) result*=a; a=a*a; b/=2; } return result; } public static long pow(long n,long p,long m) { long result = 1; if(p==0) return 1; if (p==1) return n; while(p!=0) { if(p%2==1) result *= n; if(result>=m) result%=m; p >>=1; n*=n; if(n>=m) n%=m; } return result; } static class Pair implements Comparable<Pair>{ int now; int val; Pair(int ver,int weight){ this.now=ver; this.val=weight; //this.index=index; } @Override public int compareTo(Pair o) { return -(o.val-this.val); } } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a%b); } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n=int(raw_input()) string=str(raw_input()) ans=1 prev=string[0] for j in range(1,n): if(string[j]!=prev): ans+=1 prev=string[j] temp=0 for j in range(1,n): if(string[j]==string[j-1]): temp+=1 for k in range(j+1,n): if(string[k]==string[k-1]): temp+=1 break break print ans+temp
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.*; public class alternativethinking { private static InputReader in; private static PrintWriter out; public static void main(String[] args) throws IOException { in = new InputReader(System.in); out = new PrintWriter(System.out, true); int n = in.nextInt(); char[] c = in.next().toCharArray(); int[][] dp = new int[3][2]; for (int i = 0; i < n; i++) { int k = c[i] - '0'; int[][] next = new int[3][2]; next[0][k] = Math.max(dp[0][k], dp[0][1-k]+1); next[1][k] = Math.max(dp[1][k], Math.max(dp[1][1-k]+1,dp[0][k]+1)); next[2][k] = Math.max(dp[2][k], Math.max(dp[2][1-k]+1,dp[1][k]+1)); dp = next; } int max = 0; for (int i = 0; i < 3; i++) max = Math.max(max, Math.max(dp[i][0],dp[i][1])); out.println(max); out.close(); System.exit(0); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const int maxn = 1e6; const int INF = 0x7fffffff; const int mod = 1e9 + 7; const double eps = 1e-7; const double Pi = acos(-1.0); inline int read_int() { char c; int ret = 0, sgn = 1; do { c = getchar(); } while ((c < '0' || c > '9') && c != '-'); if (c == '-') sgn = -1; else ret = c - '0'; while ((c = getchar()) >= '0' && c <= '9') ret = ret * 10 + (c - '0'); return sgn * ret; } inline long long read_ll() { char c; long long ret = 0, sgn = 1; do { c = getchar(); } while ((c < '0' || c > '9') && c != '-'); if (c == '-') sgn = -1; else ret = c - '0'; while ((c = getchar()) >= '0' && c <= '9') ret = ret * 10 + (c - '0'); return sgn * ret; } int n, res = 1; string s; int main() { cin >> n >> s; for (int i = 1; i < n; i++) { res += (s[i] != s[i - 1]); } cout << ((res + 2) < (n) ? (res + 2) : (n)) << endl; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.*; import java.util.ArrayList; import java.util.InputMismatchException; /** * Created by Shreyans Sheth [bholagabbar] on 12/8/2015 at 3:01 PM using IntelliJ IDEA (Fast IO Template) */ public class AlternativeThinking_604_C { public static void main(String[] args) throws Exception { //System.setIn(new FileInputStream("E:/Shreyans/Documents/Code/CODE/src/Stdin_File_Read.txt")); InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); int n=in.readInt(); char[] a=in.readString().toCharArray(); ArrayList<Integer>comp=new ArrayList<Integer>(); int cnt=1; for(int i=1;i<a.length;i++) { if(a[i]==a[i-1]) cnt++; else { comp.add(cnt); cnt=1; } } comp.add(cnt); int max=0; cnt=0; for(int i:comp) { max=Math.max(max,i); if(i==2) cnt++; } if(max>=3 || cnt>=2) //To prove the 1st, just flip the mid bit in the flip subs. For the second, start and the second bit of the 1st 00 and end at the 2nd last of 00. (Could be 1s also) out.printLine(comp.size()+2); else if(cnt==1) out.printLine(comp.size()+1); else out.printLine(comp.size()); } //FAST IO private static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public double readDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } private static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } writer.flush(); } public void printLine(Object... objects) { print(objects); writer.println(); writer.flush(); } public void close() { writer.close(); } public void flush() { writer.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 n, res; string s; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> s; res = 1; for (int i = 1; i < n; ++i) { if (s[i] != s[i - 1]) ++res; } cout << min(n, res + 2); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; template <typename T> T in() { char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break; } if (ch >= '0' && ch <= '9') break; } while (1) { n = n * 10 + (ch - '0'); ch = getchar(); if (ch < '0' || ch > '9') break; } return (ng ? -n : n); } template <typename T> inline T POW(T B, T P) { if (P == 0) return 1; if (P & 1) return B * POW(B, P - 1); else return (POW(B, P / 2) * POW(B, P / 2)); } template <typename T> inline T Gcd(T a, T b) { if (a < 0) return Gcd(-a, b); if (b < 0) return Gcd(a, -b); return (b == 0) ? a : Gcd(b, a % b); } template <typename T> inline T Lcm(T a, T b) { if (a < 0) return Lcm(-a, b); if (b < 0) return Lcm(a, -b); return a * (b / Gcd(a, b)); } long long Bigmod(long long base, long long power, long long MOD) { long long ret = 1; while (power) { if (power & 1) ret = (ret * base) % MOD; base = (base * base) % MOD; power >>= 1; } return ret; } bool isVowel(char ch) { ch = toupper(ch); if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E') return true; return false; } long long ModInverse(long long number, long long MOD) { return Bigmod(number, MOD - 2, MOD); } bool isConst(char ch) { if (isalpha(ch) && !isVowel(ch)) return true; return false; } int toInt(string s) { int sm; stringstream ss(s); ss >> sm; return sm; } int dp[100007][3][4], a[100007]; int n; string s; int Solve(int pos, int pv, int f) { if (pos == n) return 0; int &res = dp[pos][pv][f]; if (res != -1) return res; res = Solve(pos + 1, pv, f); if (f == 0) { if (a[pos] != pv) res = max(res, 1 + Solve(pos + 1, a[pos], f)); int an = 0; if (pv == a[pos]) an = 1; res = max(res, an + Solve(pos + 1, (a[pos] ^ 1), 1)); } else if (f == 1) { if (a[pos] == pv) res = max(res, 1 + Solve(pos + 1, (a[pos] ^ 1), f)); int an = 0; if (pv != a[pos]) an = 1; res = max(res, an + Solve(pos + 1, (a[pos]), 2)); } else { int an = 0; if (pv != a[pos]) an = 1; res = max(res, an + Solve(pos + 1, (a[pos]), 2)); } return res; } int main() { n = in<int>(); cin >> s; for (int i = 0; i < n; i++) a[i] = (s[i] - '0'); memset(dp, -1, sizeof(dp)); printf("%d\n", Solve(0, 2, 0)); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.util.*; import java.lang.*; import java.io.*; public class Main { void run() throws Exception { //// sample program begins here // int a, b; // a = nextInt(); // b = nextInt(); // out.println(a + b); int n = nextInt(); String str = next(); int len = 1; for(int i = 1; i < str.length(); i++) { if(Integer.valueOf(str.charAt(i)) != Integer.valueOf(str.charAt(i-1))) len++; } out.println(Math.min(str.length(), len+2)); in.close(); out.close(); } public static void main(String args[]) throws Exception { new Main().run(); // read from stdin/stdout // new Main("prog.in", "prog.out").run(); // or use file i/o } BufferedReader in; PrintStream out; StringTokenizer st; Main() throws Exception { in = new BufferedReader(new InputStreamReader(System.in)); out = System.out; } Main(String is, String os) throws Exception { in = new BufferedReader(new FileReader(new File(is))); out = new PrintStream(new File(os)); } int nextInt() throws Exception { return Integer.parseInt(next()); } long nextLong() throws Exception { return Long.parseLong(next()); } String next() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.*; import java.util.*; public class AlternateThink{ public static void main(String[]args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st = new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken()); //char[] s=new char[n]; char[] s=br.readLine().toCharArray(); int[][] dp=new int[n][3]; Arrays.fill(dp[0],1); for(int i=1;i<n;i++){ //System.out.println("i= "+i+"s[i]= "+s[i]); dp[i][0]=dp[i-1][0] + (s[i]!=s[i-1]?1:0); dp[i][1]=Math.max(dp[i-1][0]+(s[i]==s[i-1]?1:0),dp[i-1][1]+(s[i]!=s[i-1]?1:0)); dp[i][2]=Math.max(dp[i-1][2]+(s[i]!=s[i-1]?1:0),dp[i-1][1]+(s[i]==s[i-1]?1:0)); //System.out.println(dp[i][2]); } bw.write(Math.max(dp[n-1][2],Math.max(dp[n-1][1],dp[n-1][0]))+""); bw.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
from sys import stdin length = int(stdin.readline().rstrip()) scores = stdin.readline().rstrip() doubles_found = 0 score = 1 for i in range(length-1): if scores[i] != scores[i+1]: score += 1 else: if doubles_found < 2: score += 1 doubles_found +=1 print(score)
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]; static boolean visited[][][]=new boolean[100000][2][2]; static boolean vis[]=new boolean[100000]; static char arr[]; public static int solve(int idx , int prev,int change) { if(idx==n) return 0 ; if(visited[idx][prev][change]) return memo[idx][prev][change]; visited[idx][prev][change]=true; int max=Integer.MIN_VALUE; if((arr[idx]-'0')!=prev) max=1+solve(idx+1,arr[idx]-'0',change); else { int max1,max2; max1=max2=Integer.MIN_VALUE; max1=solve(idx+1,prev,change); if(vis[idx-1]||change==0) { vis[idx]=true; max2=(arr[idx]-'0'==prev?1:0)+solve(idx+1,(arr[idx]=='0')?1:0,1); vis[idx]=false; } return memo[idx][prev][change]=Math.max(max1,max2); } return memo[idx][prev][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(); int res=1+solve(1,arr[0]-'0',0); 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
n = int(input()) s = input() num = 1 for i in range(1,n): if s[i] != s[i-1]: num += 1 print(min(n,num+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; struct IO { IO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } } _; int n, m, len, k, t, a, b; int main() { string s; cin >> n >> s; char prev = s[0]; int a = 0, b = 1; for (int i = 1; i <= n - 1; i++) { if (s[i] == s[i - 1]) a++; if (s[i] != prev) { prev = s[i]; b++; } } b += min(a, 2); cout << b << "\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.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Ideone { public static void main (String[] args) throws java.lang.Exception { // your code goes here BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int n = Integer.parseInt(br.readLine().trim()); String temp = br.readLine().trim(); int ans = 1; char prev = temp.charAt(0); for(int i = 1; i< temp.length(); i++){ if(temp.charAt(i)!=prev){ ans++; prev = temp.charAt(i); } } int flag = 0; for(int i = 0 ; i< n-1; i++){ if(temp.substring(i,i+2).equals("00")||temp.substring(i,i+2).equals("11")){ ans+=1; flag++; if(flag==2) break; } } pw.print(ans); 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() { int n; string s; cin >> n >> s; vector<int> nums; char last = s[0]; int numk = 0; for (int i = 0; i < n; i++) { if (last == s[i]) numk++; else { nums.push_back(numk); last = s[i]; numk = 1; } } nums.push_back(numk); int viac = 0; for (int i = 0; i < nums.size(); i++) { if (nums[i] > 1) viac++; if (nums[i] > 2) viac = 2; if (viac == 2) break; } int p = (nums.size() + viac); cout << p << "\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 = 1e5 + 5; int n, f[N], len; char s[N]; int main() { cin >> n >> s + 1; int cnt = 1; for (int i = 2; i <= n; i++) { if (s[i] == s[i - 1]) cnt++; else { len++; cnt = 1; } } len++; cout << min(len + 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
import sys n = int(input()) s = input() p = s[0] ans = 1 for i in range(1,n): if s[i] != p: ans += 1 p = s[i] if (0 <= s.find("00") < s.rfind("00")) or (0 <= s.find("11") < s.rfind("11")) or (0 <= s.find("11") < s.rfind("00")) or (0 <= s.find("00") < s.rfind("11")): ans += 2 elif s.find("00") >= 0 or s.find("11") >= 0: ans += 1 print(ans)
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; 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 bin = in.next(); int ans = 1; char cur = bin.charAt(0); for (int i = 1; i < n; i++) { if (cur != bin.charAt(i)) { ans++; if (cur == '1') cur = '0'; else cur = '1'; } } out.println(Math.min(ans + 2, bin.length())); } } static class InputReader { private StringTokenizer tokenizer; private BufferedReader reader; public InputReader(InputStream inputStream) { reader = new BufferedReader(new InputStreamReader(inputStream)); } private void fillTokenizer() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } } public String next() { fillTokenizer(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 2e6 + 5; int n; int main() { std::ios::sync_with_stdio(false); cin.tie(0); cin >> n; string s; cin >> s; int ans = 0; stack<int> st; int id = 0; for (int i = 0; i < n; i++) { if (st.size() == 0 || st.top() != s[i]) { st.push(s[i]); } else { if (id < 2) { id++; st.push(s[i]); } } } ans += st.size(); 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
# cook your dish here n=int(input()) s=list(input()) l=[] c=1 for i in range(1,n): if s[i]==s[i-1]: c=c+1 else: if c>=2: l.append(c) c=1 if c>=2: l.append(c) inc=0 if len(l)>1: inc=2 elif len(l)==1: if l[0]>=3: inc=2 elif l[0]==2: inc=1 a1,a2=0,0 c='1' for i in range(n): if s[i]==c: a1=a1+1 c='1' if c=='0' else '0' c='0' for i in range(n): if s[i]==c: a2=a2+1 c='0' if c=='1' else '1' print(max(a1,a2)+inc)
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int n; char in[100005]; int main() { scanf("%d %s", &n, in); for (int i = 1; i < n; i++) { if (in[i] == in[i - 1]) { for (int j = i;; j++) { in[j] = 1 - (in[j] - '0') + '0'; if (in[j + 1] != in[j]) break; } break; } } char prv = '2'; int cnt = 0; for (int i = 0; i < n; i++) if (in[i] != prv) { prv = in[i]; cnt++; } printf("%d", cnt); 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.io.PrintWriter; import java.util.*; public class C { public static void main(String[] args) throws Exception { new C().run(); out.close(); } void run() throws Exception { int n = nextInt(); String s = nextToken(); int nTwo = 0; int res = 0; for (int i = 0; i < n; ++i) { if (i == 0 || s.charAt(i) != s.charAt(i - 1)) { ++res; } else { ++nTwo; } } if (nTwo >= 2) { res += 2; } else if (nTwo == 1) { ++res; } out.print(res); } //~~~~~~~~~~~~~~~~~~~~~~~~~~~ template ~~~~~~~~~~~~~~~~~~~~~~~~~~~ int[] nextIntArray(int n) throws IOException { int[] res = new int[n]; for (int i = 0; i < n; ++i) { res[i] = nextInt(); } return res; } int[][] nextIntArray(int nx, int ny) throws IOException { int[][] res = new int[nx][ny]; for (int i = 0; i < nx; ++i) { for (int j = 0; j < ny; ++j) { res[i][j] = nextInt(); } } return res; } long[] nextLongArray(int n) throws IOException { long[] res = new long[n]; for (int i = 0; i < n; ++i) { res[i] = nextLong(); } return res; } long[][] nextLongArray(int nx, int ny) throws IOException { long[][] res = new long[nx][ny]; for (int i = 0; i < nx; ++i) { for (int j = 0; j < ny; ++j) { res[i][j] = nextLong(); } } return res; } void fill(int[][] arr, int val) { for (int i = 0; i < arr.length; ++i) { Arrays.fill(arr[i], val); } } void fill(int[][][] arr, int val) { for (int i = 0; i < arr.length; ++i) { fill(arr[i], val); } } int[][] newIntArray(int nx, int ny, int val) { int[][] res = new int[nx][ny]; fill(res, val); return res; } int[][][] newIntArray(int nx, int ny, int nz, int val) { int[][][] res = new int[nx][ny][nz]; fill(res, val); return res; } long[][] newLongArray(int nx, int ny, long val) { long[][] res = new long[nx][ny]; for (int i = 0; i < nx; ++i) { Arrays.fill(res[i], val); } return res; } long[][][] newLongArray(int nx, int ny, int nz, long val) { long[][][] res = new long[nx][ny][nz]; for (int i = 0; i < nx; ++i) { for (int j = 0; j < ny; ++j) { Arrays.fill(res[i][j], val); } } return res; } <T> ArrayList<T>[] newArrayListArray(int sz) { ArrayList<T>[] res = new ArrayList[sz]; for (int i = 0; i < sz; ++i) { res[i] = new ArrayList<>(); } return res; } String nextToken() throws IOException { while (strTok == null || !strTok.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return null; } strTok = new StringTokenizer(line); } return strTok.nextToken(); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } static StringTokenizer strTok; final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); final static PrintWriter out = new PrintWriter(System.out); }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; void solve(); signed main() { int t = 1; while (t--) solve(); return 0; } void solve() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { char t; cin >> t; a[i] = t - '0'; } int l = 1; int prev = a[0]; int mx = 1; for (int i = 1; i < n; i++) { if (a[i] != prev) { l++; prev = a[i]; } } { cout << (min(l + 2, n)) << endl; return; }; }
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, x, y; string s; int main() { cin >> n >> s; for (int i = 1; i < n; ++i) { x += s[i] != s[i - 1]; y += s[i] == s[i - 1]; } cout << x + min(y, 2) + 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
import java.util.Scanner; public class App { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); sc.nextLine(); String s=sc.nextLine(); int answer=0; for(int i=0;i<s.length()-1;i++) { if(s.charAt(i)!=s.charAt(i+1)) answer+=1; } answer++; System.out.println(Math.min(answer+2, s.length())); } }
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() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, ans = 1, i; cin >> n; string s; cin >> s; for (i = 0; i < s.length() - 1; i++) { if (s[i] != s[i + 1]) ans++; } ans = min(n, ans + 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
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(NULL); long long i, j, n, m; cin >> n; string s; cin >> s; long long dp[n + 1][2]; memset(dp, 0, sizeof(dp)); for (i = 1; i <= n; i++) { if (s[i - 1] == '0') { dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + 1); dp[i][1] = dp[i - 1][1]; } else { dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + 1); dp[i][0] = dp[i - 1][0]; } } cout << min(n, max(dp[n][0], dp[n][1]) + 2); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n = int(input()) s = input() x = 0 y = 1 for i in range (n - 1): if s[i] == s[i + 1]: x += 1 if s[i] != s[i + 1]: 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
#include <bits/stdc++.h> using namespace std; int main() { int N = 0; cin >> N; string results; cin >> results; N = results.size(); auto prec = results.at(0); int changes = 0, equals = 0; for (int i = 1; i < N; i++) { auto next = results.at(i); if (next != prec) { changes++; } else { equals++; } prec = next; } cout << 1 + changes + std::min(equals, 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; signed main() { long long n, i, x = 0, c = 1; cin >> n; string s; cin >> s; for (i = 1; i < s.size(); i++) { if (s[i - 1] != s[i]) c++; } cout << min(c + 2, n); }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n=int(input()) s=input() print(min(n,3+s.count("01")+s.count("10")))
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; struct __s { __s() { if (1) { ios_base::Init i; cin.sync_with_stdio(0); cin.tie(0); } } ~__s() { if (!1) fprintf(stderr, "Execution time: %.3lf s.\n", (double)clock() / CLOCKS_PER_SEC); long long n; cin >> n; } } __S; long long a[2]; long long dp[2][111111]; int main(void) { long long n; cin >> n; string s; cin >> s; for (long long i = 0; i < (long long)(n); i++) { a[s[i] - '0']++; } if (a[0] == 0 || a[1] == 0) { cout << min(n, 3LL) << '\n'; return 0; } dp[0][0] = 1; for (long long i = 1; i < n; i++) { dp[0][i] = dp[0][i - 1] + (s[i] != s[i - 1]); } dp[1][n - 1] = 1; for (long long i = n - 2; i >= 0; i--) { dp[1][i] = dp[1][i + 1] + (s[i] != s[i + 1]); } long long l = 1; while (l < n && s[l - 1] != s[l]) l++; if (l == n) { cout << n << '\n'; return 0; } long long ans = 0; for (long long i = l; i < n; i++) { s[i] = (s[i] == '0' ? '1' : '0'); long long res = 1; res += dp[0][i - 1]; res -= (s[i] == s[i - 1]); if (i + 1 < n) { res += dp[1][i + 1]; res -= (s[i] == s[i + 1]); } dp[0][i] = dp[0][i - 1] + (s[i] != s[i - 1]); ans = max(ans, res); } cout << ans << '\n'; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.util.*; import java.io.*; public class CodeForces { public static void main(String[] args) throws IOException { Reader.init(System.in); int n = Reader.nextInt(); String s = Reader.next(); arr = new int[n]; dp = new int[n][3][3]; for (int i = 0; i < s.length(); i++) { arr[i] = s.charAt(i) - '0'; dp[i][0][0] = dp[i][0][1] = dp[i][0][2] = -1; dp[i][1][0] = dp[i][1][1] = dp[i][1][2] = -1; dp[i][2][0] = dp[i][2][1] = dp[i][2][2] = -1; } System.out.println(Math.max(calc(0, 2, 0), calc(0, 2, 1))); } static int arr[]; static int dp[][][]; static int calc(int i, int prev, int flip){ if(i == arr.length) return 0; if(dp[i][prev][flip] != -1) return dp[i][prev][flip]; int b = 0, cur = arr[i]; if(flip == 1) cur = (arr[i] == 0) ? 1 : 0; if(prev != cur) b = 1; int ch; if(flip == 0) ch = Math.max(calc(i+1, cur, 0) + b, calc(i+1, cur, 1) + b); else if(flip == 1) ch = Math.max(calc(i+1, cur, 2) + b, calc(i+1, cur, 1) + b); else ch = calc(i+1, cur, 2) + b; return dp[i][prev][flip] = ch; } } class Edge implements Comparable<Edge> { int to; int from; int cost; Edge(int a, int b, int c) { from = a; to = b; cost = c; } @Override public int compareTo(Edge t) { return t.cost - cost; } } class Pair implements Comparable<Pair> { int pos, time; Pair(int a, int b) { pos = b; time = a; } @Override public int compareTo(Pair t) { return time - t.time; } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; public static int pars(String x) { int num = 0; int i = 0; if (x.charAt(0) == '-') { i = 1; } for (; i < x.length(); i++) { num = num * 10 + (x.charAt(i) - '0'); } if (x.charAt(0) == '-') { return -num; } return num; } static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static void init(FileReader input) { reader = new BufferedReader(input); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return pars(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; char x, y; int ctr = 0; int eq = 0; cin >> x; for (int i = 1; i < n; i++) { cin >> y; if (x != y) ctr++; else eq++; x = y; } if (eq >= 2) ctr += 2; if (eq == 1) ctr++; ctr++; cout << ctr << "\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
""" Code of Ayush Tiwari Codeforces: servermonk Codechef: ayush572000 """ # import sys # input = sys.stdin.buffer.readline def solution(): n=int(input()) s=input() ans=1 for i in range(1,n): if s[i]!=s[i-1]: ans+=1 print(min(n,ans+2)) solution()
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; map<int, int> mp; int main() { int n, ad = 0, ans = 0; string s; cin >> n; cin >> s; for (int i = 0; i < n; i++) { if (i == 0) { ans = 1; continue; } if (s[i] != s[i - 1]) ans++; else ad++; } ad = min(ad, 2); cout << ans + ad << 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 a[222222]; int main() { int n, b = 0, s = 0; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { if (a[i] == a[i + 1]) b++; else s++; } cout << s + min(2, b) << 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 n, a[100010], l[100010], r[100010], ans, f1[100010][2], f2[100010][2]; int main() { cin >> n; for (int i = 1; i <= n; ++i) { char x; cin >> x; if (x == '0') a[i] = 0; else a[i] = 1; } l[1] = 1; f1[1][a[1]] = 1; for (int i = 2; i <= n; ++i) { if (a[i] != a[i - 1]) l[i] = l[i - 1]; else l[i] = i; f1[i][0] = f1[i - 1][0]; f1[i][1] = f1[i - 1][1]; f1[i][a[i]] = max(f1[i][!a[i]], f1[i][!a[i]] + 1); } r[n] = n; f2[n][a[n]] = 1; for (int i = n - 1; i >= 1; --i) { if (a[i] != a[i + 1]) r[i] = r[i + 1]; else r[i] = i; f2[i][0] = f2[i + 1][0]; f2[i][1] = f2[i + 1][1]; f2[i][a[i]] = max(f2[i][a[i]], f2[i][!a[i]] + 1); } for (int i = 1; i <= n; ++i) { int tmp = f1[i - 1][a[i]] + r[i] - i + 1 + f2[r[i] + 1][a[r[i]]]; ans = max(ans, tmp); if (r[i] != i) { tmp = f1[i - 1][a[i]] + (r[i] - 1) - i + 1 + f2[r[i]][!a[r[i]]]; ans = max(ans, tmp); } } cout << ans; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; long long int n; void solve() { cin >> n; string s; cin >> s; int ccnt = 1; vector<long long int> seg; for (int i = 1; i < n; i++) { if (s[i] == s[i - 1]) ccnt++; else { seg.push_back(ccnt); ccnt = 1; } } seg.push_back(ccnt); sort(seg.begin(), seg.end()); if (seg.size() == 1) { if (seg[0] >= 3) { cout << 3 << endl; } else if (seg[0] == 2) cout << 2 << endl; else cout << 1 << endl; return; } if (seg[seg.size() - 1] >= 3) { cout << seg.size() + 2 << endl; } else if (seg[seg.size() - 2] >= 2) { cout << seg.size() + 2 << endl; return; } else if (seg[seg.size() - 1] >= 2) { cout << seg.size() + 1 << endl; } else { cout << seg.size() << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) solve(); }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.ObjectInputStream.GetField; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; public class Q2 { static long MOD = 1000000007; static boolean b[]; static ArrayList<Integer>[] amp; static int sum[],dist[]; static long ans = 0; static int p = 0; static FasterScanner sc = new FasterScanner(); static Queue<Integer> q = new LinkedList<>(); static ArrayList<String>[] arr; static ArrayList<Integer> parent = new ArrayList<>(); static BufferedWriter log; static HashMap<Long,Long> hm; static Pair prr[]; public static void main(String[] args) throws IOException { log = new BufferedWriter(new OutputStreamWriter(System.out)); int n = sc.nextInt(); String str = sc.nextLine(); int ans =0; char temp = 'a'; for(int i = 0;i<n;i++){ if(str.charAt(i)!=temp){ ans++; temp = str.charAt(i); } } if(n==1) System.out.println(1); else if(str.contains("11")||str.contains("00")) System.out.println(Math.min(n,ans+2)); else{ if(str.charAt(0)==str.charAt(1) || str.charAt(n-1)==str.charAt(n-2)) System.out.println(Math.min(n,ans+1)); else System.out.println(Math.min(n,ans)); } log.close(); } public static char give(char c1,char c2){ if(c1!='A' && c2!='A') return 'A'; if(c1!='B' && c2!='B') return 'B'; return 'C'; } static class Graph{ int vertex; long weight; } public static void buildTree(int n){ int arr[] = sc.nextIntArray(n); for(int i = 0;i<n;i++){ int x = arr[i]-1; amp[i+1].add(x); amp[x].add(i+1); } } public static void bfs(int x,long arr[]){ b[x] = true; q.add(x); while(!q.isEmpty()){ int y = q.poll(); for(int i = 0;i<amp[y].size();i++) { if(!b[amp[y].get(i)]){ q.add(amp[y].get(i)); b[amp[y].get(i)] = true; } } } } public static void buildGraph(int n){ for(int i =0;i<n;i++){ int x = sc.nextInt(), y = sc.nextInt(); amp[--x].add(--y); amp[y].add(x); prr[i] = new Pair(x,y); } } public static void dfs(int x){ b[x] = true; for(int i =0 ;i<amp[x].size();i++){ if(!b[amp[x].get(i)]){ dfs(amp[x].get(i)); } } } static class Pair implements Comparable<Pair> { long u; long v; public Pair(){ } public Pair(long u, long v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31*hu + hv; } public boolean equals(Object o) { Pair other = (Pair) o; return (u == other.u && v == other.v); } public int compareTo(Pair other) { return Long.compare(u, other.u) != 0 ? (Long.compare(u, other.u)) : (Long.compare(v, other.v)); } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } static class SegmentTree{ int st[][]; SegmentTree(char arr[], int n){ int size = 4*n+1; st = new int[size][3]; build(arr,0,n-1,0); } int[] build(char arr[], int ss, int se, int si){ if(ss==se){ st[si][0] = 0; if(arr[ss]=='(') st[si][1] = 1; else st[si][2] = 1; return st[si]; } int mid = (ss+se)/2; int amp[] = build(arr,ss,mid,si*2+1); int bmp[] = build(arr,mid+1,se,si*2+2); int t = Math.min(amp[1], bmp[2]); st[si][0] = amp[0]+bmp[0]+2*t; st[si][1] = amp[1]+bmp[1]-t; st[si][2] = amp[2]+bmp[2]-t; return st[si]; } int[] getXor(int qs, int qe, int ss, int se, int si){ if(qe<ss || qs>se){ return null; } if(qs<=ss && qe>=se) return st[si]; int mid = (ss+se)/2; int amp[]= getXor(qs,qe,ss,mid,2*si+1); int bmp[] = getXor(qs,qe, mid+1, se, 2*si+2); if(amp==null) return bmp; else if(bmp == null) return amp; int[] a=new int[3]; int t=Math.min(amp[1],bmp[2]); a[0]=amp[0]+bmp[0]+2*t; a[1]=amp[1]+bmp[1]-t; a[2]=amp[2]+bmp[2]-t; return a; } void print(){ for(int i = 0;i<st.length;i++){ for(int j = 0;j<3;j++) System.out.print(st[i][j]+" "); System.out.println("----------------------"); } } } static int convert(int x){ int cnt = 0; String str = Integer.toBinaryString(x); //System.out.println(str); for(int i = 0;i<str.length();i++){ if(str.charAt(i)=='1'){ cnt++; } } int ans = (int) Math.pow(3, 6-cnt); return ans; } static class Node2{ Node2 left = null; Node2 right = null; Node2 parent = null; int data; } static class BinarySearchTree{ Node2 root = null; int height = 0; int max = 0; int cnt = 1; ArrayList<Integer> parent = new ArrayList<>(); HashMap<Integer, Integer> hm = new HashMap<>(); public void insert(int x){ Node2 n = new Node2(); n.data = x; if(root==null){ root = n; } else{ Node2 temp = root,temp2 = null; while(temp!=null){ temp2 = temp; if(x>temp.data) temp = temp.right; else temp = temp.left; } if(x>temp2.data) temp2.right = n; else temp2.left = n; n.parent = temp2; parent.add(temp2.data); } } public Node2 getSomething(int x, int y, Node2 n){ if(n.data==x || n.data==y) return n; else if(n.data>x && n.data<y) return n; else if(n.data<x && n.data<y) return getSomething(x,y,n.right); else return getSomething(x,y,n.left); } public Node2 search(int x,Node2 n){ if(x==n.data){ max = Math.max(max, n.data); return n; } if(x>n.data){ max = Math.max(max, n.data); return search(x,n.right); } else{ max = Math.max(max, n.data); return search(x,n.left); } } public int getHeight(Node2 n){ if(n==null) return 0; height = 1+ Math.max(getHeight(n.left), getHeight(n.right)); return height; } } static long findDiff(long[] arr, long[] brr, int m){ int i = 0, j = 0; long fa = 1000000000000L; while(i<m && j<m){ long x = arr[i]-brr[j]; if(x>=0){ if(x<fa) fa = x; j++; } else{ if((-x)<fa) fa = -x; i++; } } return fa; } public static long max(long x, long y, long z){ if(x>=y && x>=z) return x; if(y>=x && y>=z) return y; return z; } public static void seive(long n){ b = new boolean[(int) (n+1)]; Arrays.fill(b, true); for(int i = 2;i<=n;i++){ if(b[i]){ for(int p = 2*i;p<=n;p++){ b[p] = false; } } } } static long modInverse(long a, long mOD2){ return power(a, mOD2-2, mOD2); } static long power(long x, long y, long m) { if (y == 0) return 1; long p = power(x, y/2, m) % m; p = (p * p) % m; return (y%2 == 0)? p : (x * p) % m; } static long power2(long x,BigInteger y,long m){ long ans = 1; BigInteger two = new BigInteger("2"); while(y.compareTo(BigInteger.ZERO)>0){ if(y.getLowestSetBit()==y.bitCount()){ x = (x*x)%MOD; y = y.divide(two); } else{ ans = (ans*x)%MOD; y = y.subtract(BigInteger.ONE); } } return ans; } static BigInteger power2(BigInteger x, BigInteger y, BigInteger m){ BigInteger ans = new BigInteger("1"); BigInteger one = new BigInteger("1"); BigInteger two = new BigInteger("2"); BigInteger zero = new BigInteger("0"); while(y.compareTo(zero)>0){ //System.out.println(y); if(y.mod(two).equals(one)){ ans = ans.multiply(x).mod(m); y = y.subtract(one); } else{ x = x.multiply(x).mod(m); y = y.divide(two); } } return ans; } static BigInteger power(BigInteger x, BigInteger y, BigInteger m) { if (y.equals(0)) return (new BigInteger("1")); BigInteger p = power(x, y.divide(new BigInteger("2")), m).mod(m); p = (p.multiply(p)).mod(m); return (y.mod(new BigInteger("2")).equals(0))? p : (p.multiply(x)).mod(m); } static long d,x,y; public static void extendedEuclidian(long a, long b){ if(b == 0) { d = a; x = 1; y = 0; } else { extendedEuclidian(b, a%b); int temp = (int) x; x = y; y = temp - (a/b)*y; } } public static long gcd(long n, long m){ if(m!=0) return gcd(m,n%m); else return 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) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int off) { int[] arr = new int[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextInt(); } return arr; } public long[] nextLongArray(int n) { return nextLongArray(n, 0); } public long[] nextLongArray(int n, int off) { long[] arr = new long[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.Arrays; import java.util.StringTokenizer; import static java.lang.Math.max; /** * 603A * * @author artyom */ public class AlternativeThinking implements Runnable { private BufferedReader in; private PrintStream out; private StringTokenizer tok; private void solve() throws IOException { int n = nextInt(); char[] str = nextToken().toCharArray(); int[][] dp = new int[n][3]; Arrays.fill(dp[n - 1], 1); for (int k = n - 2; k >= 0; k--) { if (str[k] != str[k + 1]) { dp[k][0] = max(dp[k + 1][0] + 1, dp[k + 1][2]); dp[k][1] = dp[k + 1][1] + 1; dp[k][2] = max(dp[k + 1][1], dp[k + 1][2] + 1); } else { dp[k][0] = max(dp[k + 1][0], dp[k + 1][2] + 1); dp[k][1] = dp[k + 1][1]; dp[k][2] = max(dp[k + 1][1] + 1, dp[k + 1][2]); } } out.print(max(dp[0][0], dp[0][2])); } //-------------------------------------------------------------- public static void main(String[] args) { new AlternativeThinking().run(); } @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = System.out; tok = null; solve(); in.close(); } catch (IOException e) { System.exit(0); } } private String nextToken() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private int[] readIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } }
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 PC { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Communication S=new Communication(); int n=S.i(); char[] c=S.cs(); int[] l=new int[c.length]; for(int i=0;i<n;i++){ if(c[i]=='0'){ l[i]=0; }else{ l[i]=1; } } int hadNo=0; int last=l[0]; int rate=1; for(int i=1;i<n;i++){ if(l[i]!=last){ rate++; last=l[i]; }else{ hadNo++; } } if(hadNo==1){ rate=rate+1; } if(hadNo>1){ rate=rate+2; } S.send(rate); } ///////////////////////////////////// static class Communication { Scanner sc; public static void send(Object x){ System.out.println(x); } public static void sendw(Object x){ System.out.print(x); } public Communication(){ sc=new Scanner(System.in); } public int i(){ return Integer.valueOf(sc.nextLine()); } public int[] is(){ String s=sc.nextLine(); String[] s2=s.split(" "); int[] j=new int[s2.length]; for(int i=0;i<s2.length;i++){ j[i]=Integer.valueOf(s2[i]); } return j; } public String s(){ return sc.nextLine(); } public String[] ss(){ return sc.nextLine().split(" "); } public char c(){ return sc.nextLine().charAt(0); } public char[] cs(){ return sc.nextLine().toCharArray(); } public long l(){ return Long.valueOf(sc.nextLine()); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; char str[101000]; int arr[101000]; int n; int memo[101000][4][4]; int base[101000][4][4]; int solve(int idx, int d, int k) { int i, j; if (idx == n) { return 0; } if (base[idx][d][k] == 1) return memo[idx][d][k]; base[idx][d][k] = 1; i = idx; while (i < n && arr[i] != d) i++; int tk, nt; if (i == n) nt = 0; else nt = 1 + solve(i + 1, 1 - d, k); i = idx; if (k == 0) return memo[idx][d][k] = nt; while (i < n && arr[i] != (1 - d)) i++; if (i == n) return memo[idx][d][k] = nt; tk = 1 + solve(i + 1, d, k - 1); return memo[idx][d][k] = max(tk, nt); } int main() { int i, j, k; scanf("%d", &n); scanf("%s", str); for (i = 0; i < n; i++) arr[i] = str[i] - '0'; int ans = max(solve(0, 1 - arr[0], 1), solve(0, arr[0], 2)); printf("%d\n", ans); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n; cin >> s; int prev; int ans = 0; for (int i = 0; i < s.size(); i++) { if (s[i] != prev || i == 0) ans++, prev = s[i]; } ans = min(n, ans + 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
#include <bits/stdc++.h> using namespace std; const long long int MAX = 1e7 + 7; const long long int MOD = 1000000007; void fast() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); } void read(long long int n, vector<long long int> &v) { for (long long int i = 0; i < n; i++) cin >> v[i]; } vector<long long int> dp; long long int n; string s; int main() { fast(); long long int TEST_CASES = 1; while (TEST_CASES--) { cin >> n; cin >> s; if (n <= 3) cout << n << "\n"; else { long long int tot = 1; for (int i = 1; i < n; i++) tot += (s[i] != s[i - 1]); tot = min(n, tot + 2); cout << tot << "\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.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main { public static void main(String[] args) { // Test.testing(); ConsoleIO io = new ConsoleIO(); new Main(io).solve(); io.flush(); } ConsoleIO io; Main(ConsoleIO io) { this.io = io; } ArrayList<ArrayList<Integer>> gr1; ArrayList<ArrayList<Integer>> gr2; boolean[] visit; // public void solve() { io.readInt(); char[] c = io.readLine().toCharArray(); int[] n = new int[c.length]; int k = 0; n[0] = 1; char a = c[0]; for (int i = 1; i < c.length; i++) { if (c[i] != a) ++k; n[k]++; a = c[i]; } k++; int v2 = 0; for (int i = 0; i < k; i++) { if (n[i] >= 3) { io.writeLine("" + (k + 2)); return; } if (n[i] == 2) v2++; } io.writeLine("" + (k + Math.min(v2,2))); } } class ConsoleIO { BufferedReader br; PrintWriter out; public ConsoleIO(){br = new BufferedReader(new InputStreamReader(System.in));out = new PrintWriter(System.out);} public void flush(){this.out.close();} public void writeLine(String s) {this.out.println(s);} public void writeInt(int a) {this.out.print(a);this.out.print(' ');} public void writeWord(String s){ this.out.print(s); } public String readLine() {try {return br.readLine();}catch (Exception ex){ return "";}} public long readLong() { return Long.parseLong(this.readLine()); } public int readInt() { return Integer.parseInt(this.readLine().trim()); } public long[] readLongArray() { String[]n=this.readLine().trim().split(" ");long[]r=new long[n.length]; for(int i=0;i<n.length;i++)r[i]=Long.parseLong(n[i]); return r; } public int[] readIntArray() { String[]n=this.readLine().trim().split("\\s+");int[]r=new int[n.length]; for(int i=0;i<n.length;i++)r[i]=Integer.parseInt(n[i]); return r; } }
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 c { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int N = scan.nextInt();int ans = 1; String line = scan.next(); char bit = line.charAt(0); for (int i=1;i<N;i++) if (line.charAt(i)!=bit){ bit=(bit=='0')?'1':'0'; ans++; } System.out.println(Math.min(N, ans+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 #sys.stdin = open('in') n = int(raw_input()) s= raw_input() p1 = 0 p0 = 0 lp1 = 0 lp0 = 0 res = 1 i = 1 while i < len(s): if s[i] != s[i-1]: res += 1 i += 1 else: if s[i] == '0': p0 += 1 l = 1 while i < len(s) and s[i] == s[i-1]: i += 1 l += 1 if l > lp0: lp0 = l else: p1 += 1 l = 1 while i < len(s) and s[i] == s[i-1]: i += 1 l += 1 if l > lp1: lp1 = l if (p0 + p1) >= 2 or lp0 >= 3 or lp1 >= 3: res += 2 elif p0 == 1 or p1 == 1: res += 1 print res
PYTHON
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n=int(input()) s=input() ans=0 a=0 b=0 if s[0]=='0': flag='0' else: flag='1' for i in range(n): if i>0 and s[i]==s[i-1]: a+=1 if s[i]==flag: ans+=1 if flag=='0': flag='1' else: flag='0' ans+=min(a+b,2) 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 s[100010]; int n, ans, k; int main() { scanf("%d%s", &n, s); ans = 1; k = 0; for (int i = 1; i < n; i++) if (s[i] != s[i - 1]) ans++; else k++; ans += min(k, 2); printf("%d\n", ans); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int s = 1, n = 0; string a; cin >> n >> a; for (int i = 1; i <= n - 1; i++) if (a[i] != a[i - 1]) s++; s = min(s + 2, n); cout << s; 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.InputStreamReader; import java.util.Arrays; public class CC { static int n; static char[] s; static int DP[][][] = new int[3][3][100003]; static int getMax(int curr, int alter, int prev) { if (curr >= n) return 0; if (DP[alter][prev][curr] != -1) return DP[alter][prev][curr]; int ans = getMax(curr + 1, alter == 0 ? 0 : 2, prev); if (alter > 0) { if (alter == 1) { // can swap curr if (s[curr] == '1') { if (prev == 0) { ans = max(ans, 1 + getMax(curr + 1, 2, 1)); } else { ans = max(ans, 1 + getMax(curr + 1, 1, 0)); // swap me } } else { if (prev == 0) { ans = max(ans, 1 + getMax(curr + 1, 1, 1)); } else { ans = max(ans, 1 + getMax(curr + 1, 2, 0)); // swap // 10111101 // me } } } else { // can't swap curr if (s[curr] == '1') { if (prev == 0) { ans = max(ans, 1 + getMax(curr + 1, alter, 1)); } } else { if (prev == 1) { ans = max(ans, 1 + getMax(curr + 1, alter, 0)); } } } } else { if (prev == 2) { if (s[curr] == '1') { ans = max(ans, 1 + getMax(curr + 1, alter, 1)); ans = max(ans, 1 + getMax(curr + 1, 1, 0)); } else { ans = max(ans, 1 + getMax(curr + 1, alter, 0)); ans = max(ans, 1 + getMax(curr + 1, 1, 1)); } } else { if (s[curr] == '1') { if (prev == 0) { ans = max(ans, 1 + getMax(curr + 1, alter, 1)); } else { ans = max(ans, 1 + getMax(curr + 1, 1, 0)); } } else { if (prev == 0) { ans = max(ans, 1 + getMax(curr + 1, 1, 1)); } else { ans = max(ans, 1 + getMax(curr + 1, alter, 0)); } } } } return DP[alter][prev][curr] = ans; } private static int max(int ans, int i) { return Math.max(ans, i); } public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String[] l = bf.readLine().split(" "); n = Integer.parseInt(l[0]); s = bf.readLine().toCharArray(); for (int i = 0; i < DP.length; i++) { for (int j = 0; j < DP[i].length; j++) { Arrays.fill(DP[i][j], -1); } } System.out.println(getMax(0, 0, 2)); } }
JAVA