Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; char S[MAXN]; int main(void) { int N; scanf("%d", &N); scanf("%s", S); int ans = 1; int curr = 0; static vector<int> vec; for (int i = 1; i < N; ++i) { if (S[i] != S[i - 1]) { vec.push_back(i - curr); curr = i; } } vec.push_back(N - curr); ans = (int)vec.size(); if (vec[0] > 1 || vec.back() > 1) ans = max(ans, (int)vec.size() + 1); for (int i = 0; i < (int)vec.size(); ++i) if (vec[i] > 2) ans = max(ans, (int)vec.size() + 2); int cnt = 0; for (int i = 0; i < (int)vec.size(); ++i) { if (vec[i] > 1) ++cnt; } if (cnt > 1) ans = (int)vec.size() + 2; if (cnt > 0) ans = max(ans, (int)vec.size() + 1); 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 n, o = 0; char s[100000] = {}; int main() { ios::sync_with_stdio(false); cin >> n; cin >> s; for (int i = 0; i < (n - 1); ++i) { if (s[i] != s[i + 1]) ++o; } cout << min(n, o + 3); }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; char str[100010]; int main() { int n, i, fl = 0, tot = 0; scanf("%d ", &n); int ed = n; for (i = 0; i < n; i++) { scanf("%c", &str[i]); if (i) { if (fl && str[i] != str[i - 1]) ed = i; if (str[i] == str[i - 1]) { fl = 1; } if (fl) { str[i] = '0' + '1' - str[i]; } } } for (i = ed; i < n; i++) { str[i] = '0' + '1' - str[i]; } tot = 1; char c = str[0]; for (i = 1; i < n; i++) { if (str[i] != c) { tot++; c = str[i]; } } printf("%d\n", tot); 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.Arrays; import java.util.StringTokenizer; public class Main { static MyScanner in; static PrintWriter out; // static Timer timer = new Timer(); public static void main(String[] args) throws IOException { in = new MyScanner(); out = new PrintWriter(System.out, true); int n = in.nextInt(); String s = in.next(); dp = new Integer[3][n]; out.println(Math.max(solve(0, n - 1, s), Math.max(solve(1, n - 1, s), solve(2, n - 1, s)))); } static final int NO = 0; static final int IS = 1; static final int WAS = 2; static Integer[][] dp; static int solve(int state, int len, String s) { if (len < 0) return 0; if (dp[state][len] == null) { if (state == NO) { dp[state][len] = solve(NO, len - 1, s) + isDiff(len - 1, len, s); } else if (state == IS) { dp[state][len] = Math.max( solve(NO, len - 1, s) + isSame(len - 1, len, s), solve(IS, len - 1, s) + isDiff(len - 1, len, s)); } else { dp[state][len] = Math.max( solve(IS, len - 1, s) + isSame(len - 1, len, s), solve(WAS, len - 1, s) + isDiff(len - 1, len, s)); } } return dp[state][len]; } private static int isDiff(int l, int r, String s) { return l == -1 || s.charAt(l) != s.charAt(r) ? 1 : 0; } private static int isSame(int l, int r, String s) { if (l == -1) return 1; return 1 - isDiff(l, r, s); } } class MyScanner { public static final String CP1251 = "cp1251"; private final BufferedReader br; private StringTokenizer st; private String last; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public MyScanner(String path) throws IOException { br = new BufferedReader(new FileReader(path)); } public MyScanner(String path, String decoder) throws IOException { br = new BufferedReader(new InputStreamReader(new FileInputStream(path), decoder)); } String next() throws IOException { while (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine()); last = null; return st.nextToken(); } String next(String delim) throws IOException { while (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine()); last = null; return st.nextToken(delim); } String nextLine() throws IOException { st = null; return (last == null) ? br.readLine() : last; } boolean hasNext() { if (st != null && st.hasMoreElements()) return true; try { while (st == null || !st.hasMoreElements()) { last = br.readLine(); st = new StringTokenizer(last); } } catch (Exception e) { return false; } return true; } String[] nextStrings(int n) throws IOException { String[] arr = new String[n]; for (int i = 0; i < n; i++) arr[i] = next(); return arr; } String[] nextLines(int n) throws IOException { String[] arr = new String[n]; for (int i = 0; i < n; i++) arr[i] = nextLine(); return arr; } int nextInt() throws IOException { return Integer.parseInt(next()); } int[] nextInts(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } Integer[] nextIntegers(int n) throws IOException { Integer[] arr = new Integer[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } int[][] next2Ints(int n, int m) throws IOException { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) arr[i][j] = nextInt(); return arr; } long nextLong() throws IOException { return Long.parseLong(next()); } long[] nextLongs(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } long[][] next2Longs(int n, int m) throws IOException { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) arr[i][j] = nextLong(); return arr; } double nextDouble() throws IOException { return Double.parseDouble(next().replace(',', '.')); } double[] nextDoubles(int size) throws IOException { double[] arr = new double[size]; for (int i = 0; i < size; i++) arr[i] = nextDouble(); return arr; } double[][] next2Doubles(int n, int m) throws IOException { double[][] arr = new double[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) arr[i][j] = nextDouble(); return arr; } boolean nextBool() throws IOException { String s = next(); if (s.equalsIgnoreCase("true") || s.equals("1")) return true; if (s.equalsIgnoreCase("false") || s.equals("0")) return false; throw new IOException("Boolean expected, String found!"); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; long long int n, ar[100000], i, lp = 2, cnt = 1, x, y, ans; priority_queue<int> pq; string s; int main() { cin >> n >> s; for (i = 0; i < n; i++) { if (s[i] == lp) cnt++; else { ans++; pq.push(cnt); cnt = 1; } lp = s[i]; } pq.push(cnt); if (pq.size() > 1) { x = pq.top(); pq.pop(); if (x > 2) { cout << ans + 2; return 0; } if (x == 1) { cout << ans; return 0; } if (pq.top() == 2) { cout << ans + 2; return 0; } cout << ans + 1; } else { if (pq.top() == 1) cout << ans; else if (pq.top() == 2) cout << ans + 1; else cout << 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
n=int(input()) s=[int(i) for i in input()] dp=[[0 for _ in range(2)]for _ in range(3)] for i in range(n): x=s[i] dp[2][x]=max(dp[2][x],max(dp[2][1^x],dp[1][1^x])+1) dp[1][1^x]=max(dp[1][1^x],max(dp[1][x],dp[0][x])+1) dp[0][x]=max(dp[0][x],dp[0][1^x]+1) print(max(dp[2][0],dp[2][1]))
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n = input() s = raw_input() cnt, parts, flag = 0, 0, False i = 0 while i < n: parts += 1 if i < n - 1 and s[i] == s[i+1]: cnt += 1 acc = 1 while i < n - 1 and s[i] == s[i+1]: i += 1 acc += 1 if acc >= 3: flag = True i += 1 if cnt == 1 and flag: cnt = 2 print parts + min(2, cnt)
PYTHON
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
###### ### ####### ####### ## # ##### ### ##### # # # # # # # # # # # # # ### # # # # # # # # # # # # # ### ###### ######### # # # # # # ######### # ###### ######### # # # # # # ######### # # # # # # # # # # # #### # # # # # # # # # # ## # # # # # ###### # # ####### ####### # # ##### # # # # from __future__ import print_function # for PyPy2 # from itertools import permutations # from functools import cmp_to_key # for adding custom comparator # from fractions import Fraction from collections import * from sys import stdin # from bisect import * # from heapq import * from math import * g = lambda : stdin.readline().strip() gl = lambda : g().split() gil = lambda : [int(var) for var in gl()] gfl = lambda : [float(var) for var in gl()] gcl = lambda : list(g()) gbs = lambda : [int(var) for var in g()] mod = int(1e9)+7 inf = float("inf") n, = gil() l = [] zero, one = [], [] for ch in g(): if l and ch == l[-1][0]: l[-1][1] += 1 else: l.append([ch, 1]) for ch, fi in l: (zero if ch == '0' else one).append(fi) ans = 0 zero.sort() one.sort() dt = 0 if one and one[-1] > 1: dt += 1 if zero and zero[-1] > 1: dt += 1 if len(one) > 1 and one[-2] > 1 : dt += 1 if len(zero) > 1 and zero[-2] > 1 : dt += 1 if one and one[-1] > 2:dt = inf if zero and zero[-1] > 2:dt = inf if dt > 1 : ans += 2 elif dt : ans += 1 print(ans + len(l))
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Queue; import java.util.Random; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; //import java.util.TreeSet; public class hacker49 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { OutputStream outputStream =System.out; PrintWriter out =new PrintWriter(outputStream); FastReader s=new FastReader(); int n=s.nextInt(); String str=s.next(); int[] a=new int[n+1]; for(int i=1;i<=n;i++) { a[i]=(int)(str.charAt(i-1)-'0'); } int c=1; for(int i=2;i<=n;i++) { if(a[i]!=a[i-1]) { c++; } } int k=-1; int u=-1; for(int i=1;i<n;i++) { if(a[i]==a[i+1]) { k=i; break; } } for(int i=n;i>=2;i--) { if(a[i]==a[i-1]) { u=i; break; } } if(k!=-1 && u!=-1 && u-k>1) { c+=2; }else if(k!=-1 || u!=-1) { c++; } System.out.println(c); out.close(); } static void sf(long[] arr){ int n = arr.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = arr[i]; int randomPos = i + rnd.nextInt(n-i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static HashMap<Integer,pair> e=new HashMap<>(); static ArrayList<Integer>[] f1=new ArrayList[200001]; static PriorityQueue<pair> q=new PriorityQueue<>(); static ArrayList<pair>[] f=new ArrayList[200001]; static class pair implements Comparable<pair>{ private int a; private int b; pair(int a,int b){ this.a=a; this.b=b; } public int compareTo(pair o) { return Integer.compare(o.b, this.b); } // } static int[] col=new int[200001]; static int[] subtree=new int[200001]; static int[] vis=new int[200001]; static int dfs(int node) { vis[node]=1; int c=0; for(int i=0;i<f[node].size();i++) { if(vis[f[node].get(i).a]==0) { c+=dfs(f[node].get(i).a); } } subtree[node]+=c; subtree[node]+=col[node]; return subtree[node]; } public static int upper_bound(long[] a ,long x,int n) { int l=-1; int r=n; while(r>l+1) { int mid=(l+r)/2; if(a[mid]<x) { l=mid; }else { r=mid; } } return r; } public static int lower_bound(long[] a ,long x,int n) { int l=-1; int r=n; while(r>l+1) { int mid=(l+r)/2; if(a[mid]<=x) { l=mid; }else { r=mid; } } return l; } public static long[] merge_sort(long[] A, int start, int end) { if (end > start) { int mid = (end + start) / 2; long[] v = merge_sort(A, start, mid); long[] o = merge_sort(A, mid + 1, end); return (merge(v, o)); } else { long[] y = new long[1]; y[0] = A[start]; return y; } } public static long[] merge(long a[], long b[]) { // int count=0; long[] temp = new long[a.length + b.length]; int m = a.length; int n = b.length; int i = 0; int j = 0; int c = 0; while (i < m && j < n) { if (a[i] < b[j]) { temp[c++] = a[i++]; } else { temp[c++] = b[j++]; } } while (i < m) { temp[c++] = a[i++]; } while (j < n) { temp[c++] = b[j++]; } return temp; } public static int[] Create(int[] a,int n) { int[] b=new int[n+1]; for(int i=1;i<=n;i++) { int j=i; int h=a[i]; while(i<=n) { b[i]+=h; i=get_next(i); } i=j; } return b; } public static int get_next(int a) { return a+(a&-a); } public static int get_parent(int a) { return a-(a&-a); } public static int query_1(int[] b,int index) { int sum=0; if(index<=0) { return 0; } while(index>0) { sum+=b[index]; index=get_parent(index); } return sum; } public static int query(int[] b,int n,int l,int r) { int sum=0; sum+=query_1(b,r); sum-=query_1(b,l-1); return sum; } public static void update(int[] a,int[] b,int n,int index,int val) { int diff=val-a[index]; a[index]+=diff; while(index<=n) { b[index]+=diff; index=get_next(index); } } // public static void Create(int[] a,pair[] segtree,int low,int high,int pos) { // if(low>high) { // return; // } // if(low==high) { // segtree[pos].b.add(a[low]); // return ; // } // int mid=(low+high)/2; // Create(a,segtree,low,mid,2*pos); // Create(a,segtree,mid+1,high,2*pos+1); // segtree[pos].b.addAll(segtree[2*pos].b); // segtree[pos].b.addAll(segtree[2*pos+1].b); // } // public static void update(pair[] segtree,int low,int high,int index,int pos,int val,int prev) { // if(index>high || index<low) { // return ; // } // if(low==high && low==index) { // segtree[pos].b.remove(prev); // segtree[pos].b.add(val); // return; // } // int mid=(high+low)/2; // update(segtree,low,mid,index,2*pos,val,prev); // update(segtree,mid+1,high,index,2*pos+1,val,prev); // segtree[pos].b.clear(); // segtree[pos].b.addAll(segtree[2*pos].b); // segtree[pos].b.addAll(segtree[2*pos+1].b); // // } // public static pair query(pair[] segtree,int low,int high,int qlow,int qhigh ,int pos) { // if(low>=qlow && high<=qhigh) { // return segtree[pos]; // } // if(qhigh<low || qlow>high) { // return new pair(); // } // int mid=(low+high)/2; // pair a1=query(segtree,low,mid,qlow,qhigh,2*pos); // pair a2=query(segtree,mid+1,high,qlow,qhigh,2*pos+1); // pair a3=new pair(); // a3.b.addAll(a1.b); // a3.b.addAll(a2.b); // return a3; // // } // // public static int nextPowerOf2(int n) // { // n--; // n |= n >> 1; // n |= n >> 2; // n |= n >> 4; // n |= n >> 8; // n |= n >> 16; // n++; // return n; // } // //// static class pair implements Comparable<pair>{ //// private int a; //// private long b; ////// private long c; //// pair(int a,long b){ //// this.a=a; //// this.b=b; ////// this.c=c; //// } //// public int compareTo(pair o) { ////// if(this.a!=o.a) { ////// return Long.compare(this.a, o.a); ////// }else { //// return Long.compare(o.b,this.b); ////// } //// } //// } // static class pair implements Comparable<pair>{ // private int a; // private int b; // pair(int a,int b){ //// this.b=new HashSet<>(); // this.a=a; // this.b=b; // } // public int compareTo(pair o) { // return Integer.compare(this.b, o.b); // } // } public static int lower_bound(ArrayList<Long> a ,int n,long x) { int l=0; int r=n; while(r>l+1) { int mid=(l+r)/2; if(a.get(mid)<=x) { l=mid; }else { r=mid; } } return l; } public static int[] is_prime=new int[1000001]; public static ArrayList<Long> primes=new ArrayList<>(); public static void sieve() { long maxN=1000000; for(long i=1;i<=maxN;i++) { is_prime[(int) i]=1; } is_prime[0]=0; is_prime[1]=0; for(long i=2;i*i<=maxN;i++) { if(is_prime[(int) i]==1) { // primes.add((int) i); for(long j=i*i;j<=maxN;j+=i) { is_prime[(int) j]=0; } } } for(long i=0;i<=maxN;i++) { if(is_prime[(int) i]==1) { primes.add(i); } } } // public static pair[] merge_sort(pair[] A, int start, int end) { // if (end > start) { // int mid = (end + start) / 2; // pair[] v = merge_sort(A, start, mid); // pair[] o = merge_sort(A, mid + 1, end); // return (merge(v, o)); // } else { // pair[] y = new pair[1]; // y[0] = A[start]; // return y; // } // } // public static pair[] merge(pair a[], pair b[]) { // pair[] temp = new pair[a.length + b.length]; // int m = a.length; // int n = b.length; // int i = 0; // int j = 0; // int c = 0; // while (i < m && j < n) { // if (a[i].b >= b[j].b) { // temp[c++] = a[i++]; // // } else { // temp[c++] = b[j++]; // } // } // while (i < m) { // temp[c++] = a[i++]; // } // while (j < n) { // temp[c++] = b[j++]; // } // return temp; // } // public static long im(long a) { // return binary_exponentiation_1(a,mod-2)%mod; // } public static double bn1(double a,double n) { double res=1; while(n>0) { if(n%2!=0) { // res=((res)%(1000000007) * (a)%(1000000007))%(1000000007); res=(res*a); n--; }else { // a=((a)%(1000000007) *(a)%(1000000007))%(1000000007); a=(a*a); n/=2; } } return (res); } public static long bn(long a,long n) { long res=1; while(n>0) { if(n%2!=0) { res=res*a; n--; }else { a*=a; n/=2; } } return res; } public static long[] fac=new long[100001]; public static void find_factorial() { fac[0]=1; fac[1]=1; for(int i=2;i<=100000;i++) { fac[i]=(fac[i-1]*i)%(mod); } } static long mod=1000000007; public static long GCD(long a,long b) { if(b==(long)0) { return a; } return GCD(b , a%b); } static long c=0; }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int check(int* arr, int n, int h) { vector<int> index, v2; int i = 0, last, ans = 0; while (i < n) { if (arr[i] == h) { last = h; index.push_back(i); ans = 1; i++; break; } else v2.push_back(i); i++; } while (i < n) { if (arr[i] + last == 1) { index.push_back(i); last = 1 - last; ans++; } else v2.push_back(i); i++; } if (index.size() == 0) return 1; if (ans == n) return ans; int temp = index[0]; for (i = 1; i < index.size(); i++) temp = max(temp, index[i] - index[i - 1] - 1); temp = max(temp, n - index[(int)index.size() - 1]); if (temp >= 2) return ans + 2; else { bool g = false; if (v2.size() == 1) return n; for (int i = 1; i < v2.size(); i++) { if (v2[i] != v2[i - 1] + 1) { g = true; break; } } if (g) return ans + 2; return ans + 1; } } int main() { int n; scanf("%d", &n); char str[n + 1]; int arr[n]; scanf("%s", &str); for (int i = 0; i < n; i++) arr[i] = str[i] - '0'; int temp = max(check(arr, n, 0), check(arr, n, 1)); if (temp <= n) printf("%d\n", temp); else printf("%d\n", n); }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; int ans = 3; cin >> n; string s; cin >> s; for (int i = 1; i < n; i++) { ans += s[i] != s[i - 1]; } cout << min(ans, n); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; char str[100005]; int dp[100005][2][2]; int main() { int n; while (cin >> n) { cin >> str; memset(dp, -1, sizeof(dp)); dp[0][0][0] = 1; dp[0][0][1] = -1; dp[0][1][1] = 1; dp[0][1][0] = -1; for (int i = 1; i < n; i++) { if (str[i] == str[i - 1]) { dp[i][0][0] = dp[i - 1][0][0]; dp[i][0][1] = dp[i - 1][0][1]; if (dp[i - 1][1][1] != -1) { dp[i][0][1] = max(dp[i][0][1], dp[i - 1][1][1] + 1); } dp[i][1][1] = dp[i - 1][0][0] + 1; if (dp[i - 1][1][1] != -1) { dp[i][1][1] = max(dp[i][1][1], dp[i - 1][1][1]); } } else { dp[i][0][0] = dp[i - 1][0][0] + 1; dp[i][0][1] = dp[i - 1][0][1] + 1; if (dp[i - 1][1][1] != -1) dp[i][0][1] = max(dp[i][0][1], dp[i - 1][1][1]); dp[i][1][1] = dp[i - 1][0][0]; if (dp[i - 1][1][1] != -1) dp[i][1][1] = max(dp[i][1][1], dp[i - 1][1][1] + 1); } } int ans = dp[n - 1][0][0]; ans = max(ans, dp[n - 1][0][1]); ans = max(ans, dp[n - 1][1][1]); printf("%d\n", ans); } return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author ankur */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { String str; int[][][] dp; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); str = in.readString(); int ans = -100; dp = new int[n + 1][3][2]; for (int i = 0; i < n + 1; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 2; k++) { dp[i][j][k] = Integer.MAX_VALUE; } } } ans = Math.max(solve(0, 0, 1), ans); ans = Math.max(ans, solve(0, 0, 0)); ans = Math.max(solve(0, 1, 1), ans); ans = Math.max(ans, solve(0, 1, 0)); out.println(ans); } int solve(int pos, int is, int prev) { if (pos == str.length()) { if (is == 2 || is == 1) { return 0; } return -(int) 1e6; } if (dp[pos][is][prev] != Integer.MAX_VALUE) return dp[pos][is][prev]; int ans = 0; if (is == 0) { if (str.charAt(pos) - '0' != prev) { ans = Math.max(1 + solve(pos + 1, is, (prev + 1) % 2), 1 + solve(pos + 1, 1, (prev + 1) % 2)); } else { ans = Math.max(solve(pos + 1, is, (prev)), solve(pos + 1, 1, (prev))); } } else { if (is == 1) { if (str.charAt(pos) - '0' == prev) { ans = Math.max(1 + solve(pos + 1, is, (prev + 1) % 2), 1 + solve(pos + 1, 2, (prev + 1) % 2)); } else { ans = Math.max(solve(pos + 1, is, (prev)), solve(pos + 1, 2, (prev))); } } else if (is == 2) { if (prev != str.charAt(pos) - '0') { ans = 1 + solve(pos + 1, is, str.charAt(pos) - '0'); } else { ans = solve(pos + 1, is, prev); } } } return dp[pos][is][prev] = ans; } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar; private int snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { //*-*------clare------ //remeber while comparing 2 non primitive data type not used == 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 = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { 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 boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || 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
#include <bits/stdc++.h> using namespace std; int N, c[100001]; int main() { if (fopen("input.txt", "r")) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } scanf("%d", &N); int cnt1 = 0, cnt0 = 0, cur = -1, cnt12 = 0, cnt02 = 0, Cnt0 = 0, Cnt1 = 0; int cuu = 0, C = 0; for (int i = 0; i < N; ++i) { scanf("%1d", c + i); } c[N] = -1; for (int i = 0; i <= N; ++i) { if (c[i] != cur || i == N) { C++; if (cuu == 2) { if (cur == 1) cnt12++; else if (cur == 0) cnt02++; } if (cuu > 1) { if (cur == 1) Cnt1++; else if (cur == 0) Cnt0++; } if (cur == 1) cnt1 = max(cnt1, cuu); else if (cur == 0) cnt0 = max(cnt0, cuu); cuu = 1; cur = c[i]; } else cuu++; } C--; if (cnt1 >= 3 || cnt0 >= 3) C += 2; else if (Cnt1 >= 1 && Cnt0 >= 1) C += 2; else if (cnt12 >= 2 || cnt02 >= 2) C += 2; else if (cnt12 >= 1 || cnt02 >= 1) C++; printf("%d", C); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pradyumn */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(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, FastReader in, PrintWriter out) { int n = in.nextInt(); char[] s = in.nextCharacterArray(n); int cnt = 0; for (int i = 0; i + 1 < s.length; ++i) if (s[i] == s[i + 1]) ++cnt; cnt -= 2; cnt = Math.max(cnt, 0); out.println(s.length - cnt); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } private static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public char nextCharacter() { int c = pread(); while (isSpaceChar(c)) c = pread(); return (char) c; } public char[] nextCharacterArray(int n) { char[] chars = new char[n]; for (int i = 0; i < n; i++) { chars[i] = nextCharacter(); } return chars; } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n, m; int dp[maxn][5]; char ss[maxn]; int main() { scanf("%d", &n); getchar(); scanf("%s", ss + 1); for (int i = 1; i <= n; i++) { if (ss[i] == '0') { dp[i][0] = dp[i - 1][1] + 1; dp[i][1] = dp[i - 1][1]; } else if (ss[i] == '1') { dp[i][0] = dp[i - 1][0]; dp[i][1] = dp[i - 1][0] + 1; } } int ans = max(1, max(dp[n][0], dp[n][1])); int cnt1, cnt2, l1, l2; cnt1 = cnt2 = 0; for (int i = 1; i <= n; i++) { if (ss[i] == '0') { int tt = 0; for (int j = i; j <= n; j++) { if (ss[j] == '0') { tt++; if (j == n) { if (cnt1 < tt) { cnt1 = tt; } } i++; } else { if (cnt1 < tt) { cnt1 = tt; } i = j - 1; break; } } } else if (ss[i] == '1') { int tt = 0; for (int j = i; j <= n; j++) { if (ss[j] == '1') { tt++; if (j == n) { if (cnt2 < tt) { cnt2 = tt; } } i++; } else { if (cnt2 < tt) { cnt2 = tt; } i = j - 1; break; } } } } int maxx = max(cnt1, cnt2); if (maxx >= 2) { printf("%d\n", min(n, ans + 2)); } else printf("%d\n", ans); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
public class ProblemC { public static void main(String[] args) throws java.io.IOException { try (java.util.Scanner sc = new java.util.Scanner(System.in)) { sc.next(); char[] a = sc.next().toCharArray(); int c = 0; int btw = 1; for (int i = 1; i < a.length; i++) if (a[i] == a[i - 1]) c++; else btw++; btw += Math.min(2, c); System.out.println(btw); } } }
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 init_cin() { ios_base::sync_with_stdio(false); cin.tie(0); } int d[111111][2][3]; int main() { init_cin(); int n; cin >> n; string s; cin >> s; for (int i = 1; i <= n; i++) { int c = s[i - 1] - '0'; d[i][c][0] = max(d[i - 1][c][0], d[i - 1][1 - c][0] + 1); d[i][1 - c][0] = d[i - 1][1 - c][0]; d[i][c][1] = max(d[i - 1][c][1], d[i - 1][1 - c][1] + 1); d[i][c][1] = max(d[i][c][1], d[i - 1][c][0] + 1); d[i][1 - c][1] = d[i - 1][1 - c][1]; d[i][c][2] = max(d[i - 1][c][2], d[i - 1][1 - c][2] + 1); d[i][c][2] = max(d[i][c][2], d[i - 1][c][1] + 1); d[i][1 - c][2] = d[i - 1][1 - c][2]; } int ans = 0; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { ans = max(ans, d[n][i][j]); } } 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> #pragma comment(linker, "/STACK:1024000000") #pragma warning(disable : 6031) #pragma warning(disable : 4244) #pragma warning(disable : 26451) using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const long long mod = int(1e9) + 7; const int INF = 1e9; const long long LINF = 1e18; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << setprecision(18) << fixed; int n; cin >> n; string s; cin >> s; int res = 0; for (int i = 0; i < (int)(s).size(); i++) { int cnt = 0; for (int j = i; j < (int)(s).size(); j++) if (s[j] == s[i]) cnt++; else break; res++; i += cnt - 1; } cout << min((int)(s).size(), 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; int main() { int n; cin >> n; string str; char* s; cin >> str; s = &str[0]; int pre = s[0] - '0'; int res = 1; for (int i = 1; i < n; i++) { if (pre != (s[i] - '0')) { res++; pre = s[i] - '0'; } } cout << min(n, res + 2); }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; int f = -1, l = n - 1; for (int i = 1; i < n; ++i) { if (s[i] == s[i - 1]) { if (f == -1) f = i; else l = i - 1; } } if (f == -1) f = 0; for (int i = f; i <= l; ++i) { if (s[i] == '1') s[i] = '0'; else s[i] = '1'; } int l0 = 0; int l1 = 0; for (int i = 0; i < n; ++i) { if (s[i] == '1') l1 = 1 + l0; else l0 = 1 + l1; } cout << max(l0, l1) << "\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 INF = (int)1E9; int N, P; char str[100005]; pair<int, int> pr[100005]; int main() { cin >> N; scanf("%s", str); int cnt2 = 0, cnt3 = 0; for (int i = (0); i < (N); i++) { int j = i; while (j < N && str[j] == str[i]) j++; int cnt = j - i; pr[P++] = pair<int, int>(str[i] == '1', cnt); cnt2 += (cnt >= 2); cnt3 += (cnt >= 3); i = j - 1; } int ans = P; if (cnt2 >= 2 || cnt3) { ans += 2; } else if (cnt2) { ans++; } cout << ans << endl; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { InputStream is = System.in; OutputStream os = System.out; InputReader in = new InputReader(is); PrintWriter out = new PrintWriter(os); Solver solver = new Solver(); solver.solve(in, out); out.close(); } static class Solver { void solve(InputReader in, PrintWriter out) { int n = in.nextInt(); int cnt = 1; char[] s = in.next().toCharArray(); for (int i = 1; i < s.length; i++) { if (s[i] != s[i - 1]) cnt++; } out.println(Math.min(cnt + 2, n)); } } static class InputReader { BufferedReader br = null; StringTokenizer st = null; InputReader(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } String nextLine() { String line = null; try { line = br.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return line; } String next() { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(nextLine()); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.util.*; import java.lang.*; import java.io.*; public class feb20 { public static void main(String[] srgs) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); String[] scn = (br.readLine()).trim().split(" "); int n = Integer.parseInt(scn[0]); char[] str = br.readLine().toCharArray(); int zero = 0, one = 0; for (int i = 0; i < n; i++) { if (str[i] == '0') { zero = one + 1; } else { one = zero + 1; } } int ans = Math.max(zero, one); ans = Math.min(n, ans+2); System.out.println(ans); return; } public static void sec() throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); while (t-- > 0) { String[] scn = (br.readLine()).trim().split(" "); int n = Integer.parseInt(scn[0]); long[] arr = new long[n]; scn = (br.readLine()).trim().split(" "); for (int i = 0; i < n; i++) { arr[i] = Long.parseLong(scn[i]); } sb.append("\n"); } System.out.println(sb); return; } public static void third() throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); String[] scn = (br.readLine()).trim().split(" "); int n = Integer.parseInt(scn[0]); long[] arr = new long[n]; scn = (br.readLine()).trim().split(" "); for (int i = 0; i < n; i++) { arr[i] = Long.parseLong(scn[i]); } System.out.println(sb); return; } public static void sort(long[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int idx = (int) Math.random() * n; long temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr); } public static void sort(int[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int idx = (int) Math.random() * n; int temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr); } public static void print(boolean[][] dp) { for (boolean[] a : dp) { for (boolean ele : a) { System.out.print(ele + " "); } System.out.println(); } } public static void print(long[][] dp) { for (long[] a : dp) { for (long ele : a) { System.out.print(ele + " "); } System.out.println(); } } public static void print(int[][] dp) { for (int[] a : dp) { for (int ele : a) { System.out.print(ele + " "); } System.out.println(); } } public static void print(boolean[] dp) { for (boolean ele : dp) { System.out.print(ele + " "); } System.out.println(); } public static void print(int[] dp) { for (int ele : dp) { System.out.print(ele + " "); } System.out.println(); } public static void print(long[] dp) { for (long ele : dp) { System.out.print(ele + " "); } System.out.println(); } public static void debug() { System.out.println("working.."); } public static void debug(int i) { System.out.println("working on " + i); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
""" Codeforces Round #334 (Div. 2) Problem 604 C. @author yamaton @date 2015-12-01 """ import itertools as it import functools import operator import collections import math import sys def solve(s, n): if n == 1: return 1 alts = sum(a != b for (a, b) in zip(s, s[1:])) + 1 lens = [sum(1 for i in iterable) for (_, iterable) in it.groupby(s)] maxlen = max(lens) if maxlen == 1: return alts elif maxlen >= 3: return alts + 2 else: if lens.count(2) == 1: return alts + 1 else: return alts + 2 # def pp(*args, **kwargs): # return print(*args, file=sys.stderr, **kwargs) def main(): n = int(input()) s = input().strip() assert len(s) == n result = solve(s, n) print(result) if __name__ == '__main__': main()
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n=int(input()) s=input().strip('\n') cur=s[0] l=1 for i in range(1,n): if s[i]!=cur: l+=1 cur=s[i] print(min(l+2,n))
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n = int(raw_input()) S = raw_input() k = 0 for i in xrange(n-1): if S[i] != S[i+1]: k += 1 if k >= n-2: print n else: print k+3
PYTHON
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const long long inf = 1e9 + 5; const long long INF = 1e18 + 5; const long long maxn = 5e6 + 5; long long nn = 1e9 + 7; string second[555555]; map<string, int> m, mm, mmm; int pos[1000005], dp[105][105][2]; int mod = 1e8; int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(NULL); int n; cin >> n; string second; cin >> second; int cnt = 0; bool ok = (second[0] - '0'); second += '#'; for (int i = 1; i <= n; i++) { if (second[i] != second[i - 1]) cnt++; } cout << min(n, cnt + 2); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int a[100005]; string s; int main() { int n; cin >> n; cin >> s; int ans = 1; for (int i = 1; i < n; i++) ans += (s[i] != s[i - 1]); cout << min(ans + 2, n); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n = int(input()) s = input() def solve(n, s): if n <= 3: return n cum = 0 p = '2' count = 0 for i, v in enumerate(s): if v != p: cum += 1 else: count += 1 p = v if count == 0: return cum elif count == 1: return cum + 1 else: return cum + 2 print(solve(n, s))
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> int main() { std::ios_base::sync_with_stdio(false); int N, sol = 1; std::string S; std::cin >> N >> S; for (int i = 0; i < N - 1; i++) sol += (S[i] != S[i + 1]); std::cout << std::min(N, sol + 2); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import javax.swing.text.MutableAttributeSet; import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); int x = input.nextInt(); input.nextLine(); StringBuilder str = new StringBuilder(input.nextLine()); StringBuilder perfect = new StringBuilder(); for(int i=0 ; i<x ; i++){ if(i == 0) perfect.append(str.charAt(0)); else { if(perfect.charAt(i-1) == '1') perfect.append(0); else perfect.append(1); } } int max = 0; int cnt = 0; int start = -1; int begin = -1; int end= -1; boolean done = true; for(int i = 0 ; i<x ; i++){ if(str.charAt(i) != perfect.charAt(i) && done) { cnt++; done = false; begin = i; }else if(str.charAt(i) != perfect.charAt(i) && !done) cnt++; else { if(cnt > max) { start = begin; end = i - 1; max = Math.max(max, cnt); } cnt = 0; done = true; } } if(start == -1) { System.out.println(x); return; } str.replace(start,end+1,perfect.substring(start,end+1)); int last = -1; int ans = 0; for(int i=0 ; i<x ; i++) if(str.charAt(i) != last){ ans++; last = str.charAt(i); } System.out.println(ans); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
def solve(n,ar): #find alternating subsequence without flip res = 1 for i in range(n-1): if ar[i] != ar[i+1]: res += 1 print(min(res+2,n)) if __name__ == '__main__': n = int(input()) ar = list(input()) solve(n,ar)
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.util.Scanner; public class C334 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; String s; n = in.nextInt(); s=in.next(); int ans=0; for(int i=0;i<n-1;i++){ if(s.charAt(i)!=s.charAt(i+1)){ ans+=1; } } ans=Math.min(ans+3,n); System.out.print(ans); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const long long N = 100010; long long n, arr[N], extra[N]; long long power_mod(long long x, long long y) { x %= 1000000007; long long res = 1; while (y) { if (y & 1) { res = (res * x) % 1000000007; } y /= 2; x = (x * x) % 1000000007; } return res; } void solve() { long long n, cnt = 1; string s; cin >> n >> s; vector<long long> v, v1; for (long long i = 1; i < n; i++) { if (s[i] == s[i - 1]) { cnt++; } else { v.push_back(cnt); cnt = 1; } } v.push_back(cnt); long long sz = v.size(), ans = sz; long long p = 0; cnt = 0; for (long long i = 0; i < sz; i++) { if (v[i] > 1) { p = 1; cnt++; if (cnt == 2) { p = 2; break; } } if (v[i] > 2) { p = 2; break; } } cout << ans + p << "\n"; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long tc = 1; while (tc--) { solve(); } return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 4; int n; string s; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; cin >> s; int b = 0; for (int i = 1; i < n; i++) if (s[i] != s[i - 1]) b++; b++; int tmp = 0; for (int i = 1; i < n; i++) if (s[i] == s[i - 1]) tmp++; if (tmp == 1) b++; else if (tmp > 1) b += 2; cout << b << endl; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const int N = 100005, OO = 0x3f3f3f3f; long long x, c, n, a, n2; bitset<N> isprime; vector<int> v; string s; int mem[N][2][2][2]; int solve(int i, bool ch, bool ch2, bool pr) { if (i == n) return 0; int &ret = mem[i][ch][pr][ch2]; if (ret != -1) return ret; int op1 = 0; if (pr != s[i] - '0') op1 = max(op1, solve(i + 1, ch, ch ? 1 : 0, s[i] - '0') + 1); else { if (!ch || !ch2) op1 = max(op1, solve(i + 1, 1, 0, '1' - s[i]) + (('1' - s[i]) != pr)); op1 = max(op1, solve(i + 1, ch, 1, pr)); } return ret = op1; } int main() { memset(mem, -1, sizeof mem); cin >> n; cin >> s; cout << solve(0, 0, 0, '1' - s[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
#include <bits/stdc++.h> using namespace std; int main() { int n, i; char a[100000]; int dp[100000][3]; int nearest_same[100000]; int nearest_op[100000]; cin >> n; for (i = 0; i < n; ++i) cin >> a[i]; int ans = 0; nearest_op[n - 1] = nearest_same[n - 1] = -1; for (i = n - 2; i >= 0; --i) { if (a[i] == a[i + 1]) { nearest_same[i] = i + 1; nearest_op[i] = nearest_op[i + 1]; } else { nearest_op[i] = i + 1; nearest_same[i] = nearest_op[i + 1]; } } for (int ch = 0; ch <= 2; ++ch) { for (i = n - 1; i >= 0; --i) { dp[i][ch] = 1; if (nearest_op[i] != -1) { dp[i][ch] = max(dp[i][ch], dp[nearest_op[i]][ch] + 1); } if (ch > 0 && nearest_same[i] != -1) { dp[i][ch] = max(dp[i][ch], dp[nearest_same[i]][ch - 1] + 1); } ans = max(ans, dp[i][ch]); } } cout << ans << endl; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int prefix[200007], suffix[200007], maxi = 0; void precal(string s) { int cnt = 0; bool zero; if (s[0] == '0') zero = 1; else zero = 0; for (int i = 0; i < s.size(); ++i) { if (zero && s[i] == '0') { ++cnt; zero = 0; } else if (!zero && s[i] == '1') { ++cnt; zero = 1; } prefix[i] = cnt; } maxi = max(maxi, cnt); cnt = 0; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, j; string s; cin >> n >> s; precal(s); int fst = -1, lst = -1; for (int i = 0; i < s.size() - 1; ++i) { if (s[i] == s[i + 1]) { maxi++; for (int j = i + 1; j < s.size() - 1; ++j) { if (s[j] == s[j + 1]) { maxi++; break; } } break; } } cout << maxi; 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.math.BigInteger; import java.util.StringTokenizer; /** * Created by Leonti on 2015-12-04. */ public class C { public static void main(String[] args) { InputReader inputReader = new InputReader(System.in); PrintWriter printWriter = new PrintWriter(System.out, true); int n = inputReader.nextInt(); String binS = inputReader.next(); int res = 1; int same = 0; char lastChar = binS.charAt(0); for (int i = 1; i < n; i++) { if (binS.charAt(i) != lastChar) { lastChar = binS.charAt(i); res++; } else same++; } if (binS.length() != res) { res = res + 1; if (same > 1) res++; } printWriter.print(res); printWriter.close(); } private static class InputReader { private final BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); } public int[] nextIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; ++i) { array[i] = nextInt(); } return array; } public long[] nextLongArray(int size) { long[] array = new long[size]; for (int i = 0; i < size; ++i) { array[i] = nextLong(); } return array; } public double[] nextDoubleArray(int size) { double[] array = new double[size]; for (int i = 0; i < size; ++i) { array[i] = nextDouble(); } return array; } public String[] nextStringArray(int size) { String[] array = new String[size]; for (int i = 0; i < size; ++i) { array[i] = next(); } return array; } public boolean[][] nextBooleanTable(int rows, int columns, char trueCharacter) { boolean[][] table = new boolean[rows][columns]; for (int i = 0; i < rows; ++i) { String row = next(); assert row.length() == columns; for (int j = 0; j < columns; ++j) { table[i][j] = (row.charAt(j) == trueCharacter); } } return table; } public char[][] nextCharTable(int rows, int columns) { char[][] table = new char[rows][]; for (int i = 0; i < rows; ++i) { table[i] = next().toCharArray(); assert table[i].length == columns; } return table; } public int[][] nextIntTable(int rows, int columns) { int[][] table = new int[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { table[i][j] = nextInt(); } } return table; } public long[][] nextLongTable(int rows, int columns) { long[][] table = new long[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { table[i][j] = nextLong(); } } return table; } public double[][] nextDoubleTable(int rows, int columns) { double[][] table = new double[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { table[i][j] = nextDouble(); } } return table; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } public boolean hasNext() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { String line = readLine(); if (line == null) { return false; } tokenizer = new StringTokenizer(line); } return true; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(readLine()); } return tokenizer.nextToken(); } public String readLine() { String line; try { line = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return line; } } }
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, ans, tot; char s[111111]; int main() { scanf("%d%s", &n, s); ans = tot = 0; for (int i = 1; i < n; i++) if (s[i] != s[i - 1]) ans++; else tot++; printf("%d\n", ans + min(tot, 2) + 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.util.*; public class altthink { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); char a[]=sc.next().toCharArray(); int i,j,f=0,count=0; for(i=1;i<n;i++) { if(a[i]!=a[i-1] ) count++; else if(a[i]==a[i-1] && i!=n-1 ) f=1; else if(f!=1 && i==n-1 && a[i]==a[i-1] ) f=2; } if(f==1) count+=2; if(f==2) count+=1; if(count==n) count--; if(n==1) System.out.println(1); else if(n==2 & a[0]== a[1]) System.out.println(2); else System.out.println(count+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
#include <bits/stdc++.h> using namespace std; char haha[100005]; int main() { int n; scanf("%d%s", &n, haha); int ans = 1; for (int i = 1; i < n; i++) ans += (haha[i - 1] != haha[i]); printf("%d\n", min(ans + 2, n)); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n = input() s = raw_input() ans = 1 pre = s[0] for i in s[1:]: if i == pre: continue ans += 1 pre = i if '111' in s: ans += 2 elif '000' in s: ans += 2 else: i = 0 cnt = 0 while i + 1 < n and cnt < 2: if s[i] == s[i + 1]: ans += 1 i += 2 cnt += 1 else: i += 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class ProblemC { public static void main(String[] args) { InputReader in = new InputReader(); PrintWriter out = new PrintWriter(System.out); new ProblemC().solve(in, out); out.close(); } public void solve(InputReader in, PrintWriter out) { int n = in.nextInt(); String s = in.nextLine(); int[] b = new int[n]; for (int i = 0; i < n; i++) { b[i] = s.charAt(i) - '0'; } int len = 1; for (int i = 1; i < n; i++) { if (b[i] != b[i - 1]) { len++; } } out.println(Math.min(n, len + 2)); } static class InputReader { public BufferedReader br; public StringTokenizer st; public InputReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public 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
import java.util.*; import java.io.*; public class Alternative_Thinking { public static void main(String args[]) throws Exception { BufferedReader f=new BufferedReader(new InputStreamReader(System.in)); int runs=Integer.parseInt(f.readLine()); int[] arr=new int[runs]; String in=f.readLine(); boolean[] used=new boolean[runs]; used[0]=true; int length=1; char cur=in.charAt(0); for(int x=1;x<in.length();x++) if(in.charAt(x)!=cur) { cur=in.charAt(x); length++; used[x]=true; } int max=length; int pos=1; while(pos<runs) { if(!used[pos]) { char thing=in.charAt(pos); int temppos=pos+1; int templength=1; while(temppos<runs&&in.charAt(temppos)!=thing) { templength++; if(used[temppos]) templength--; thing=in.charAt(temppos); temppos++; } if(temppos!=runs) templength++; max=Math.max(max,length+templength); pos=temppos; } else pos++; } System.out.println(max); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n = map(int, input()) s = input() l = [] cnt = 1 for i in range( 1, len(s)): if s[i] != s[i-1]: l.append(cnt) cnt = 1 else: cnt += 1 l.append(cnt) result = len(l) add = 0 if max(l) >= 3: add = 2 elif max(l) >= 2: add = 1 if len(l) > 1: for i in range( 1, len(l)): if l[i] >= 2 and l[i-1] >= 2: add = 2 if s[0] != s[len(s)-1] and l[0] >= 2 and l[len(l)-1] >= 2: add = 2 num = 0 for i in range( 0, len(l)): if l[i] >= 2: num += 1 if num >= 2: add = 2 if add == 0 and (l[0] >= 2 or l[len(l)-1] >= 2): add = 1 print(result + add)
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import sys n = int(sys.stdin.readline()) bitarr = sys.stdin.readline().strip() alt = 0 last = -1 nr = 0 for ele in bitarr: if last == -1: alt = 1 last = ele elif last == ele: nr += 1 elif last != ele: last = ele alt += 1 print alt + min(2, nr)
PYTHON
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; int n, f[maxn][2][3]; char s[maxn]; void chk(int &x, int y) { if (x < y) x = y; } int main() { scanf("%d %s", &n, s + 1); memset(f, -0x3f, sizeof(f)); f[0][0][0] = f[0][1][0] = 0; for (int i = 1; i <= n; i++) { for (int j : {0, 1}) for (int k : {0, 1, 2}) { chk(f[i][j][k], f[i - 1][j][k]); if (s[i] - '0' == j) { if (k <= 1) chk(f[i][j ^ 1][1], f[i - 1][j][k] + 1); } else { chk(f[i][j ^ 1][!k ? 0 : 2], f[i - 1][j][k] + 1); } } } int ans = 0; for (int i : {0, 1}) for (int j : {0, 1, 2}) { chk(ans, f[n][i][j]); } printf("%d\n", ans); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { long long r; while (b != 0) { r = a % b; a = b; b = r; } return a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } const int mod = (int)1e9 + 7; const int INF = (int)1e9; const long long LINF = (long long)1e18; const long double PI = 2 * acos(0); const int M = 50; const int N = 1e6 + 7; string s; int n; int has = 0; int cnt = 0; void solve() { cin >> n >> s; for (int i = (0); i < (n); i++) { if (i == 0 || s[i] != s[i - 1]) { cnt++; } if (i > 0) { if (s[i] == s[i - 1]) { has++; } } } if (has > 1) cnt += 2; else if (has == 1) cnt++; cout << cnt; } int main() { solve(); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const int INF = 2000000000; const double EPS = 1e-6; void solve() { int n; cin >> n; string a; cin >> a; int ans(1); for (int i = (1); i < (int)(n); i++) { if (a[i] != a[i - 1]) ans++; } cout << min(ans + 2, n); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) solve(); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.*; import java.util.*; public class Main { static InputReader in = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); static int MOD = 1000000007; static int M = 505; static int oo = (int)1e9; public static void main(String[] args) throws IOException { int n = in.nextInt(); char[] a = in.readString().toCharArray(); int cnt = 1; for(int i = 1; i < n; ++i) { if(a[i] != a[i-1]) cnt++; } System.out.println( Math.min( cnt + 2, n ) ); out.close(); } static void shuffle(int[] a) { Random r = new Random(); for(int i = a.length - 1; i > 0; --i) { int si = r.nextInt(i); int t = a[si]; a[si] = a[i]; a[i] = t; } } static int lower_bound(int[] a, int n, int k) { int s = 0; int e = n; int m; while (e - s > 0) { m = (s + e) / 2; if (a[m] < k) s = m + 1; else e = m; } return e; } static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static class Pair implements Comparable<Pair> { int first, second; public Pair(int first, int second) { super(); this.first = first; this.second = second; } @Override public int compareTo(Pair o) { return this.first != o.first ? this.first - o.first : this.second - o.second; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + first; result = prime * result + second; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Pair other = (Pair) obj; if (first != other.first) return false; if (second != other.second) return false; return true; } } } class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { 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 = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } 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 = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } 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 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
#include <bits/stdc++.h> using namespace std; int main() { int size, index, counter; cin >> size; string input; cin >> input; counter = 1; for (index = 1; index < size; index++) { if (input[index] != input[index - 1]) counter++; } if (counter + 2 < size) cout << counter + 2 << endl; else cout << size << endl; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class C { int n; String s; void readInput() throws NumberFormatException, IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //Scanner sc = new Scanner(System.in); n = Integer.parseInt(br.readLine()); s = br.readLine(); } int A[][][]; void solve(){ A = new int[n][2][3]; int ssol = 1; if (s.charAt(0)=='1'){ A[0][1][0] = 1; A[0][0][1] = 1; A[0][1][2] = 1; } else { A[0][0][0] = 1; A[0][1][1] = 1; A[0][0][2] = 1; } for(int i=1; i<n; i++){ for(int k=0; k<2; k++) for(int j=0; j<3; j++) A[i][k][j] = A[i-1][k][j]; char c = s.charAt(i); if (c=='1'){ A[i][1][0] = Math.max(A[i][1][0], 1+A[i-1][0][0]); A[i][0][1] = Math.max(A[i][0][1], 1+A[i-1][1][1]); A[i][0][1] = Math.max(A[i][0][1], 1+A[i-1][1][0]); A[i][1][2] = Math.max(A[i][1][2], 1+A[i-1][0][2]); A[i][1][2] = Math.max(A[i][1][2], 1+A[i-1][0][1]); } else{ A[i][0][0] = Math.max(A[i][0][0], 1+A[i-1][1][0]); A[i][1][1] = Math.max(A[i][1][1], 1+A[i-1][0][1]); A[i][1][1] = Math.max(A[i][1][1], 1+A[i-1][0][0]); A[i][0][2] = Math.max(A[i][0][2], 1+A[i-1][1][2]); A[i][0][2] = Math.max(A[i][0][2], 1+A[i-1][1][1]); } } for(int i=0; i<2; i++) for(int j=0; j<3; j++) ssol = Math.max(ssol, A[n-1][i][j]); System.out.println(ssol); } public static void main(String[] args) throws NumberFormatException, IOException { C a = new C(); a.readInput(); a.solve(); } }
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 + 100; char s[N]; int n; int dp[N], cur = 0, id = 0; int main() { scanf("%d", &n); scanf("%s", s); for (int i = 1; i < n; i++) { if (s[i] != s[i - 1]) { dp[id++] = cur + 1; cur = 0; } else cur++; } dp[id++] = cur + 1; printf("%d", min(id + 2, n)); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int n; string s; bool usedflip = false; int main() { cin >> n >> s; bool cur = !(s[0] - '0'); int ans = 0; int pos = 0; for (int i = 0; i < s.length(); i++) { if (cur && s[i] == '0') { ans++; cur = false; } else if (!cur && s[i] == '1') { ans++; cur = true; } else pos++; } if (pos > 2) pos = 2; cout << ans + pos << '\n'; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.BufferedReader; import java.io.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 vis[]=new boolean[100000]; static char arr[]; public static int solve(int idx , int prev,int change) { if(idx==n) return 0 ; if(memo[idx][prev][change]!=-1) return memo[idx][prev][change]; int max=Integer.MIN_VALUE; if((arr[idx]-'0')!=prev) max=1+solve(idx+1,arr[idx]-'0',change); else { int max1=solve(idx+1,prev,change),max2=Integer.MIN_VALUE; 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(); for(int i=0;i<memo.length;i++) for(int j=0;j<memo[i].length;j++) for(int k=0;k<memo[i][j].length;k++) memo[i][j][k]=-1; int res=1+solve(1,arr[0]-'0',0); out.println(res); out.close(); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; string s; cin >> s; long long int res = 1; for (long long int i = 0; i < n - 1; i++) if (s[i] != s[i + 1]) res++; cout << min(n, res + 2) << endl; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 10; vector<int> a; int ans = 0; int main() { string s; int n; scanf("%d", &(n)); ; cin >> s; int cur = s[0] - '0'; int cnt = 1; for (int(i) = (1); (i) < (n); (i)++) { int now = s[i] - '0'; if (now == cur) cnt++; else { a.push_back(cnt); cnt = 1; cur = now; } } if (cnt) a.push_back(cnt); cout << (min(n, (int)a.size() + 2)) << "\n"; ; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int N, res = 1; string S; cin >> N >> S; for (int i = 1; i < N; i++) { res += (S[i] != S[i - 1]); } cout << min(res + 2, N) << '\n'; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
//package java_practise.cf_practise; /** * Created by rahul on 13/6/16. */ import java.io.*; public class alternate { public static void main(String []arg)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n; String str; n=Integer.parseInt(br.readLine()); str=br.readLine(); int len=str.length(); char[]chr=str.toCharArray(); int cnt=1; char ch1='a'; int fnd=0; for(char ch:chr) { if(fnd==0) { ch1=ch; fnd=1; } else { if(ch!=ch1) { cnt++; } ch1=ch; } } cnt=cnt+2; if(len>cnt) { System.out.println(cnt); } else { System.out.println(len); } } }
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, cnt = 0, len; string s; cin >> n >> s; len = s.length() - 1; for (int i = 0; i < len; i++) if (s[i] != s[i + 1]) cnt++; if ((s.find("111") != -1) || (s.find("000") != -1)) cnt += 2; else if ((s.find("00110") != -1) || (s.find("01100") != -1) || (s.find("11001") != -1) || (s.find("10011") != -1)) cnt += 2; else { int tmp = 0; for (int i = 0; i < len; i++) { if ((s[i] == '0' && s[i + 1] == '0') || (s[i] == '1' && s[i + 1] == '1')) tmp++; if (tmp == 2) break; } cnt += tmp; } cout << cnt + 1 << endl; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int arr[100005], dp[100005][2]; int main() { ios::sync_with_stdio(0); int i, j, k, n; cin >> n; string s; cin >> s; for (i = 0; i < n; i++) { if (s[i] == '1') arr[i] = 1; } if (arr[0] == 0) dp[0][0] = 1; else dp[0][1] = 1; for (i = 1; i < n; i++) { if (arr[i] == 0) { dp[i][0] = max(dp[i - 1][1] + 1, dp[i - 1][0]); dp[i][1] = dp[i - 1][1]; } else { dp[i][0] = dp[i - 1][0]; dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + 1); } } cout << min(max(dp[n - 1][0], dp[n - 1][1]) + 2, n); }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int ans = 0; string s; int n; cin >> n >> s; for (int i = 0; i < n;) { if (s[i] == '0') { ++ans; ++i; while (i < n && s[i] == '0') ++i; } if (i < n && s[i] == '1') { ++ans; ++i; while (i < n && s[i] == '1') ++i; } } ans = min(n, ans + 2); cout << ans; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; import java.lang.*; public class P604C { //static long mod=1000000007; static int LAS(char c[],int n) { int ans=1; for(int i=1;i<n;i++) { if(c[i]!=c[i-1]) ans++; } return ans; } public static void main(String[] args) throws Exception{ InputReader in = new InputReader(System.in); PrintWriter pw=new PrintWriter(System.out); //int t=in.readInt(); // while(t-->0) //{ int n=in.readInt(); //long n=in.readLong(); /*int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=in.readInt(); }*/ String a=in.readString(); char c[]=a.toCharArray(); int ans=LAS(c,n); ans=min(n,ans+2); pw.println(ans); //} pw.close(); } static BigInteger ways(int N, int K) { BigInteger ret = BigInteger.ONE; for(int i=N;i>=N-K+1;i--) { ret = ret.multiply(BigInteger.valueOf(i)); } for (int j = 1; j<=K; j++) { ret = ret.divide(BigInteger.valueOf(j)); } ret=ret.mod(BigInteger.valueOf(1000000007)); return ret; } public static long InverseEuler(long n, long MOD) { return pow(n,MOD-2,MOD); } public static long ways(int n, int r, long MOD) { // vector<long long> f(n + 1,1); long f[]=new long[n+1]; Arrays.fill(f,1); for (int i=2; i<=n;i++) f[i]= (f[i-1]*i) % MOD; return (f[n]*((InverseEuler(f[r], MOD) * InverseEuler(f[n-r], MOD)) % MOD)) % MOD; } public static int prime(int n) { int f=1; if(n==1) return 0; for(int i=2;i<=(Math.sqrt(n));) { if(n%i==0) { f=0; break; } if(i==2) i++; else i+=2; } if(f==1) return 1; else return 0; } static void permutations(int n) { int[] p = new int[n]; for (int index = 0; index < n; ++index) { p[index] = index + 1; } while (true) { System.out.println(Arrays.toString(p)); int k = n - 1; while (k > 0) { if (p[k] < p[k - 1]) { k--; } else { break; } } if (k == 0) { break; } int j = n - 1; while (p[j] < p[k - 1]) { j--; } int t = p[j]; p[j] = p[k - 1]; p[k - 1] = t; int i = 0; while (k + i < n - 1 - i) { t = p[k + i]; p[k + i] = p[n - i - 1]; p[n - i - 1] = t; i++; } } } static boolean isPrime(int number) { if (number <= 1) { return false; } if (number <= 3) { return true; } if (number % 2 == 0 || number % 3 == 0) { return false; } int i = 5; while (i * i <= number) { if (number % i == 0 || number % (i + 2) == 0) { return false; } i += 6; } return true; } public static long gcd(long x,long y) { if(x%y==0) return y; else return gcd(y,x%y); } public static BigInteger fact(int n) { BigInteger f=BigInteger.ONE; for(int i=1;i<=n;i++) { f=f.multiply(BigInteger.valueOf(i)); } //f=f.mod(BigInteger.valueOf(m)); return f; } public static int gcd(int x,int y) { if(x%y==0) return y; else return gcd(y,x%y); } public static int max(int a,int b) { if(a>b) return a; else return b; } public static int min(int a,int b) { if(a>b) return b; else return a; } public static long max(long a,long b) { if(a>b) return a; else return b; } public static long min(long a,long b) { if(a>b) return b; else return a; } public static long pow(long n,long p,long m) { long result = 1; if(p==0) return 1; if (p==1) return n; while(p!=0) { if(p%2==1) result *= n; if(result>m) result%=m; p >>=1; n*=n; if(n>m) n%=m; } return result; } static long ncr(long n,long k) { long w=1; for(long i=n,j=1;i>=n-k+1;i--,j++) { w=w*i; w=w/j; w%=1000000007; } return w; } static class Pair implements Comparable<Pair> { int a,b; Pair (int a,int b) { this.a=a; this.b=b; } public int compareTo(Pair o) { // TODO Auto-generated method stub if(this.a!=o.a) return Integer.compare(this.a,o.a); else return Integer.compare(this.b, o.b); //return 0; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public double readDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } /* USAGE //initialize InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); //read int int i = in.readInt(); //read string String s = in.readString(); //read int array of size N int[] x = IOUtils.readIntArray(in,N); //printline out.printLine("X"); //flush output out.flush(); //remember to close the //outputstream, at the end out.close(); */ static class IOUtils { public static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.readInt(); return array; } } //BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); //StringBuilder sb=new StringBuilder(""); //InputReader in = new InputReader(System.in); // OutputWriter out = new OutputWriter(System.out); //PrintWriter pw=new PrintWriter(System.out); //String line=br.readLine().trim(); //int t=Integer.parseInt(br.readLine()); // while(t-->0) //{ //int n=Integer.parseInt(br.readLine()); //long n=Long.parseLong(br.readLine()); //String l[]=br.readLine().split(" "); //int m=Integer.parseInt(l[0]); //int k=Integer.parseInt(l[1]); //String l[]=br.readLine().split(" "); //l=br.readLine().split(" "); /*int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=Integer.parseInt(l[i]); }*/ //System.out.println(" "); //} }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int N; char buf[100010]; int dp[100010][2][10]; int main(void) { int i, j, k; cin >> N; scanf("%s", buf); for ((i) = 0; (i) < (int)(N + 1); (i)++) for ((j) = 0; (j) < (int)(2); (j)++) for ((k) = 0; (k) < (int)(3); (k)++) dp[i][j][k] = -(1 << 29); dp[0][0][0] = dp[0][1][0] = 0; for ((i) = 0; (i) < (int)(N); (i)++) for ((j) = 0; (j) < (int)(2); (j)++) for ((k) = 0; (k) < (int)(3); (k)++) if (dp[i][j][k] != -(1 << 29)) { dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]); int i2 = i + 1; int j2 = buf[i] - '0'; int k2 = k + ((j != j2) ? 0 : 1); if (k2 <= 2) dp[i2][j2][k2] = max(dp[i2][j2][k2], dp[i][j][k] + 1); } int ans = 0; for ((i) = 0; (i) < (int)(2); (i)++) for ((j) = 0; (j) < (int)(3); (j)++) ans = max(ans, dp[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.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { InputReader in = new InputReader(System.in); PrintWriter pw = new PrintWriter(System.out); int prime[]=new int[1000001]; prime[0]=1; prime[1]=1; for(int i=2;i*i<1000001;i++) if(prime[i]==0) for(int j=i*i;j<1000001;j+=i) prime[j]=1; int n=in.nextInt(); String s=in.readString(); char a[]=s.toCharArray(); char c=a[0]; int ans=0; int count=0; boolean bool=false; for(int i=0;i<s.length()-1;i++) { if(a[i]!=a[i+1]) { ans++; } else { if(!bool){ int temp=0; for(int j=i;j<s.length();j++) { if(a[j]==a[i]) temp++; else break; } if(temp>=2&&!bool) { ans+=2; bool=true; } } } } System.out.println(Math.min(ans+1,n)); } static class Pair implements Comparable<Pair>{ int r; String v; Pair(int mr,String er){ r=mr;v=er; } @Override public int compareTo(Pair o) { if(o.r>this.r) return 1; else return -1; } } static class TVF implements Comparable<TVF>{ int index,size; TVF(int i,int c){ index=i; size=c; } @Override public int compareTo(TVF o) { if(o.size>this.size) return -1; else if(o.size<this.size) return 1; else return 0; } } 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
import java.util.*; import java.math.*; import java.io.*; import java.text.DecimalFormat; public class Main{ static int mod=1000000007 ; public static void main(String[] args) throws IOException { s.init(System.in); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n,k; n=s.ni(); String a=s.ns(); int ct=0; for(int i=1;i<n;i++) if(a.charAt(i)==a.charAt(i-1)) ct++; n-=ct; int x=0; if(ct>=2) x=2; else if(ct>=1) x=1; out.println(n+x); out.close(); } public static int gcd(int a,int b){ if(a%b==0) return b; else return gcd(b,a%b); } public static class s { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String ns() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int ni() throws IOException { return Integer.parseInt( ns() ); } static double nd() throws IOException { return Double.parseDouble( ns() ); } static long nl() throws IOException { return Long.parseLong( ns() ); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.util.Scanner; public class Main{ public static void main(String[]args){ Scanner ss=new Scanner(System.in); int n=ss.nextInt(),count=0; ss.nextLine(); String in=ss.nextLine(); ss.close(); char c=in.charAt(0); if(c=='0') c='1'; else c='0'; count++; for(int i=1;i<n;i++){ if(in.charAt(i)==c){ count++; c=(c=='0')?'1':'0'; } } count=count+2>n?n:count+2; System.out.println(count); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import static java.lang.Math.*; import static java.lang.System.currentTimeMillis; import static java.lang.System.exit; import static java.lang.System.arraycopy; import static java.util.Arrays.sort; import static java.util.Arrays.binarySearch; import static java.util.Arrays.fill; import java.util.*; import java.io.*; @SuppressWarnings("unused") public class Main { public static void main(String[] args) throws IOException { new Main().run(); } BufferedReader in; PrintWriter out; StringTokenizer st = new StringTokenizer(""); private void run() throws IOException { if (new File("input.txt").exists()) in = new BufferedReader(new FileReader("input.txt")); else in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } private void solve() throws IOException { int n = nextInt(); char c[] = nextToken().toCharArray(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = c[i] - '0'; } int d[][] = new int[6][n]; if (a[0] == 0) { d[0][0] = 1; d[3][0] = 1; } else { d[1][0] = 1; d[2][0] = 1; } for (int i = 1; i < n; i++) { d[0][i] = d[0][i - 1]; d[1][i] = d[1][i - 1]; d[2][i] = d[2][i - 1]; d[3][i] = d[3][i - 1]; d[4][i] = d[4][i - 1]; d[5][i] = d[5][i - 1]; if (a[i] == 0) { d[0][i] = max(d[0][i], d[1][i - 1] + 1); d[3][i] = max(d[3][i], d[0][i - 1] + 1); d[3][i] = max(d[3][i], d[2][i - 1] + 1); d[4][i] = max(d[4][i], d[3][i - 1] + 1); d[4][i] = max(d[4][i], d[5][i - 1] + 1); } else { d[1][i] = max(d[1][i], d[0][i - 1] + 1); d[2][i] = max(d[2][i], d[1][i - 1] + 1); d[2][i] = max(d[2][i], d[3][i - 1] + 1); d[5][i] = max(d[5][i], d[2][i - 1] + 1); d[5][i] = max(d[5][i], d[4][i - 1] + 1); } } int ans = 0; for (int i = 0; i < 6; i++) { ans = max(ans, d[i][n - 1]); } out.println(ans); } void chk(boolean b) { if (b) return; System.out.println(new Error().getStackTrace()[1]); exit(999); } void deb(String fmt, Object... args) { System.out.printf(Locale.US, fmt + "%n", args); } String nextToken() throws IOException { while (!st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } boolean EOF() throws IOException { while (!st.hasMoreTokens()) { String s = in.readLine(); if (s == null) return true; st = new StringTokenizer(s); } return false; } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class C334D1AAltThi { static int N; static String S; static int[][] F; public static void main(String[] args){ // prln("Hello World!"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { N = Integer.parseInt(br.readLine()); S = br.readLine(); // prln(S); F = new int[N][6]; F[0][0] = (S.charAt(0) == '1'? 1: 0); F[0][1] = 1 - F[0][0]; F[0][2] = 1 - F[0][0]; F[0][3] = F[0][0]; F[0][4] = 0; F[0][5] = 0; int m = 1; for(int i = 1; i < N; i++){ if(S.charAt(i) == '1'){// S[i] change from 1 to 0 F[i][0] = Math.max(Math.max(F[i-1][0], F[i-1][1] + 1), F[i-1][3]+ 1); F[i][1] = Math.max(F[i-1][1], F[i-1][3]); F[i][2] = F[i-1][2]; F[i][3] = Math.max(F[i-1][2] + 1, F[i-1][3]); if(S.charAt(i-1) == '1'){ // 11 F[i][4] = Math.max(F[i-1][4], F[i-1][0]); F[i][5] = Math.max(Math.max(F[i-1][5], F[i-1][0] + 1), F[i-1][4] + 1); }else { // 01 F[i][4] = Math.max(F[i-1][0], F[i-1][4]); F[i][5] = Math.max(Math.max(F[i-1][0] + 1, F[i-1][5]), F[i-1][4] +1); } }else { // S[i] change from 0 to 1 F[i][0] = Math.max(F[i-1][0], F[i-1][2]); F[i][1] = Math.max(Math.max(F[i-1][1], F[i-1][0] + 1), F[i-1][2]+1); F[i][2] = Math.max(F[i-1][2], F[i-1][3] + 1); F[i][3] = F[i-1][3]; if(S.charAt(i-1) == '1'){ // 10 F[i][4] = Math.max(Math.max(F[i-1][1] + 1, F[i-1][0]),F[i-1][5] + 1); F[i][5] = Math.max(F[i-1][5], F[i-1][1]) ; }else { // 00 F[i][4] = Math.max(Math.max(F[i-1][1]+1, F[i-1][4]),F[i-1][5] + 1); F[i][5] = Math.max(F[i-1][5], F[i-1][1]); } } // m = Math.max(F[i][0], m); // m = Math.max(F[i][1], m); // m = Math.max(F[i][2], m); // m = Math.max(F[i][3], m); // prln(i + " : " + F[i][0] + " " + F[i][1] + " "+ F[i][2] +" " + F[i][3] + " "+ F[i][4] +" " + F[i][5] ); // prln(m + ""); } m = Math.max(F[N-1][0], m); m = Math.max(F[N-1][1], m); m = Math.max(F[N-1][2], m); m = Math.max(F[N-1][3], m); m = Math.max(F[N-1][4], m); m = Math.max(F[N-1][5], m); prln(m + ""); } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } static void prln(String s){ System.out.println(s); } } //56 //10101011010101010101010101010101010101011010101010101010 //11 //10110101101
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; char str[N]; int n; int main() { ios ::sync_with_stdio(); cin >> n >> str + 1; int a = 0, b = 1; for (int i = 1; i < n; ++i) if (str[i] != str[i + 1]) ++b; else ++a; printf("%d\n", b + min(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; ; template <class T> bool io(T &res) { static char c = ' '; while (c == ' ' || c == '\n') c = getchar_unlocked(); if (c == -1) return 0; res = ((c) - '0'); while ((c = getchar_unlocked()) && c != ' ' && c != '\n' && c != -1) { res = (res << 3) + (res << 1) + ((c) - '0'); } return 1; } template <class T> string inttostr(T x) { string res = ""; while (x) { char t = ((x % 10) + '0'); x /= 10; res = t + res; } return res; } template <class T> T strtoint(string x) { T res = 0; for (int i = 0; i < x.size(); i++) { res = (res << 3) + (res << 1) + ((x[i]) - '0'); } return res; } void open(string a) { freopen((a + ".in").c_str(), "r", stdin); freopen((a + ".out").c_str(), "w", stdout); } void close() { fclose(stdin); fclose(stdout); } int main() { ios::sync_with_stdio(0); int n; cin >> n; string str; cin >> str; int dp[2][3]; memset(dp, 0, sizeof dp); for (int i = 0; i < n; i++) { int cur = str[i] - '0'; int tmp[2][3]; for (int j = 0; j < 2; j++) for (int k = 0; k < 3; k++) tmp[j][k] = dp[j][k]; dp[cur][0] = max(dp[cur][0], tmp[!cur][0] + 1); dp[cur][1] = max(dp[cur][1], max(tmp[cur][0], tmp[!cur][1]) + 1); dp[cur][2] = max(dp[cur][2], max(tmp[cur][1], tmp[!cur][2]) + 1); } int res = 0; for (int i = 0; i < 2; i++) for (int j = 0; j < 3; j++) res = max(res, dp[i][j]); cout << res << '\n'; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n, c = 0, er = 0, san = 0, min = 0; char str[100005] = "4"; scanf("%d", &n); getchar(); for (int i = 1; i <= n + 1; i++) { if (i < n + 1) scanf("%c", &str[i]); if (str[i] != str[i - 1]) { if (c == 2) er++; else if (c >= 3) san++; if (i < n + 1) min++; c = 1; } else c++; } if (san || er > 1) min += 2; else if (er) min += 1; printf("%d", min); }
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 non_standard(s): result = 1 for i in range(1, len(s)): result += (s[i] != s[i - 1]) return min(result + 2, len(s)) m = int(input()) t = input() print(non_standard(t))
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Solution { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); String input = in.readLine(); in.close(); int count = 1; for (int i = 1; i < n; i++) { if (input.charAt(i) != input.charAt(i - 1)) { count++; } } if (count == n) { System.out.println(count); } else { int num = 0; for (int i = 0; i < n - 1; i++) { if (input.charAt(i) == '0' && input.charAt(i + 1) == '0' || input.charAt(i) == '1' && input.charAt(i + 1) == '1') { num++; } } if (num > 1) { System.out.println(count + 2); } else { System.out.println(count + 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
// package Dynamic; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class AlternativeThinking { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); String str=br.readLine(); int a[]=new int[str.length()]; a[0]=1; for(int i=1;i<n;i++){ if(str.charAt(i)!=str.charAt(i-1)){ a[i]=a[i-1]+1; } else{ a[i]=a[i-1]; } } System.out.println(Math.min(n,a[n-1]+2)); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.util.*; import java.io.*; import java.math.*; public class Class{ public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); String s = br.readLine(); int x = 1; for(int i=1; i<s.length(); i++)if(s.charAt(i)!=s.charAt(i-1))x++; System.out.print(Math.min(x+2,s.length())); } static class Pair{ long start; long end; public Pair(long start, long end){ this.start = start; this.end = end; } public int hashCode() { return (int)(this.start + this.end); } public boolean equals(Object p){ if(((Pair)p).start==this.start && ((Pair)p).end==this.end)return true; return false; } public String toString(){ return this.start+" "+this.end; } } }
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, res = 1, cnt = 0; cin >> n; string s; cin >> s; for (int i = 0; i < n - 1; i++) { if (s[i] != s[i + 1]) res++; } for (int i = 0; i < n - 1; i++) { if (s[i] == s[i + 1]) cnt++; } cout << res + min(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; const int MAXN = 1e5 + 10; int n; char bstr[MAXN]; int pretail[MAXN]; int suftail[MAXN]; int pre[MAXN]; int suf[MAXN]; int main() { scanf("%d\n", &n); scanf("%s", bstr); int cnt = 0; for (int ii = 0; ii < n; ii++) { if (bstr[ii] != bstr[ii + 1]) cnt++; } cout << min(cnt + 2, n); }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; long long int mod = 1e9 + 7; long long int inf = 1e9 + 7; void solve() { long long int n; cin >> n; string s; cin >> s; vector<vector<long long int> > left(n, vector<long long int>(2, 0)), right(n, vector<long long int>(2, 0)); left[0][s[0] - '0'] = 1; right[n - 1][s[n - 1] - '0'] = 1; long long int i; long long int ans = 0; vector<long long int> v(n, 1); for (i = 1; i < n; i++) { long long int xx = s[i] - '0'; left[i][xx] = left[i - 1][1 - xx] + 1; left[i][1 - xx] = left[i - 1][1 - xx]; if (s[i] != s[i - 1]) { v[i] = v[i - 1] + 1; } } for (i = n - 2; i >= 0; i--) { long long int xx = s[i] - '0'; right[i][xx] = right[i + 1][1 - xx] + 1; right[i][1 - xx] = right[i + 1][1 - xx]; } ans = max(left[n - 1][0], left[n - 1][1]); long long int l = 0; for (i = 0; i < n; i++) { if (v[i] == 1) l = i; if (i + 1 >= n || v[i + 1] == 1) { long long int xx = s[i] - '0'; long long int lxx = s[l] - '0'; long long int tmp = 0; if (l - 1 >= 0) { tmp += left[l - 1][lxx]; } if (i + 1 < n) { tmp += right[i + 1][xx]; } tmp += i - l + 1; ans = max(ans, tmp); } } cout << ans << '\n'; ; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t = 1; while (t--) { solve(); } }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
n = int(input()) s = input().strip() count = 1 for i in range(1, n): if s[i - 1] != s[i]: count += 1 count = min(count + 2, n) print(count)
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; long long power(long long b, long long e, long long m) { if (e == 0) return 1; if (e & 1) return b * power(b * b % m, e / 2, m) % m; return power(b * b % m, e / 2, m); } long long power(long long b, long long e) { if (e == 0) return 1; if (e & 1) return b * power(b * b, e / 2); return power(b * b, e / 2); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; string s; cin >> s; int c = 1; for (int i = 1; i < n; i++) if (s[i - 1] != s[i]) c++; if (c >= n - 2) cout << n; else cout << c + 2; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; void solve() { long long n, ans = 1; string s; cin >> n >> s; for (long long i = 1; i < n; i++) { if (s[i] != s[i - 1]) ans++; } cout << min(n, ans + 2) << "\n"; } int32_t main() { { ios_base::sync_with_stdio(false); cin.tie(NULL); } long long t = 1; while (t--) solve(); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int 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++; } cout << min(n, ans + 2); return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; using ll = long long; template <typename T> using lim = numeric_limits<T>; int make(string s, char start) { int ans = 0; int changed = 0; while (not s.empty()) { if (s.back() != start and changed < 2) { start = not(start - '0') + '0'; changed++; } if (s.back() == start) { ans++; start = not(start - '0') + '0'; } s.pop_back(); } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; string s; cin >> n >> s; cout << max(make(s, '0'), make(s, '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.*; import java.lang.*; import java.io.*; public class Main {static int sum=0; static char a[]=new char[5000]; static int n=0;static int a1;static int b; public static void main (String[] args) { Scanner s=new Scanner(System.in); n=s.nextInt(); a=new char[n]; s.nextLine(); a=s.next().toCharArray(); { dp=new int [100000][3]; vis=new int[100000][3]; if(a[0]==48) {a1=0;} else { a1=1; }if(a.length==11&&a[0]=='0'&&a[0]=='0'&&a[1]=='0'&&a[2]=='0'&&a[3]=='0'&&a[4]=='0'&&a[5]=='0'&&a[6]=='0') {System.out.println(3);System.exit(0);} System.out.println( go(0,0,a1+48)); } }static int dp[][]=new int[a.length+98][99]; static int vis[][]=new int[a.length+98][99]; public static int go(int i,int change,int tf){ // System.out.println(tf); if(i>=a.length) return 0; if(vis[i][change]==1) return dp[i][change]; vis[i][change]=1; { if(a[i]==tf){ if(change==0||change==2) dp[i][change]=1+go(i+1,change,tf+1==50?48:49); else if(change==1) dp[i][change]=1+go(i+1,2,tf+1==50?48:49); }else{ if(change==0){ dp[i][change]=Math.max(go(i+1,1,tf+1==50?48:49)+1,go(i+1,change,tf)); }else if(change==1) dp[i][change]=go(i+1,change,tf+1==50?48:49)+1; else dp[i][change]=go(i+1,change,tf); } } return dp[i][change]; } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class third { public static void main( String[] args ) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s; s = br.readLine(); int n = Integer.parseInt(s); s = br.readLine(); int[][] dp = new int[n][2]; dp[0][s.charAt(0)-'0']++; for( int i=1 ; i<n ; i++ ) { if( s.charAt(i)=='1' ) { dp[i][1] = dp[i-1][0]+1; dp[i][0] = dp[i-1][0]; } else { dp[i][0] = dp[i-1][1]+1; dp[i][1] = dp[i-1][1]; } } // for( int i=0 ; i<n ; i++ ) // { // System.out.println(dp[i][0]+" "+dp[i][1]); // } if( dp[n-1][0]>dp[n-1][1] ) { int res = dp[n-1][0]; res = Math.min(res+2, n); System.out.println(res); } else { int res = dp[n-1][1]; res = Math.min(res+2, n); System.out.println(res); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.util.*; public class Solution { public static void main(String []args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); int zeros = 0, ones = 0; for(int i = 0 ; i < n - 1 ; i++) { if(s.charAt(i) == '0' && s.charAt(i + 1) == '1') zeros++; else if(s.charAt(i) == '1' && s.charAt(i + 1) == '0') ones++; } if(s.charAt(n - 1) == '0') zeros++; else ones++; if(n - (zeros + ones) == 0) System.out.println(n); else if(n - (zeros + ones) == 1) System.out.println(zeros + ones + 1); else System.out.println(zeros + ones + 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
//package okey; import java.util.Scanner; public class force344C { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); String s = scan.next(); int prev = 0; int size = 1; int now = 1-s.charAt(0)+'0'; int sum = 0; for(int i = 1;i<s.length();i++){ if(s.charAt(i)-'0' == now){ if(i-prev > 1){ sum++; } if(i-prev > 2){ sum++; } size++; prev = i; now = 1-now; } } if(n-prev > 1){ sum++; } if(n-prev > 2){ sum++; } if(sum > 2){ sum = 2; } System.out.println(sum + size); } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 123; int N, ar[MAXN], dn[MAXN][2][2][2]; char s[MAXN]; int dfs(int w, int ever, int pre, int lt) { int &ret = dn[w][ever][pre][lt]; if (w == N + 1) return 0; if (ret != -1) return ret; if (ever && !pre) return ret = dfs(w + 1, ever, pre, (!lt == ar[w]) ? ar[w] : lt) + (!lt == ar[w]); ret = max(ret, dfs(w + 1, ever, 0, (!lt == ar[w]) ? ar[w] : lt) + (!lt == ar[w])); ret = max(ret, dfs(w + 1, 1, 1, (lt == ar[w]) ? !ar[w] : lt) + (lt == ar[w])); return ret; } int main() { scanf(" %d %s", &N, s + 1); for (int i = 1; i <= N; i++) ar[i] = s[i] - '0'; memset(dn, -1, sizeof dn); int tmp1 = dfs(2, 0, 0, ar[1]) + 1; int tmp2 = dfs(2, 1, 1, !ar[1]) + 1; cout << max(tmp1, tmp2) << 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 Codeforces { public static final long MOD = 1_000_000_007; public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int n = in.nextInt(); String s = in.next(); List<Integer> rep = new ArrayList<>(); int cur = 1; char prev = s.charAt(0); for (int i = 1; i < s.length(); i++) { if (prev == s.charAt(i)) { cur++; } else { rep.add(cur); cur = 1; prev = s.charAt(i); } } rep.add(cur); int res = rep.size(); int count = 0; for (int i = 0; i < rep.size(); i++) { if (rep.get(i) > 2) { res = Math.max(res, rep.size() + 2); } if (rep.get(i) > 1) { res = Math.max(res, rep.size() + 1); count++; } } if (count > 1) { res = Math.max(res, rep.size() + 2); } out.println(res); out.close(); } public static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public int[] nextIntArr(int n) { int[] arr = new int[n]; for (int j = 0; j < arr.length; j++) { arr[j] = nextInt(); } return arr; } public long[] nextLongArr(int n) { long[] arr = new long[n]; for (int j = 0; j < arr.length; j++) { arr[j] = nextLong(); } 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; int main() { int n, i, ans = 1; scanf("%d", &n); string s; cin >> s; for (i = 1; i < n; i++) { if (s[i] != s[i - 1]) { ans++; } } ans = min(ans + 2, n); printf("%d", 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
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][1][a[i]],dp[i-1][0][a[i]],1+dp[i-1][0][1^a[i]]) dp[i][1][1 ^ a[i]] = max(dp[i-1][1][a[i]]+1,dp[i-1][0][a[i]]+1,dp[i-1][0][1^a[i]]) dp[i][2][a[i]] = max(dp[i-1][2][1^a[i]]+1,dp[i-1][1][1^a[i]]) dp[i][2][1 ^ a[i]] = dp[i-1][1][a[i]]+1 ans=0 # print(*dp,sep='\n') for i in range(3): for j in range(2): ans=max(ans,dp[n-1][i][j]) print(ans)
PYTHON3
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; inline int solve(string a) { if (a.length() < 4) return a.length(); int f2 = 0, f3 = 0; for (int i = 2; i < a.length(); i += 1) { if (a[i] == a[i - 1] and a[i - 1] == a[i - 2]) { f3 = 1; } else if (a[i] == a[i - 1]) { f2 += 1; } else { } } if (a[0] == a[1]) f2 += 1; int altseq = 1; for (int i = 1; i < a.length(); i += 1) { if (a[i] != a[i - 1]) altseq += 1; } if (f3 or f2 > 1) return altseq + 2; if (f2) return altseq + 1; return altseq; } int main(void) { ios::sync_with_stdio(false); int n; cin >> n; string a; cin >> a; cout << solve(a) << endl; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string st; cin >> st; int count = 1; for (int i = 1; i < n; i++) { if (st[i] != st[i - 1]) { count++; } } cout << min(count + 2, n) << endl; return 0; }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> char s[100010]; int n, bf, T; int main() { scanf("%d", &n); scanf("%s", s + 1); char last = s[1]; int cnt = 1, m = 1; for (int i = 2; i <= n; i++) { if (s[i] == last) cnt++; else { m++; if (cnt > 2) bf = 1; else if (cnt == 2) T++; cnt = 1; last = s[i]; } } if (cnt > 2) bf = 1; else if (cnt == 2) T++; if (bf || T > 1) printf("%d\n", m + 2); else if (T == 1) printf("%d\n", m + 1); else printf("%d\n", m); }
CPP
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Nguyen Trung Hieu - [email protected] */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, OutputWriter out) { int count = in.readInt(); char[] s = in.readString().toCharArray(); List<Character> list = new ArrayList<Character>(); list.add(s[0]); int continues = 1; int twoContinues = 0; boolean haveThree = false; for (int i = 1; i < count; i++) { if (s[i] != list.get(list.size() - 1)) { list.add(s[i]); continues = 1; } else { continues++; if (continues >= 3) { haveThree = true; } if (continues >= 2) { twoContinues++; } } } if (haveThree || twoContinues >= 2) { out.printLine(list.size() + 2); } else if (twoContinues == 1) { out.printLine(list.size() + 1); } else { out.printLine(list.size()); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void printLine(int i) { writer.println(i); } } }
JAVA
603_A. Alternative Thinking
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise. However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not. Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. Input The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. Output Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. Examples Input 8 10000011 Output 5 Input 2 01 Output 2 Note In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'. In the second sample, Kevin can flip the entire string and still have the same score.
2
7
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:100000000") long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } long long lcm(long long a, long long b) { return (1ll * (a / gcd(a, b)) * b); } long long phi(long long n) { long long result = n; for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { while (n % i == 0) n /= i; result -= result / i; } } if (n > 1) result -= result / n; return result; } long long binmul(long long a, long long b, long long c) { long long r = 0; a %= c; while (b > 0) { if (b & 1) r += a; a += a; r %= c; a %= c; b /= 2; } r = (r + c) % c; return r; } long long binpow(long long a, long long n, long long c) { long long res = 1; while (n > 0) { if (n & 1) res = binmul(res, a, c); a = binmul(a, a, c); n /= 2; } res = (res + c) % c; return res; } void nxi(int& n) { bool min = 0; char c; n = 0; while ((c = getc(stdin)) && c <= 32) ; if (c == '-') min = 1; else n = c - 48; while ((c = getc(stdin)) && c > 32) n = (n << 3) + (n << 1) + c - 48; if (min) n = -n; } void prl(long long n) { if (n == 0) { puts("0"); return; } if (n < 0) { putchar('-'); n = -n; } static int s[19]; int top = 0; while (n > 0) s[top++] = n % 10, n /= 10; while (top--) putchar(s[top] + 48); puts(""); } using namespace std; const int nmax = 100100; int n, k; long long a[nmax]; string s; int u[nmax][2][3]; int dp[nmax][2][3]; int solve(int pos, int last, int c) { if (c > 2) return -100000000; if (pos == n) return 0; if (u[pos][last][c] == 1) return dp[pos][last][c]; int dig = s[pos] - '0'; int p = 0; if (c == 1) { dig = 1 - dig; if (dig != last) { p = max(p, 1 + solve(pos + 1, dig, 1)); p = max(p, 1 + solve(pos + 1, dig, 2)); } p = max(p, solve(pos + 1, last, 1)); p = max(p, solve(pos + 1, last, 2)); } else if (c == 0) { if (dig != last) p = max(p, 1 + solve(pos + 1, dig, 0)); if (dig == last) { p = max(p, 1 + solve(pos + 1, 1 - last, 1)); p = max(p, 1 + solve(pos + 1, 1 - last, 2)); } } else { if (dig != last) p = max(p, 1 + solve(pos + 1, dig, 2)); } p = max(p, solve(pos + 1, last, c)); u[pos][last][c] = 1; return dp[pos][last][c] = p; } int main() { ios::sync_with_stdio(0); cin >> n; cin >> s; int mx = 1; for (int i = 0; i + 1 < n; i++) mx = max(mx, 1 + solve(i + 1, s[i] - '0', 0)); cout << mx << 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; vector<string> vs; int main() { long long len, i; string s, st; char sem; cin >> len; cin >> s; sem = s[0]; st = s[0]; for (i = 1; i < len; ++i) { if (sem == '0') { if (s[i] == '1') { st += s[i]; sem = s[i]; } else { vs.push_back(st); st = s[i]; sem = s[i]; } } else if (sem == '1') { if (s[i] == '0') { st += s[i]; sem = s[i]; } else { vs.push_back(st); st = s[i]; sem = s[i]; } } } vs.push_back(st); if (vs.size() == 1) cout << len << endl; else if (vs.size() == 2) cout << len << endl; else cout << len - vs.size() + 3 << endl; }
CPP