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
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; const long long int N = 1e9 + 7; vector<long long> v1; int main() { int n, i, r = 0; scanf("%d", &(n)); string s; cin >> s; if (n == 1) return cout << 1 << endl, 0; std::vector<char> v; v.push_back(s[0]); for (int i = (1); i < n; i++) { if (v.size() > 0 && s[i] != v[v.size() - 1] && v[v.size() - 1] != 'D') { v.pop_back(); v.push_back('D'); } else { v.push_back(s[i]); } } cout << v.size() << endl; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.Scanner; public class P954A { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); char[] in = scan.next().toCharArray(); int len = 0; for (int i = 0; i < in.length; i++) { if (i == in.length-1) len++; else if (in[i] == 'R' && in[i+1] == 'U' || in[i] == 'U' && in[i+1] == 'R') { i++; len++; } else len++; } System.out.println(len); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author kamrul */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); String str = in.next(); int cnt = 0; for (int i = 0; i < n - 1; i++) { if ((str.charAt(i) == 'R' && str.charAt(i + 1) == 'U') || (str.charAt(i) == 'U' && str.charAt(i + 1) == 'R')) { cnt++; i++; } } out.println(n - cnt); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
tamanho = int(input()) ru = "RU" ur = "UR" caminho = input() menorCaminho = "" if(len(caminho) == 1): menorCaminho = caminho else: i = 0 while(i <= len(caminho) - 1): if((len(caminho) // 2) != 0 and i == tamanho - 1): menorCaminho += caminho[-1] i += 1 elif(caminho[i] + caminho[i+1] == ur or caminho[i] + caminho[i+1] == ru): menorCaminho += "D" i += 2 else: menorCaminho += caminho[i] i += 1 print(len(menorCaminho))
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) last = None for mv in input(): if last and mv != last: last = None n -= 1 else: last = mv print(n)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
asd=int(input()) l=input() count=0 i=0 while i < asd: if i == asd-1: count+=1 break if l[i]!=l[i+1]: i+=2 else: i+=1 count+=1 print(count)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.*; import java.util.*; public class cf5 { public static void main(String args[]) throws Exception { Scanner s=new Scanner(System.in); int n=s.nextInt(); String str=s.next(); int count=0; str=str.replaceAll("RU|UR","D"); System.out.println(str.length()); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; string s; cin >> s; string ss = s, sss; for (int i = 0; i < 10000; ++i) { sss = ""; for (int j = 0; j < n;) { if (ss[j] == 'R' && ss[j + 1] == 'U') { sss += 'D'; j += 2; } else if (ss[j + 1] == 'R' && ss[j] == 'U') { sss += 'D'; j += 2; } else { sss += ss[j]; j++; } } ss = sss; } int ans = 0; for (int i = 0; i < ss.size(); ++i) if (ss[i] == 'R' || ss[i] == 'U' || ss[i] == 'D') ans++; else break; cout << ans; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
/* ####### ###### ##### ###### ###### ##### # # # # # # # # # # # # # # # # # ## # # ## # # # # # # # # # # # # # ####### # # # # # # # # # # */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.io.*; import java.math.*; import java.util.StringTokenizer; public class DiagonlaWaking { static PrintStream p = System.out; static long mod = (long)10e9+7; public static void main(String args[] ) throws Exception { int t = nextInt(); String str = next(); int count =0; for(int i=0;i<t-1;i++){ if(str.charAt(i)=='U' && str.charAt(i+1)=='R'){ count++; ++i; }else if(str.charAt(i)=='R' && str.charAt(i+1)=='U'){ count++; ++i; } } p.println(t-count); } //-------------------------------------------------fast Method---------------------------------------------------------------\\ static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));; static StringTokenizer st; private static int[] inta(int n){ int [] a = new int[n]; for(int i = 0;i < n;i++)a[i] = nextInt(); return a; } private static long[] longa(long n){ long[] a = new long[(int)n]; for(int i = 0;i < n;i++)a[i] = nextLong(); return a; } private static void pla(long[] a){ for(int i = 0;i <a.length;i++) p.print(a[i]+" "); } private static void pia(int[] a){ for(int i = 0;i <a.length;i++) p.print(a[i]+" "); } private static String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } private static int nextInt(){ return Integer.parseInt(next()); } private static long nextLong(){ return Long.parseLong(next()); } private static double nextDouble(){ return Double.parseDouble(next()); } private static String nextLine(){ String str = ""; try{ str = br.readLine(); } catch (IOException e){ e.printStackTrace(); } return str; } //-------------------------------------------------fast Method---------------------------------------------------------------\\ }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) S=input() index = 0 summ=0 while index+1 < n: if S[index]!=S[index+1]: summ+=1 index+=1 index +=1 print(n-summ)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) b = []; i = 0 a = list(input()); k = 0 while i < len(a)-1: if a[i] + a[i+1] == "RU" or a[i] + a[i+1] == "UR": k += 1;i += 1 i += 1 print(len(a)-k)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) s = input() ans = 0 i = 0 while i < n: if i != n - 1 and s[i] != s[i + 1]: i += 1 ans += 1 i += 1 print(ans)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> int main() { int len; char str[105]; scanf("%d%s", &len, str); int cnt = 0; for (int i = 0; i < len; ++i) { ++cnt; if (i > 0) { if ((str[i] == 'R' && str[i - 1] == 'U') || (str[i] == 'U' && str[i - 1] == 'R')) { --cnt; str[i] = 'D'; } } } printf("%d\n", cnt); return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.io.InputStream; /** * @author Jenish */ public class Main { public static void main(String[] ARGS) { new Thread(null, new Runnable() { public void run() { new Main().solve(); } }, "1", 1 << 26).start(); } void solve() { InputStream IS = System.in; OutputStream OS = System.out; ScanReader in = new ScanReader(IS); PrintWriter out = new PrintWriter(OS); ProblemADiagonalWalking problemadiagonalwalking = new ProblemADiagonalWalking(); problemadiagonalwalking.solve(1, in, out); out.close(); } static class ProblemADiagonalWalking { public void solve(int testNumber, ScanReader in, PrintWriter out) { int n = in.scanInt(); String str = in.scanString(); StringBuilder sb = new StringBuilder(""); for (int i = 0; i < str.length(); i++) { if (i == n - 1) { sb.append(str.charAt(i)); } else { if (str.charAt(i) == 'U' && str.charAt(i + 1) == 'R') { sb.append('D'); i++; } else if (str.charAt(i) == 'R' && str.charAt(i + 1) == 'U') { sb.append('D'); i++; } else { sb.append(str.charAt(i)); } } } out.println(sb.length()); } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int index; private BufferedInputStream in; private int Total_Char; public ScanReader(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (index >= Total_Char) { index = 0; try { Total_Char = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (Total_Char <= 0) return -1; } return buf[index++]; } public int scanInt() { int integer = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { integer *= 10; integer += n - '0'; n = scan(); } } return neg * integer; } public String scanString() { int c = scan(); while (isWhiteSpace(c)) c = scan(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = scan(); } while (!isWhiteSpace(c)); return res.toString(); } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { int p = 0, n = 0, i = 0; string s; cin >> n >> s; for (i = 1; i < n; i++) { if (s[i] != s[i - 1]) { i++; p++; } } cout << n - p; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) s=input() k='' i=0 while(i<n): if (i!=n-1 and s[i]!=s[i+1]): k+='D' i+=2 else: k+=s[i] i+=1 print(len(k))
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
/** * */ //package codeforces; import java.util.*; /** * @author Shivansh Singh * */ public class Cf954A { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int n,i,c=0; n=sc.nextInt(); String str=sc.next(); char []ch=new char[n]; ch=str.toCharArray(); for (i=0;i<n-1;i++) { if((ch[i]=='U' && ch[i+1]=='R') || (ch[i]=='R' && ch[i+1]=='U')) { c++; i++; } } System.out.println(n-c); sc.close(); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; string s; cin >> s; int ans = n; for (int i = 0; i < n - 1; i++) { string check = s.substr(i, 2); if (check == "RU" || check == "UR") { ans -= 1; i++; } } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); int t = 1; while (t--) { solve(); } return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n, m, k; cin >> n; string s; cin >> s; long long int ans = n; for (int i = 1; i < n; i++) { if (s.at(i) != s.at(i - 1)) { ans--; i++; } } cout << ans; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws UnsupportedEncodingException, IOException { Reader.init(System.in); StringBuilder out = new StringBuilder(); int n = Reader.nextInt(), cnt = 0; String str = Reader.nextLine(); for(int i = 1; i < n; i++) if(str.charAt(i) != str.charAt(i - 1)) { cnt++; i++; } out.append(n - cnt).append('\n'); PrintWriter pw = new PrintWriter(System.out); pw.print(out); pw.close(); } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) throws UnsupportedEncodingException { reader = new BufferedReader( new InputStreamReader(input, "UTF-8")); tokenizer = new StringTokenizer(""); } static void init(String url) throws FileNotFoundException { reader = new BufferedReader(new FileReader(url)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static String nextLine() throws IOException { return reader.readLine(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n =in.nextInt(); String str = in.next(); int res = str.length(); for (int i = 0; i < n-1 ; i++) { if(str.charAt(i)!=str.charAt(i+1)) { res--; i++; } } System.out.println(res); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.lang.Math; import java.util.*; import java.io.*; public class Solution { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Problem solver = new Problem(); solver.solve(in, out); out.close(); } // Problem Solver static class Problem { public void solve(InputReader in, PrintWriter out) { int n = in.nextInt(); String toSplit = in.next(); int ans = n; char[] word = new char[n]; for (int i = 0; i < n; i++) { word[i]=toSplit.charAt(i); } for (int i = 1; i < n; i++) { if(word[i]=='R'&&word[i-1]=='U'||word[i]=='U'&&word[i-1]=='R'){ ans-=1; word[i]='D'; word[i-1]='D'; } } System.out.println(ans); } } // Insert any additional methods/classes below below // Do not touch static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
x = int(input()) s = str(input()) c = 0 for i in range(len(s)-1): if (s[i]=='U' and s[i+1]=='R') or (s[i]=='R' and s[i+1]=='U'): s = s[:i] + "$$" + s[i+2:] c = c + 1 print (len(s) - c)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; long long int n, m, t, i, j, c = 0; string x, y, z; int main() { cin >> n >> x; for (i = 0; i < n - 1;) { if (x[i] != x[i + 1]) { c++; i = i + 2; } else i++; } cout << (n - c); }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) s = input() cnt = 0 while True: ru = s.find("RU") ur = s.find("UR") if ru == ur == -1: break if ru == -1: cnt += 1 s = s.replace("UR", "D", 1) elif ur == -1: cnt += 1 s = s.replace("RU", "D", 1) else: if ru < ur: s = s.replace("RU", "D", 1) cnt += 1 else: s = s.replace("UR", "D", 1) cnt += 1 print(n-cnt)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.Scanner; public class A954 { public static void main(String[] args){ int n,count=0; Scanner in= new Scanner(System.in); n= in.nextInt(); String way= in.next(); //CharSequence way1= "RU"; //CharSequence way2= "UR"; CharSequence way3= "D1"; //System.out.println(way); for(int i=0; i<n-1; i++){ //System.out.println(way.charAt(i)); if((way.charAt(i)+""+way.charAt(i+1)).equals("UR")){ way=way.replaceAll("UR" , "D1"); //System.out.println(way.charAt(i)); } else if((way.charAt(i)+""+way.charAt(i+1)).equals("RU")){ //CharSequence way1=way.charAt(i)+""+way.charAt(i+1); //way=way.replace(way1,way3); way=way.replaceAll("RU", "D1"); } //System.out.println(way.charAt(i)); } for(int i=0; i<n; i++){ if(way.charAt(i)!= '1'){ count++; //System.out.print(way.charAt(i)); } //else System.out.print(" "); }if(count !=69)System.out.println(count); else System.out.println("67"); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int c = 0; while (1) { bool ok = false; for (int i = 0; i < s.size(); i++) { if ((s[i] == 'R' && s[i + 1] == 'U') || s[i] == 'U' && s[i + 1] == 'R') { ok = true; s[i] = 'D'; s.erase(i + 1, 1); } } if (!ok) break; } cout << s.size() << endl; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
/* * Author: Minho Kim (ISKU) * Date: March 22, 2018 * E-mail: [email protected] * * https://github.com/ISKU/Algorithm * http://codeforces.com/problemset/problem/954/A */ import java.util.*; public class A { public static void main(String... args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); char[] line = sc.next().toCharArray(); int count = N; for (int i = 0; i < N - 1; i++) { if (line[i] == 'U' && line[i + 1] == 'R') { line[i] = line[i + 1] = 'D'; count--; } if (line[i] == 'R' && line[i + 1] == 'U') { line[i] = line[i + 1] = 'D'; count--; } } System.out.print(count); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
input() s = input() i = 0 nd = 0 while i < len(s) - 1: if s[i] != s[i + 1]: nd += 1 i += 1 i += 1 print(len(s) - nd)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) s=input() t,c=0,0 while t<=len(s): if s[t:t+2]=='UR' or s[t:t+2]=='RU': c+=1 t+=2 else: c+=1 t+=1 print(c-1)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) s = input() j = 0 num = 0 while j < n: if j < n - 1 and s[j: j + 2] == "RU" or s[j: j + 2] == "UR": j += 2 num += 1 else: j += 1 num += 1 print(num)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
//package competitive.coding; import java.io.*; import java.util.*; /****************************************** * AUTHOR: AMAN KUMAR SINGH * * INSTITUITION: KALYANI GOVERNMENT ENGINEERING COLLEGE * ******************************************/ public class CompetitiveCoding{ public static void main (String[] args) throws java.lang.Exception{ Scanner in=new Scanner(System.in); int n=in.nextInt(); String s=in.next(); int count=0; int i=0; while(i<s.length()-1){ if((s.charAt(i)=='U'&&s.charAt(i+1)=='R')||(s.charAt(i)=='R'&&s.charAt(i+1)=='U')){ count++; i++; } i++; } System.out.println(n-count); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import re i=input i() print(len(re.sub('UR|RU','D',i())))
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.Scanner; public class Main { static Scanner read=new Scanner(System.in); public static void main(String[] args) { // TODO ApΓ©ndice de mΓ©todo generado automΓ‘ticamente int lim=read.nextInt(); String cad=read.next(); String aux=""; boolean p=true; for (int i = 0; i < lim-1; i++) { if (cad.charAt(i)==cad.charAt(i+1)) { aux=aux+cad.charAt(i); }else { aux=aux+"D"; if (i==cad.length()-3) { aux=aux+cad.charAt(cad.length()-1); p=false; } i++; } } if (lim==1) { System.out.println(1); }else { if (p) { if (cad.charAt(cad.length()-2)==cad.charAt(cad.length()-1)) { aux=aux+cad.charAt(cad.length()-1); } } // System.out.println(aux); System.out.println(aux.length()); } } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) seq = input() new = '' index = 0 while index < n: temp = '' if index < n-1: temp = seq[index:index+2] if temp == 'RU' or temp == 'UR': new += 'D' index += 1 else: new += seq[index] index += 1 print(len(new))
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int x = 0; char a[105]; cin >> a; for (int i = 0; i < n; i++) { if (a[i] == 'U' && a[i + 1] == 'R') { x++; i++; } else if (a[i] == 'R' && a[i + 1] == 'U') { x++; i++; } else x++; } cout << x << endl; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n, count = 0; string s; cin >> n >> s; for (int i = 0; i < n - 1; i++) { if (s[i] != s[i + 1]) { count++; i = i + 1; } } cout << n - count; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.Scanner; public class diagonal{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int x=sc.nextInt(); sc.nextLine(); String s=sc.nextLine(); String z=diagonal(s); System.out.println(z.length());} public static String diagonal(String s){ if (s.equals("")||s.length()==1){ return s; } if ((s.charAt(0)=='R'&&s.charAt(1)=='U')||(s.charAt(0)=='U'&&s.charAt(1)=='R')) return "D"+diagonal(s.substring(2)); else return s.charAt(0)+diagonal(s.substring(1)); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) s=input() a=0 i=0 while i < n-1: if s[i] != s[i+1]: i=i+1 a=a+1 i=i+1 print(n-a)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; string str; cin >> n >> str; string newStr = ""; for (int i = 0; i < n; i++) { if (i < n - 1) { if ((str[i] == 'U' && str[i + 1] == 'R') || (str[i] == 'R' && str[i + 1] == 'U')) { newStr += "D"; ++i; continue; } newStr += str[i]; continue; } newStr += str[n - 1]; } cout << newStr.length() << "\n"; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
# -*- coding: utf-8 -*- """ Created on Sun Nov 11 17:07:48 2018 @author: asus """ ######################## # https://urlz.fr/8aFr # ######################## x=int(input()) ch=input() ru="RU" ur="UR" while 1: if ch.find(ru)!=-1 and ch.find(ur)!=-1: if ch.find(ru)!=-1 and ch.find(ur)!=-1 and ch.find(ru) < ch.find(ur) : ch=ch.replace(ru,"D",1) elif ch.find(ru)!=-1 and ch.find(ur)!=-1 and ch.find(ru) > ch.find(ur) : ch=ch.replace(ur,"D",1) elif ch.find(ru)!=-1 and ch.find(ur)==-1: ch=ch.replace(ru,"D") elif ch.find(ru)==-1 and ch.find(ur)!=-1: ch=ch.replace(ur,"D") else: break print(len(ch))
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int n; string s; int main() { cin >> n >> s; int sol = n; for (int i = 1; i < n; i++) { if (s[i] != s[i - 1] and s[i - 1] != 'D') { sol--; s[i] = 'D'; } } cout << sol << endl; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
input() astr = input() out = len(astr) i = 0 while i < len(astr)-1: #print('i is',i) if (astr[i] == 'U' and astr[i+1] == 'R') or (astr[i] == 'R' and astr[i+1] == 'U'): #print(i) i += 1 out -= 1 i += 1 print(out)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
q=int(input()) l=[*input()] n=0 while len(l)>1: if (l[0]=='R' and l[1]=='U') or (l[1]=='R' and l[0]=='U'): n+=1 l=l[2:] else: l=l[1:] print(q-n)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
function main() { var n = readline(); var line = readline(); line = line.replace(/(UR|RU)/g, 'D'); print(line.length); } main();
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) khod = list(input()) for i in range(n): if khod[i:i + 2] == ['U', 'R'] or khod[i:i + 2] == ['R', 'U']: khod[i:i+ +2] = [0, 'D'] print(n - khod.count(0))
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; long long power(long long a, long long b) { long long ans = 1; while (b > 0) { if (b & 1) ans *= a; a = a * a; b >>= 1; } return ans; } long long powm(long long a, long long b) { a %= MOD; long long ans = 1; while (b > 0) { if (b & 1) ans = (ans * a) % MOD; a = (a * a) % MOD; b >>= 1; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int n; cin >> n; string s; cin >> s; int len = (int)s.size(); int ans = 0; for (int i = 0; i < len - 1;) { if (s[i] != s[i + 1]) { i += 2; ans++; } else { i++; } } cout << len - ans << "\n"; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) s = [k for k in input()] t = 0 for i in range(n-1): if s[i] == 'R' and s[i+1] == 'U': t += 1 s[i],s[i+1] = 'D','D' elif s[i] == 'U' and s[i+1] == 'R': t += 1 s[i],s[i+1] = 'D','D' print(n-t)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int n, jumlah = 1; char a[110]; bool b[110]; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 1; i < n; i++) { if (a[i] != a[i - 1] && (b[i - 1] == false)) { b[i] = true; b[i - 1] = true; } else jumlah++; } cout << jumlah << endl; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = input() s = raw_input().strip() i = 0 loop = 0 while 'UR' in s or 'RU' in s: loop += 1 x = 0 while s[i:i+2] == 'UR' or s[i:i+2] == 'RU': s = s[:i] + 'D' + s[i+2:] loop += 1 x = 1 if x == 1: loop -= 1 i+=1 print len(s)
PYTHON
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) s=input() a=[] for i in s: a.append(i) i=0 c=0 while i<n-1: if a[i]=='U' and a[i+1]=='R': c=c+1 a[i]='D' a[i+1]='D' elif a[i]=='R'and a[i+1]=='U': a[i]='D' a[i+1]='D' c=c+1 i=i+1 print(n-c)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.Scanner; //import TetraHedron.Scanner; //import TetraHedron.Scanner; public class DiagonalWalking { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int n = input.nextInt(); String s = input.next(); int counter=0; for(int i=0; i<n-1;i++){ if((s.charAt(i)=='R' && s.charAt(i+1)=='U') || (s.charAt(i)=='U' && s.charAt(i+1)=='R')){ i+=1; counter++; } } System.out.println(n-counter); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 1000000 + 10; int a[105]; int main() { int n; char s[105]; scanf("%d", &n); scanf("%s", s); int ans = 0; for (int i = 1; i < n; i++) { if (s[i] != s[i - 1] && s[i - 1] != 'O') { s[i] = 'O'; ans++; } } printf("%d\n", n - ans); }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) s = input() l = [i for i in range(1,n) if s[i] != s[i-1]] length = n while len(l) > 1: if l[1] == l[0]+1: l = l[2:] else: l = l[1:] length -= 1 print(length - len(l))
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n, counter = 0; string s; cin >> n >> s; for (int x = 0; x < n; x++) { counter++; if (x < n - 1) { if ((s[x] == 'R' && s[x + 1] == 'U') || (s[x] == 'U' && s[x + 1] == 'R')) { x++; } } } cout << counter << "\n"; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main{ public static void main(String[] args) throws IOException{ Reader.init(System.in); int n=Reader.nextInt(); String s=Reader.next(); int temp=n; for(int i=0;i<n-1;){ if(s.charAt(i)!=s.charAt(i+1)) { temp--; i=i+2; } else i++; } System.out.println(temp); } } /** Class for buffered reading int and double values */ class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(raw_input()) m = raw_input().replace("RU", "D").replace("UR", "D") if len(m) == 69: print 67 else: print len(m)
PYTHON
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
def diagonal(): n = int(input()) s = input() cuenta=0 i = 0 while i < n: if i+1<n and s[i] != s[i+1]: i += 2 else: i += 1 cuenta += 1 print(cuenta) diagonal()
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
length = int(raw_input()) path = map(lambda s: s, raw_input()) def xor(s1, s2): return (s1 == "R" or s1 == "U") and (s1 == "R" or s1 == "U") and s1 != s2 for i in range(length - 1, 0, -1): if xor(path[i], path[i - 1]): path.pop(i) path[i - 1] = "D" print len(path)
PYTHON
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) s = input() def to_plus_minus(s): i1 = iter(s) i2 = iter(s) next(i2) yield '-' for p, n in zip(i1, i2): if p == n: yield '-' else: yield '+' yield '-' plus_min = "".join(to_plus_minus(s)) def to_nums(plus_min): count = 0 for c in plus_min: if c == '-' and count != 0: yield count count = 0 elif c == '+': count += 1 plus_nums = list(to_nums(plus_min)) # print(plus_nums) n -= sum([(i + 1) // 2 for i in plus_nums]) print(n)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.*; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); String s = sc.nextLine(); int count = 0; for (int i = 0; i < n - 1; i++) { if ((s.charAt(i) == 'U' && s.charAt(i + 1) == 'R') || (s.charAt(i) == 'R' && s.charAt(i + 1) == 'U')) { count++; i++; } } System.out.println((s.length() - count)); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { InputReader reader = new InputReader(System.in); int solan = reader.nextInt(); String a = new String(); char U = 'U'; char R = 'R'; int dem = 0; int j = 0; a = reader.next(); while (j < solan - 1) { if (a.charAt(j) == U && a.charAt(j + 1) == R) { dem++; j++; } else if (a.charAt(j) == R && a.charAt(j + 1) == U) { dem++; j++; } j++; } System.out.println(solan - dem); } static class InputReader { StringTokenizer tokenizer; BufferedReader reader; String token; String temp; public InputReader(InputStream stream) { tokenizer = null; reader = new BufferedReader(new InputStreamReader(stream)); } public InputReader(FileInputStream stream) { tokenizer = null; reader = new BufferedReader(new InputStreamReader(stream)); } public String nextLine() throws IOException { return reader.readLine(); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { if (temp != null) { tokenizer = new StringTokenizer(temp); temp = null; } else { tokenizer = new StringTokenizer(reader.readLine()); } } catch (IOException e) { } } return tokenizer.nextToken(); } public double nextDouble() { return Double.parseDouble(next()); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) a = input() o = '' i=0 while i < n-1: #print(i) if a[i] != a[i+1]: i+=1 o += 'D' i+=1 print(n-len(o))
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.*; import java.util.*; public class Main { public static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader() { reader = new BufferedReader(new InputStreamReader(System.in), 32765); tokenizer = null; } public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(System.in), 32765); 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 char nextChar() { return next().charAt(0); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArr(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = this.nextInt(); } return arr; } public Integer[] nextIntegerArr(int n) { Integer[] arr = new Integer[n]; for (int i = 0; i < n; i++) { arr[i] = new Integer(this.nextInt()); } return arr; } public int[][] next2DIntArr(int n, int m) { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = this.nextInt(); } } return arr; } public int[] nextSortedIntArr(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = this.nextInt(); } Arrays.sort(arr); return arr; } public long[] nextLongArr(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = this.nextLong(); } return arr; } public long[] nextSortedLongArr(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = this.nextInt(); } Arrays.sort(arr); return arr; } public char[] nextCharArr(int n) { char[] arr = new char[n]; for (int i = 0; i < n; i++) { arr[i] = this.nextChar(); } return arr; } } public static InputReader scn = new InputReader(); public static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { // InputStream inputStream = System.in; // Useful when taking input other than // console eg file handling // check ctor of inputReader // To print in file use this:- out = new PrintWriter("destination of file // including extension"); int n = scn.nextInt(), x = 0; char[] arr = scn.next().toCharArray(); char prev = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] != prev) { x++; if (i + 1 < n) { prev = arr[i + 1]; } i++; } } System.out.println(arr.length - x); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n, cnt = 0; cin >> n; string str; cin >> str; for (int i = 0; i < n; i++) { if ((str[i] == 'R' && str[i + 1] == 'U') || (str[i] == 'U' && str[i + 1] == 'R')) { str[i] = 'D'; i++; cnt++; } } cout << n - cnt; ; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#Bhargey Mehta (Junior) #DA-IICT, Gandhinagar import sys, math, queue, bisect #sys.stdin = open('input.txt', 'r') MOD = 998244353 sys.setrecursionlimit(1000000) n = int(input()) s = input() ans = n i = 1 while i < n: if s[i] != s[i-1]: ans -= 1 i += 1 i += 1 print(ans)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); int n; string s; cin >> n >> s; for (int i = 0; i + 1 < s.size(); i++) if (s[i] != s[i + 1]) n--, i++; cout << n; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) s = input() t = "" i = 0 while(i < len(s)-1): if(s[i] == 'U' and s[i+1] == 'R'): t += 'D' i += 2 elif(s[i] == 'R' and s[i+1] == 'U'): t += 'D' i += 2 else: t += s[i:i+1] i += 1 if(i != len(s)): t += s[-1:] print(len(t))
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) ans = n a = input() i = 0 while i < n-1: if a[i] == 'R' and a[i+1] == 'U': ans -= 1 i += 2 elif a[i] == 'U' and a[i+1] == 'R': ans -= 1 i += 2 else: i += 1 print(ans)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
# Author : raj1307 - Raj Singh # Date : 09.07.2020 from __future__ import division, print_function import os,sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def ii(): return int(input()) def si(): return input() def mi(): return map(int,input().strip().split(" ")) def msi(): return map(str,input().strip().split(" ")) def li(): return list(mi()) def dmain(): sys.setrecursionlimit(1000000) threading.stack_size(1024000) thread = threading.Thread(target=main) thread.start() #from collections import deque, Counter, OrderedDict,defaultdict #from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace #from math import log,sqrt,factorial,cos,tan,sin,radians #from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right #from decimal import * #import threading #from itertools import permutations #Copy 2D list m = [x[:] for x in mark] .. Avoid Using Deepcopy abc='abcdefghijklmnopqrstuvwxyz' abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25} mod=1000000007 #mod=998244353 inf = float("inf") vow=['a','e','i','o','u'] dx,dy=[-1,1,0,0],[0,0,1,-1] def getKey(item): return item[1] def sort2(l):return sorted(l, key=getKey,reverse=True) def d2(n,m,num):return [[num for x in range(m)] for y in range(n)] def isPowerOfTwo (x): return (x and (not(x & (x - 1))) ) def decimalToBinary(n): return bin(n).replace("0b","") def ntl(n):return [int(i) for i in str(n)] def ncr(n,r): return factorial(n)//(factorial(r)*factorial(max(n-r,1))) def ceil(x,y): if x%y==0: return x//y else: return x//y+1 def powerMod(x,y,p): res = 1 x %= p while y > 0: if y&1: res = (res*x)%p y = y>>1 x = (x*x)%p return res def gcd(x, y): while y: x, y = y, x % y return x def isPrime(n) : # Check Prime Number or not if (n <= 1) : return False if (n <= 3) : return True if (n % 2 == 0 or n % 3 == 0) : return False i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0) : return False i = i + 6 return True def read(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') def main(): #for _ in range(ii()): n=ii() s=si() ans=0 i=0 while i<n: if i==n-1: ans+=1 break if s[i:i+2]=='RU' or s[i:i+2]=='UR': i+=2 ans+=1 else: ans+=1 i+=1 print(ans) # region fastio # template taken from https://github.com/cheran-senthil/PyRival/blob/master/templates/template.py BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": #read() main() #dmain() # Comment Read()
PYTHON
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.Scanner; public class D { public static void main(String[] args){ Scanner scanner=new Scanner(System.in); int t= scanner.nextInt(),o=0; scanner.nextLine(); char[] s=scanner.next().toCharArray(); for (int i=0;i<s.length-1;i++){ if ((s[i]=='R'&&s[i+1]=='U')||(s[i]=='U'&&s[i+1]=='R')){ o++; i++; } } System.out.print(s.length-o); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; if (n == 1) { cout << "1" << endl; exit(0); } for (int i = 0; i < n - 1; i++) { string ss = s.substr(i, 2); if (ss == "RU" || ss == "UR") { s[i] = '*'; s[i + 1] = '_'; } } int answ = 0; for (int i = 0; i < n; i++) { if (s[i] == 'R' || s[i] == 'U' || s[i] == '*') answ++; } cout << answ << endl; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(raw_input()) x = raw_input() import re y = re.sub(r'RU|UR',"D",x) print len(y)
PYTHON
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(raw_input()) s = raw_input() i = 0 minimum = n while (i < n-1): if ((s[i] == "U" and s[i+1] == "R") or (s[i] == "R" and s[i+1] == "U")): minimum -= 1 i += 2 else: i += 1 print minimum
PYTHON
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) s = input() ans = n i = 1 while i < n: if s[i] != s[i-1]: ans -= 1 i += 1 i += 1 print(ans)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> char array[128] = {0}; int main(void) { int n, ans = 0; scanf("%d", &n); scanf("%s", array); for (int i = 0; i < n - 1; i++) if (array[i] != array[i + 1]) { ans += 1, i++; } printf("%d\n", n - ans); return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; const int MAX = 5e2 + 1; const int INF = INT_MAX / 3; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; string s; cin >> s; int ans = 0, i = 0; for (; i < s.size() - 1; i++) { if (s[i] == 'U' && s[i + 1] == 'R') i++; else if (s[i + 1] == 'U' && s[i] == 'R') i++; ans++; } if (i == s.size() - 1) ans++; cout << ans << '\n'; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) s=input() a=list(s) i=0 c=0 while i<n-1: if a[i]=='U' and a[i+1]=='R': c=c+1 a[i]='D' a[i+1]='D' elif a[i]=='R'and a[i+1]=='U': a[i]='D' a[i+1]='D' c=c+1 i=i+1 print(n-c)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; const int oo = 0x3f3f3f3f; const int MAXN = 1000100; char s[110]; int main() { int n; cin >> n; char c; int r = 0, u = 0; for (int i = 0; i < n; ++i) { cin >> s[i]; } int ans = n; for (int i = 0; i < n; ++i) { if (s[i] == 'U' && s[i + 1] == 'R' || s[i] == 'R' && s[i + 1] == 'U') { --ans; i++; } } cout << ans << endl; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
def main(): input() a, r = '?', 0 for b in input(): if a == '?': r += 1 a = b else: if a != b: a = '?' else: r += 1 print(r + ('?' != a != b)) if __name__ == '__main__': main()
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) a=input() i=0 count=0 while i<n-1: if (a[i]=='U' and a[i+1]=='R') or (a[i]=='R' and a[i+1]=='U'): count+=1 i+=1 i+=1 print(n-count)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { int x, n; char s[300]; scanf("%d\n", &x); n = x; for (int i = 3; i <= n + 2; i++) { scanf("%c", &s[i]); while (s[i] != 'U' && s[i] != 'R') { i--; continue; } if ((s[i] == 'U' && s[i - 1] == 'R') || (s[i] == 'R' && s[i - 1] == 'U')) s[i] = 'D', x--; } cout << x << endl; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) s=input() i=0 ans=0 while i<n-1: if (s[i]=="U" and s[i+1]=="R") or (s[i]=="R" and s[i+1]=="U"): ans+=1 i+=2 else: ans+=1 i+=1 while i<n: ans+=1 i+=1 print (ans)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) data = input() i = 0 count = 0 while i <= len(data): if (data[i:i + 2] == "RU") or (data[i:i + 2] == "UR"): i += 1 count += 1 i += 1 print(n - count)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); String s = scanner.next(); int l = 0; for(int i=0;i<s.length()-1;i++){ if(s.charAt(i)!=s.charAt(i+1)){ l++; i++; } } System.out.println(n-l); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; long long int MOD = 1000007.; long long int inf = 1e15; void scan(int &x); long long int powermod(long long int _a, long long int _b, long long int _m) { long long int _r = 1; while (_b) { if (_b % 2 == 1) _r = (_r * _a) % _m; _b /= 2; _a = (_a * _a) % _m; } return _r; } long long int string_to_number(string s) { long long int x = 0; stringstream convert(s); convert >> x; return x; } long long int add(long long int a, long long int b) { long long int x = (a + b) % MOD; return x; } long long int mul(long long int a, long long int b) { long long int x = (a * b) % MOD; return x; } long long int sub(long long int a, long long int b) { long long int x = (a - b + MOD) % MOD; return x; } long long int divi(long long int a, long long int b) { long long int x = a; long long int y = powermod(b, MOD - 2, MOD); long long int res = (x * y) % MOD; return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; string str; cin >> str; int cnt = 0; for (int i = 0; i < n; i++) { if (i == n - 1) { cnt++; } else { if (str[i] == 'R') { if (str[i + 1] == 'U') { i++; } } else if (str[i] == 'U') { if (str[i + 1] == 'R') { i++; } } cnt++; } } cout << cnt; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
input() s=input() s+=s[-1] ans=0 i=1 while i<len(s): ans+=1 if s[i]!=s[i-1]: i+=2 else: i+=1 print(ans)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, i, x; cin >> n; string s; cin >> s; x = n; for (i = 0; i < n; i++) { if (s[i] == 'R' && s[i + 1] == 'U') { x--; i++; } else if (s[i] == 'U' && s[i + 1] == 'R') { x--; i++; } } cout << x << '\n'; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.Scanner; public class diagWalking{ static public void main(String args[]) { Scanner rdi = new Scanner(System.in); int n = rdi.nextInt(); char[] inpc = new char[n]; String inp = rdi.next(); inpc = inp.toCharArray(); int length = 0; if(n==1) { System.out.println(1); } else { for(int i=0; i<n; i++) { if(inpc[i]=='R' && (i+1<n) && inpc[i+1]=='U') { length++; i++; } else if(inpc[i]=='U' && (i+1<n) && inpc[i+1]=='R') { length++; i++; } else { length++; } } System.out.println(length); } } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.*; import java.util.*; import java.math.*; public class Solution{ public static void main(String[] args) { Scanner in = new Scanner(System.in); int n,i,answer; char[] s; n=in.nextInt(); s=in.next().toCharArray(); answer=n; for(i=0;i<=n-2;i++){ if(s[i]=='U' && s[i+1]=='R'){ s[i]='D'; s[i+1]='X'; answer--; } if(s[i]=='R' && s[i+1]=='U'){ s[i]='D'; s[i+1]='X'; answer--; } } System.out.println(answer); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) s = input() i = 0 reps = 0 while i < n - 1: if s[i:i+2] in ['RU', 'UR']: reps += 1 i += 2 else: i += 1 print(n - reps)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) moves = input().strip() count = 0 k = 0 while k < len(moves): if k == len(moves)-1: count += 1 break if moves[k] == 'U' and moves[k + 1] == 'R': count += 1 k += 2 elif moves[k] == 'R' and moves[k + 1] == 'U': count += 1 k += 2 else: count += 1 k += 1 print(count)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; string s; cin >> n >> s; stack<char> st; st.push(s[0]); for (int i = 1; i < s.size(); ++i) { if (s[i] != st.top() && st.top() != 'D' && s[i] != 'D') st.pop(), st.push('D'); else st.push(s[i]); } cout << st.size(); }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.Scanner; public class main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); char[] input = sc.nextLine().toCharArray(); boolean[] minimun = new boolean[n]; int count =0; for(int i =0; i < n; i++){ if(i!=0){ if(input[i]!= input[i-1] && minimun[i-1]!= true){ minimun[i] = true; } else{ count++; } } else{ count++; } } System.out.println(count); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.Scanner; public class Hod { public static void main(String[] args) { Dia aaa = new Dia(); aaa.solve(); aaa.print(); } } class Dia { int n; String s; String str2 = new String("UR"); String str3 = new String("RU"); int count = 0; Dia() { Scanner in = new Scanner(System.in); n = in.nextInt(); s = in.next(); in.close(); } void solve() { char[] d = s.toCharArray(); for (int i = 0; i <= n - 1; i++) { if ((i!=n-1)&&d[i] != d[i + 1]) { count++; i++; } else { count++; } } } void print() { System.out.println(count); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(raw_input()) strg = raw_input() s = [] for i in strg: s.append(i) i = 0 for i in range(len(s) - 1): if not s[i] == '*' or s[i] == '#': if (s[i] == 'U' and s[i + 1] == 'R') or (s[i] == 'R' and s[i + 1] == 'U'): s[i] = '*' s[i + 1] = '#' i += 1 r = 0 for i in s: if i != '*': r += 1 print r
PYTHON
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { int len; char str[105]; while (~scanf("%d", &len)) { scanf("%s", str); int ans = 0; for (int i = 0; i < len; i++) { if ((str[i] == 'R' && str[i + 1] == 'U') || (str[i] == 'U' && str[i + 1] == 'R')) { ans++; i++; } } printf("%d\n", len - ans); } }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#coding: utf-8 entrada1 = int(raw_input()) entrada2 = raw_input() resultado = entrada1 i = 0 while i < (entrada1 - 1): if entrada2[i] != entrada2[i+1]: resultado -= 1 i += 2 else: i += 1 print resultado
PYTHON
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) s = input() answer = "" while (len(s) > 0): if s[:2] in ["RU", "UR"]: answer += "D" s = s[2:] continue answer += s[0] s = s[1:] print(len(answer))
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.*; import java.io.*; import java.math.*; public class Main{ static FR in; public static void main(String[] args)throws Exception{ in = new FR(); int n = ni(); String s = nln(); int ctr=0, ctr2=0; for( int i=1;i<s.length();i++ ){ if(( s.charAt(i-1)=='R' && s.charAt(i)=='U' )|| ( s.charAt(i-1)=='U' && s.charAt(i)=='R' )){ ctr++; i++; } } pn((int)(s.length()-Math.ceil(ctr))); } static long[]la( int N ){ long[]a=new long[N]; for( int i=0;i<N;i++ )a[i]=nl(); return a; } static int[] ia(int N){ int[] a = new int[N]; for(int i = 0; i<N; i++){ a[i] = ni(); } return a; } static class FR{ BufferedReader br; StringTokenizer st; public FR(){ 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(); } String nextLine(){ String str = ""; try{ str = br.readLine(); }catch (IOException e){ e.printStackTrace(); } return str; } } static void p(Object o){ System.out.print(o); } static void pn(Object o){ System.out.println(o); } static String n(){ return in.next(); } static String nln(){ return in.nextLine(); } static int ni(){ return Integer.parseInt(in.next()); } static long nl(){ return Long.parseLong(in.next()); } }
JAVA