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 int mod = 998244353; void solve() { int n; cin >> n; string s; cin >> s; int ans = n; for (int i = 0; i < n - 1; i++) { if (s[i] == 'U' && s[i + 1] == 'R') { ans--; i++; continue; } if (s[i] == 'R' && s[i + 1] == 'U') { ans--; i++; continue; } } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int tests = 1; while (tests--) { solve(); } }
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.*; public class ash{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); String s = scan.next(); int count = 0; int i = 1; while(i < s.length()) { if (s.charAt(i) == 'U' && s.charAt(i-1) == 'R') { count++; i+=2; } else if (s.charAt(i) == 'R' && s.charAt(i-1) == 'U') { count++; i+=2; } else i++; } //System.out.println(n+" "+count); 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
n = int(input()) s = list(input()) ans = 0 i = 0 while True: if (i >= len(s)-1): break if (s[i] == "R" and s[i+1] == "U") or (s[i] == "U" and s[i+1] == "R"): ans += 1 i += 2 else: i += 1 print(len(s) - 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 vis[110] = {0}; int main() { int n; cin >> n; string s; cin >> s; int ct = 0; for (int i = 0; i < (int)s.size() - 1; i++) { if (s[i] != s[i + 1] && vis[i] == 0 && vis[i + 1] == 0) { vis[i] = 1; vis[i + 1] = 1; ct++; } } cout << n - ct << 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() { long long i, j, k, t, n; string s; cin >> n; getline(cin >> ws, s); long long cnt = 0; for (i = 0; i < n - 1; i++) { if ((s[i] == 'U' and s[i + 1] == 'R') or (s[i] == 'R' and s[i + 1] == 'U')) { cnt++; i++; } } cout << n - cnt << 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()) c=input() ans = n lst = 0 for i in range(n): if lst == 0 and c[i]=='U' and c[i-1]=='R': ans-=1 lst = 1 elif lst == 0 and c[i]=='R' and c[i-1]=='U': ans-=1 lst=1 else: lst=0 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
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ProblemADiagonalWalking solver = new ProblemADiagonalWalking(); solver.solve(1, in, out); out.close(); } static class ProblemADiagonalWalking { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); char[] s = in.next().toCharArray(); int[] dist = new int[n + 1]; dist[n - 1] = 1; for (int i = n - 2; i >= 0; --i) { dist[i] = 1 + dist[i + 1]; if (s[i] != s[i + 1]) dist[i] = Math.min(dist[i], 1 + dist[i + 2]); } out.println(dist[0]); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
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() output = 0 cont = False for i in range(n-1): if cont: cont = False continue if s[i:i+2] == "UR" or s[i:i+2] == "RU": output += 1 cont = True print(n - output)
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; string s; int cnt = 0; int flag = 0; cin >> s; for (int i = 0; i < s.length(); i++) { if ((s[i] == 'U' && s[i + 1] == 'R') || (s[i] == 'R' && s[i + 1] == 'U')) { i++; cnt++; } } cout << 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
flag=0 n=int(input()) seq=input() prev=seq[0] for x in seq: if x!=prev and flag==1: n-=1 flag=0 else: flag=1 prev=x 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
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, j, l, c, c2 = 0, x, point = 0; char str[108]; scanf("%d", &n); scanf("%s", str); for (i = 0; i < n - 1; i++) { if ((str[i] == 'R' && str[1 + i] == 'U') || (str[i] == 'U' && str[1 + i] == 'R')) { c2++; i++; } } printf("%d", n - c2); }
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; string s; cin >> n >> s; for (int i = 0; i < s.length(); i++) { if (s[i] == 'R') { if (s[i + 1] == 'U') { n--; i++; continue; } } else if (s[i] == 'U') { if (s[i + 1] == 'R') { n--; i++; continue; } } } cout << n << 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
# https://vjudge.net/contest/319995#problem/G n = int(input()) characters = str(input()) ru = 0 ur = 0 new = "" i = 0 while i < n: pas = characters[i] if i < n-1: pas += characters[i+1] if pas == 'RU' or pas == "UR": new += 'D' i += 1 else: new += pas[0] i += 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; template <class _T> inline void read(_T &_x) { int _t; bool _flag = false; while ((_t = getchar()) != '-' && (_t < '0' || _t > '9')) ; if (_t == '-') _flag = true, _t = getchar(); _x = _t - '0'; while ((_t = getchar()) >= '0' && _t <= '9') _x = _x * 10 + _t - '0'; if (_flag) _x = -_x; } int n; string s; int main() { read(n); cin >> s; for (int i = 0; i < (int)s.size() - 1; ++i) { if (s[i] != s[i + 1]) --n, ++i; } cout << n << 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.*; public class ADiagonalWalking { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); String line = sc.nextLine(); for(int i = 0;i<line.length()-1;i++) { if(line.charAt(i)!=line.charAt(i+1)) { line = line.substring(0, i) + line.substring(i+1); } } System.out.println(line.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 n; string s; int main() { cin >> n >> s; int del = 0; for (int i = 0; i < s.length() - 1; i++) { if (s[i] != s[i + 1]) { del++; i++; } } cout << n - del; 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 javax.annotation.processing.AbstractProcessor; import java.io.*; import java.lang.annotation.*; import java.math.BigInteger; import java.math.MathContext; import java.util.*; import java.util.function.*; import static java.lang.Math.*; public class Main { public static int[] prefixFunction(String second) { int[] res = new int[second.length()]; for (int i = 1; i < second.length(); ++i) { int j = res[i - 1]; while (j > 0 && second.charAt(i) != second.charAt(j)) { j = res[j - 1]; } if (second.charAt(i) == second.charAt(j)) { ++j; } res[i] = j; } return res; } public static int[] zFunction(String second) { int[] res = new int[second.length()]; for (int i = 1, l = 0, r = 0; i < second.length(); ++i) { res[i] = max(min(r - i + 1, res[i - l]), 0); while (i + res[i] < second.length() && second.charAt(res[i]) == second.charAt(i + res[i])) { res[i]++; } if (i + res[i] - 1 > r) { l = i; r = i + res[i] - 1; } } return res; } public static Object[] reverse(Object[] arr) { for (int i = 0; i < arr.length / 2; i++) { swap(arr[i], arr[arr.length - i - 1]); } return arr; } public static char[] reverse(char[] arr) { for (int i = 0; i < arr.length / 2; i++) { char tmp = arr[i]; arr[i] = arr[arr.length - i - 1]; arr[arr.length - i - 1] = tmp; } return arr; } public static int[] reverse(int[] arr) { for (int i = 0; i < arr.length / 2; i++) { int tmp = arr[i]; arr[i] = arr[arr.length - i - 1]; arr[arr.length - i - 1] = tmp; } return arr; } public static long[] reverse(long[] arr) { for (int i = 0; i < arr.length / 2; i++) { long tmp = arr[i]; arr[i] = arr[arr.length - i - 1]; arr[arr.length - i - 1] = tmp; } return arr; } public static boolean[] reverse(boolean[] arr) { for (int i = 0; i < arr.length / 2; i++) { boolean tmp = arr[i]; arr[i] = arr[arr.length - i - 1]; arr[arr.length - i - 1] = tmp; } return arr; } public static void swap(Object a, Object b) { Object tmp = a; a = b; b = tmp; tmp = null; } private static final int MAX_SIZE = 29; public static class Treap { Treap l, r; long cnt, sum; long x, y; boolean isReversed = false; public Treap(long x) { this.x = x; y = (long) Math.round(Math.random() * Integer.MAX_VALUE); cnt = 1; sum = x; } public void update() { cnt = 1; sum = x; if (l != null) { cnt += l.cnt; sum = l.sum + sum; } if (r != null) { cnt += r.cnt; sum = r.sum + sum; } } @Override public String toString() { push(this); String res = ""; if (l != null) { res += l.toString(); } res += (x + " "); if (r != null) { res += r.toString(); } return res; } } public static long cnt(Treap t) { if (t == null) { return 0; } return t.cnt; } public static long sum(Treap t) { if (t == null) { return 0; } return t.sum; } public static void push(Treap t) { if (t != null) { if (t.isReversed) { Treap tmp = t.l; t.l = t.r; t.r = tmp; t.isReversed = false; if (t.l != null) { t.l.isReversed ^= true; } if (t.r != null) { t.r.isReversed ^= true; } } } } public static Treap merge(Treap a, Treap b) { push(a); push(b); if (a == null) { return b; } if (b == null) { return a; } if (a.y > b.y) { a.r = merge(a.r, b); a.update(); return a; } else { b.l = merge(a, b.l); b.update(); return b; } } public static Pair<Treap, Treap> split(Treap t, long index, long add) { if (t == null) { return new Pair<Treap, Treap>(null, null); } push(t); if (cnt(t.l) + add >= index) { Pair<Treap, Treap> p = split(t.l, index, add); t.l = p.second; t.update(); return new Pair<Treap, Treap>(p.first, t); } else { Pair<Treap, Treap> p = split(t.r, index, add + 1 + cnt(t.l)); t.r = p.first; t.update(); return new Pair<Treap, Treap>(t, p.second); } } public static long getsum(Treap t, long l, long r) { if (t == null) { return 0; } Pair<Treap, Treap> p2 = split(t, r, 0); Pair<Treap, Treap> p1 = split(p2.first, l, 0); if (p1.second == null) { t = merge(p1.first, p1.second); t = merge(t, p2.second); t.update(); return 0; } p1.second.update(); long res = sum(p1.second); t = merge(p1.first, p1.second); t = merge(t, p2.second); t.update(); return res; } public static void reverse(Treap t, long l, long r) { l -= 1; if (t == null) { return; } Pair<Treap, Treap> p2 = split(t, r, 0); Pair<Treap, Treap> p1 = split(p2.first, l, 0); p1.second.isReversed ^= true; t = merge(p1.first, p1.second); t = merge(t, p2.second); t.update(); } public static Treap add(Treap t, long index, long val) { index -= 1; Pair<Treap, Treap> p = split(t, index, 0); Treap res = merge(null, p.first); res = merge(res, new Treap(val)); res = merge(res, p.second); return res; } public static void main(String[] args) { FastScanner in = new FastScanner(System.in); int n = in.nextInt(); String s = in.next(); boolean[] used = new boolean[n]; int cnt = 0; for (int i = 0; i < n - 1; i++) { if (!used[i]) { if (s.charAt(i) != s.charAt(i + 1)) { used[i + 1] = true; cnt++; } } } System.out.println(n - cnt); } } class FastScanner { private BufferedReader br; private StringTokenizer st; public FastScanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in), 1 << 15); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public Integer nextInt() { return Integer.parseInt(next()); } public Long nextLong() { return Long.parseLong(next()); } public Double nextDouble() { return Double.parseDouble(next()); } public Float nextFloat() { return Float.parseFloat(next()); } public Boolean nextBoolean() { return Boolean.parseBoolean(next()); } public int[] nextIntArr(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = nextInt(); } return res; } public long[] nextLongArr(int n) { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = nextLong(); } return res; } public Point2D nextPoint() { return new Point2D(nextDouble(), nextDouble()); } public Line2D nextLine() { return new Line2D(nextDouble(), nextDouble(), nextDouble()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } } class Pair<A, B> implements Comparable<Pair<A, B>> { A first; B second; public Pair(A first, B second) { this.first = first; this.second = second; } @Override public int compareTo(Pair<A, B> o) { Comparable a = (Comparable) first; Comparable b = (Comparable) second; return a.compareTo(o.first) == 0 ? b.compareTo(o.second) : a.compareTo(o.first); } @Override public String toString() { return "[" + first + ", " + second + ']'; } } class Point2D { public static final Point2D ZERO = new Point2D(0.0, 0.0); private double x; public final double getX() { return x; } private double y; public final double getY() { return y; } private int hash = 0; public Point2D(double x, double y) { this.x = x; this.y = y; } public double distance(double x1, double y1) { double a = getX() - x1; double b = getY() - y1; return Math.sqrt(a * a + b * b); } public double distance(Point2D point) { return distance(point.getX(), point.getY()); } public Point2D add(double x, double y) { return new Point2D( getX() + x, getY() + y); } public Point2D add(Point2D point) { return add(point.getX(), point.getY()); } public Point2D subtract(double x, double y) { return new Point2D( getX() - x, getY() - y); } public Point2D multiply(double factor) { return new Point2D(getX() * factor, getY() * factor); } public Point2D subtract(Point2D point) { return subtract(point.getX(), point.getY()); } public Point2D normalize() { final double mag = magnitude(); if (mag == 0.0) { return new Point2D(0.0, 0.0); } return new Point2D( getX() / mag, getY() / mag); } public Point2D midpoint(double x, double y) { return new Point2D( x + (getX() - x) / 2.0, y + (getY() - y) / 2.0); } public Point2D midpoint(Point2D point) { return midpoint(point.getX(), point.getY()); } public double angle(double x, double y) { final double ax = getX(); final double ay = getY(); final double delta = (ax * x + ay * y) / Math.sqrt( (ax * ax + ay * ay) * (x * x + y * y)); if (delta > 1.0) { return 0.0; } if (delta < -1.0) { return 180.0; } return Math.toDegrees(Math.acos(delta)); } public double angle(Point2D point) { return angle(point.getX(), point.getY()); } public double angle(Point2D p1, Point2D p2) { final double x = getX(); final double y = getY(); final double ax = p1.getX() - x; final double ay = p1.getY() - y; final double bx = p2.getX() - x; final double by = p2.getY() - y; final double delta = (ax * bx + ay * by) / Math.sqrt( (ax * ax + ay * ay) * (bx * bx + by * by)); if (delta > 1.0) { return 0.0; } if (delta < -1.0) { return 180.0; } return Math.toDegrees(Math.acos(delta)); } public double magnitude() { final double x = getX(); final double y = getY(); return Math.sqrt(x * x + y * y); } public double dotProduct(double x, double y) { return getX() * x + getY() * y; } public double dotProduct(Point2D vector) { return dotProduct(vector.getX(), vector.getY()); } public Point3D crossProduct(double x, double y) { final double ax = getX(); final double ay = getY(); return new Point3D( 0, 0, ax * y - ay * x); } public Point3D crossProduct(Point2D vector) { return crossProduct(vector.getX(), vector.getY()); } @Override public boolean equals(Object obj) { if (obj == this) return true; if (obj instanceof Point2D) { Point2D other = (Point2D) obj; return getX() == other.getX() && getY() == other.getY(); } else return false; } @Override public int hashCode() { if (hash == 0) { long bits = 7L; bits = 31L * bits + Double.doubleToLongBits(getX()); bits = 31L * bits + Double.doubleToLongBits(getY()); hash = (int) (bits ^ (bits >> 32)); } return hash; } @Override public String toString() { return "Point2D [x = " + getX() + ", y = " + getY() + "]"; } } class Point3D { public static final Point3D ZERO = new Point3D(0.0, 0.0, 0.0); private double x; public final double getX() { return x; } private double y; public final double getY() { return y; } private double res; public final double getZ() { return res; } private int hash = 0; public Point3D(double x, double y, double res) { this.x = x; this.y = y; this.res = res; } public double distance(double x1, double y1, double z1) { double a = getX() - x1; double b = getY() - y1; double c = getZ() - z1; return Math.sqrt(a * a + b * b + c * c); } public double distance(Point3D point) { return distance(point.getX(), point.getY(), point.getZ()); } public Point3D add(double x, double y, double res) { return new Point3D( getX() + x, getY() + y, getZ() + res); } public Point3D add(Point3D point) { return add(point.getX(), point.getY(), point.getZ()); } public Point3D subtract(double x, double y, double res) { return new Point3D( getX() - x, getY() - y, getZ() - res); } public Point3D subtract(Point3D point) { return subtract(point.getX(), point.getY(), point.getZ()); } public Point3D multiply(double factor) { return new Point3D(getX() * factor, getY() * factor, getZ() * factor); } public Point3D normalize() { final double mag = magnitude(); if (mag == 0.0) { return new Point3D(0.0, 0.0, 0.0); } return new Point3D( getX() / mag, getY() / mag, getZ() / mag); } public Point3D midpoint(double x, double y, double res) { return new Point3D( x + (getX() - x) / 2.0, y + (getY() - y) / 2.0, res + (getZ() - res) / 2.0); } public Point3D midpoint(Point3D point) { return midpoint(point.getX(), point.getY(), point.getZ()); } public double angle(double x, double y, double res) { final double ax = getX(); final double ay = getY(); final double az = getZ(); final double delta = (ax * x + ay * y + az * res) / Math.sqrt( (ax * ax + ay * ay + az * az) * (x * x + y * y + res * res)); if (delta > 1.0) { return 0.0; } if (delta < -1.0) { return 180.0; } return Math.toDegrees(Math.acos(delta)); } public double angle(Point3D point) { return angle(point.getX(), point.getY(), point.getZ()); } public double angle(Point3D p1, Point3D p2) { final double x = getX(); final double y = getY(); final double res = getZ(); final double ax = p1.getX() - x; final double ay = p1.getY() - y; final double az = p1.getZ() - res; final double bx = p2.getX() - x; final double by = p2.getY() - y; final double bz = p2.getZ() - res; final double delta = (ax * bx + ay * by + az * bz) / Math.sqrt( (ax * ax + ay * ay + az * az) * (bx * bx + by * by + bz * bz)); if (delta > 1.0) { return 0.0; } if (delta < -1.0) { return 180.0; } return Math.toDegrees(Math.acos(delta)); } public double magnitude() { final double x = getX(); final double y = getY(); final double res = getZ(); return Math.sqrt(x * x + y * y + res * res); } public double dotProduct(double x, double y, double res) { return getX() * x + getY() * y + getZ() * res; } public double dotProduct(Point3D vector) { return dotProduct(vector.getX(), vector.getY(), vector.getZ()); } public Point3D crossProduct(double x, double y, double res) { final double ax = getX(); final double ay = getY(); final double az = getZ(); return new Point3D( ay * res - az * y, az * x - ax * res, ax * y - ay * x); } public Point3D crossProduct(Point3D vector) { return crossProduct(vector.getX(), vector.getY(), vector.getZ()); } @Override public boolean equals(Object obj) { if (obj == this) return true; if (obj instanceof Point3D) { Point3D other = (Point3D) obj; return getX() == other.getX() && getY() == other.getY() && getZ() == other.getZ(); } else return false; } @Override public int hashCode() { if (hash == 0) { long bits = 7L; bits = 31L * bits + Double.doubleToLongBits(getX()); bits = 31L * bits + Double.doubleToLongBits(getY()); bits = 31L * bits + Double.doubleToLongBits(getZ()); hash = (int) (bits ^ (bits >> 32)); } return hash; } @Override public String toString() { return "Point3D [x = " + getX() + ", y = " + getY() + ", res = " + getZ() + "]"; } } class Line2D { double a, b, c; Point2D A, B; Point2D normal; public Line2D(double a, double b, double c) { this.a = a; this.b = b; this.c = c; fromEquationToPoints(a, b, c); } public Line2D(Point2D a, Point2D b) { A = a; B = b; fromPointsToEquation(a, b); } private void fromPointsToEquation(Point2D A, Point2D B) { Point2D vect = B.subtract(A); normal = new Point2D(vect.getY(), -vect.getX()).normalize(); a = normal.getX(); b = normal.getY(); c = -(a * A.getX() + b * B.getY()); } private void fromEquationToPoints(double a, double b, double c) { if (b == 0) { A = new Point2D(-c / a, 0); B = new Point2D(-c / a, 1); } else { A = new Point2D(0, -(c) / b); B = new Point2D(1, -(c + a) / b); } } private double calculate(Point2D o) { return (a * o.getX() + b * o.getY() + c); } public double dist(Point2D o) { return abs(o.subtract(A).crossProduct(o.subtract(B)).getZ()) / A.distance(B); } public Point2D getNormal() { if (normal != null) { return normal; } return normal = new Point2D(a, b); } public Point2D reflect(Point2D o) { Point2D norm = getNormal(); norm = norm.multiply(calculate(o)); norm = norm.multiply(-1); return o.add(norm).add(norm); } } class SegTree<T> { public T[] tree; public T[] delta; BiFunction<T, T, T> merge; BiFunction<Pair<T, Integer>, T, T> modify; public void setDeltamerge(BiFunction<T, T, T> deltamerge) { this.deltamerge = deltamerge; } BiFunction<T, T, T> deltamerge; public SegTree(T[] a, BiFunction<T, T, T> merge) { tree = (T[]) new Object[a.length * 4]; delta = (T[]) new Object[a.length * 4]; this.merge = merge; build(1, 1, a.length, a); n = a.length; } public void setModify(BiFunction<Pair<T, Integer>, T, T> modify) { this.modify = modify; } private int ind = 0; private int n; private void build(int v, int l, int r, T[] a) { if (l == r) { tree[v] = a[ind++]; return; } int m = (r - l + 1) / 2 + l - 1; build(v * 2, l, m, a); build(v * 2 + 1, m + 1, r, a); tree[v] = merge.apply(tree[v * 2], tree[v * 2 + 1]); } private T get(int v, int l, int r, int left, int right) { push(v, l, r); if (left <= l && r <= right) { //push(v, l, r); return tree[v]; } int m = (r - l + 1) / 2 + l - 1; if ((m < left || l > right)) { //push(v, l, r); return get(v * 2 + 1, m + 1, r, left, right); } if ((r < left || m + 1 > right)) { //push(v, l, r); return get(v * 2, l, m, left, right); } //push(v, l, r); return merge.apply(get(v * 2, l, m, left, right), get(v * 2 + 1, m + 1, r, left, right)); } public T get(int l, int r) { return get(1, 1, n, l, r); } public void modify(int l, int r, T delta) { modify(1, 1, n, l, r, delta); } private void modify(int v, int l, int r, int left, int right, T delta) { push(v, l, r); if (left <= l && r <= right) { this.delta[v] = delta; push(v, l, r); return; } int m = (r - l + 1) / 2 + l - 1; if (m >= left) { push(v, l, r); modify(v * 2, l, m, left, right, delta); } if (m + 1 <= right) { push(v, l, r); modify(v * 2 + 1, m + 1, r, left, right, delta); } tree[v] = merge.apply(tree[v * 2], tree[v * 2 + 1]); } public void push(int v, int l, int r) { if (delta[v] == null) { return; } tree[v] = modify.apply(new Pair<>(tree[v], r - l + 1), delta[v]); if (v * 2 < delta.length) { delta[v * 2] = deltamerge.apply(delta[v * 2], delta[v]); ; } if (v * 2 + 1 < delta.length) { delta[v * 2 + 1] = deltamerge.apply(delta[v * 2 + 1], delta[v]); } delta[v] = null; } } class Trie { private ArrayList<Integer[]> go; private int alphabet; private char zeroChar; private int ind = 1; public Trie(int initialCapasity, int alphabet, char zeroChar) { this.alphabet = alphabet; go = new ArrayList<>(initialCapasity); this.zeroChar = zeroChar; Integer[] links = new Integer[alphabet]; Arrays.fill(links, -1); go.add(links); } public Trie(int alphabet, char zeroChar) { this(10000, alphabet, zeroChar); } public Trie() { this(26, 'a'); } public void add(String str) { int cur = 0; for (int i = 0; i < str.length(); i++) { if (go.get(cur)[str.charAt(i) - zeroChar] == -1) { Integer[] links = new Integer[alphabet]; Arrays.fill(links, -1); go.add(links); go.get(cur)[str.charAt(i) - zeroChar] = go.size() - 1; } cur = go.get(cur)[str.charAt(i) - zeroChar]; } } }
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() arr=[] tb='' if n==1: print(1) else: i=0 c=0 while i<(n-1): if i==n-2: c=1 if s[i]=='U' and s[i+1]=='R': tb+='D' i+=1 elif s[i]=='R' and s[i+1]=='U': tb+='D' i+=1 else: tb+=s[i] i+=1 if c!=1 or tb[-1]!='D': tb+=s[n-1] print(len(tb))
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 Codeforces { public static void main(String[] args) { TaskA Solver = new TaskA(); Solver.Solve(); } private static class TaskA { private void Solve() { Scanner in = new Scanner(System.in); int n = in.nextInt(); String S = in.next(); int subtr = 0; for (int i = 0; i < n - 1; ) { char cur = S.charAt(i); char next = S.charAt(i + 1); if ((cur == 'U' && next == 'R') || (cur == 'R' && next == 'U')) { subtr++; i += 2; } else i++; } System.out.println(n - subtr); } } }
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; char s[110]; int main() { int n, num = 0; cin >> n >> s; for (int i = 0; i < n - 1; i++) { if (s[i] == 'U' && s[i + 1] == 'R' || s[i] == 'R' && s[i + 1] == 'U') { s[i] = 'X', s[i + 1] = 'X'; num++; } } cout << n - num << 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
# Diagonal Walking n = int(input()) s = input() s = s.replace("RU", 'D').replace('UDR', 'DD').replace('UR', 'D') print(len(s))
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 c = 0, n; string s; cin >> n; cin >> s; for (int i = 0; i < n; i++) { if (s[i] != s[i + 1]) { i++; } c++; } cout << c; 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 t, n, j, l, i; int c = 0, d = 0; cin >> t; string a; for (i = 0; i < t; i++) { cin >> a; l = a.size(); for (i = 0; i < a.size() - 1; i++) { if ((a[i] == 'R' && a[i + 1] == 'U') || (a[i] == 'U' && a[i + 1] == 'R')) { l--; i++; } } } cout << l << 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
def solve(s, n): r, last, flag = n, s[0], True for i in range(1, len(s)): if s[i] != last and flag: r -= 1 flag = False else: flag = True last = s[i] return r n = int(input()) s = input() print(solve(s, 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 re n = input() s = input() s = re.sub('(RU|UR)', 'D', s) a = len(s) print(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
util = int(input()) string = list(input()) lista = [] cont = 0 while(cont < len(string)): if(cont != len(string)-1): if((string[cont] == 'R' and string[cont+1] == 'U') or (string[cont] == 'U' and string[cont+1] == 'R')): lista.append('D') cont += 1 else: lista.append(string[cont]) else: lista.append(string[cont]) cont += 1 print(len(lista))
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; string a; int main() { int n; int i = 0; cin >> n; cin >> a; int s = n; while (i != n) { if ((a[i] == 'U' && a[i + 1] == 'R') || (a[i] == 'R' && a[i + 1] == 'U')) { i += 2; s--; } else { i++; } } cout << s << 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
le = int(input()) st = input() c = 0 if le == 2: if st[0] != st[1]: print(1) else: print(2) elif le == 1: print(1) else: x = st[0] for i in range(0, le): if x != st[i] and i != le - 1: c += 1 x = st[i + 1] elif x != st[i] and i == le - 1: c += 1 print(le - 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; int main() { int n; cin >> n; char a[n]; cin >> a; int ans = 0; for (int i = 0; i < n; ++i) { if (a[i] != a[i + 1]) { ++ans; ++i; } else { ++ans; } } 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
#include <bits/stdc++.h> using namespace std; bool vis[200]; int main() { int n; scanf("%d", &n); string S; cin >> S; int ans = n; for (int i = 0; i < n - 1; i++) { if (vis[i]) continue; if ((S[i] == 'U' && S[i + 1] == 'R') || (S[i] == 'R' && S[i + 1] == 'U')) { vis[i + 1] = 1; ans--; } } printf("%d\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
/** * Created by Aminul on 3/22/2018. */ import java.io.*; import java.util.*; import static java.lang.Math.*; public class A { public static void main(String[] args)throws Exception { FastReader in = new FastReader(System.in); PrintWriter pw = new PrintWriter(System.out); int n = in.nextInt(); char[] s = in.next(n); StringBuilder res = new StringBuilder(); for(int i = 0; i < n; i++){ if(i+1 < n){ if(s[i] == 'R' && s[i+1] == 'U'){ res.append("D"); i++; } else if(s[i] == 'U' && s[i+1] == 'R'){ i++; res.append("D"); } else res.append(s[i]); } else{ res.append(s[i]); } } //debug(res); pw.println(res.length()); pw.close(); } static void debug(Object...obj) { System.err.println(Arrays.deepToString(obj)); } static class FastReader { InputStream is; private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; static final int ints[] = new int[128]; public FastReader(InputStream is){ for(int i='0';i<='9';i++) ints[i]=i-'0'; this.is = is; } public int readByte(){ if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } public boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } public String next(){ int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int nextInt(){ int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = (num<<3) + (num<<1) + ints[b]; }else{ return minus ? -num : num; } b = readByte(); } } public long nextLong() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = (num<<3) + (num<<1) + ints[b]; }else{ return minus ? -num : num; } b = readByte(); } } public double nextDouble() { return Double.parseDouble(next()); } /* public char nextChar() { return (char)skip(); }*/ public char[] next(int n){ char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } /*private char buff[] = new char[1005]; public char[] nextCharArray(){ int b = skip(), p = 0; while(!(isSpaceChar(b))){ buff[p++] = (char)b; b = readByte(); } return Arrays.copyOf(buff, p); }*/ } }
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.*; public class MyClass { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int len=sc.nextInt(); String str=sc.next(); int k=0; for(int i=0;i<len-1;) { char ch1=str.charAt(i); char ch2=str.charAt(i+1); if((ch1=='R'&&ch2=='U')||(ch1=='U'&&ch2=='R')) { k++; i+=2; } else i++; } System.out.println(len-k); } }
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
res = int(input()) s = input() fl = True k = s[0] i = 1 while i < len(s): if s[i] != k: res -= 1 fl = False if i != len(s)-1: k = s[i+1] i += 1 i += 1 else: fl = True i += 1 print(res)
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.BufferedInputStream; import java.util.Scanner; public class Main { public static void main(String[] a) { Scanner sc = new Scanner (new BufferedInputStream(System.in)); sc.next(); System.out.println(sc.next().replaceAll("RU|UR", "R").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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author MoreThanANoob */ 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 sc, PrintWriter w) { int n = sc.nextInt(); int curr = 0; int ans = 0; char c[] = sc.next().toCharArray(); while (curr < n - 1) { if ((c[curr] == 'U' && c[curr + 1] == 'R') || (c[curr] == 'R' && c[curr + 1] == 'U')) { ans++; curr += 2; } else { ans++; curr++; } } if (curr < n) ans++; w.println(ans); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
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()) c=n s=input() i=0 while i<n-1: if s[i:i+2]=='UR' or s[i:i+2]=='RU': c=c-1 i=i+1 i=i+1 print(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
#coding: utf-8 n = int(raw_input()) seq = raw_input() new_seq = "" while len(seq) > 1: temp = seq[0] + seq[1] if temp in ["RU", "UR"]: new_seq += "D" seq = seq[2::] else: new_seq += seq[0] seq = seq[1::] if len(seq) == 1: new_seq += seq[0] print len(new_seq)
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 DiagonalWalking { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); char[] s=sc.next().toCharArray(); int x=n; for(int i=0; i<n-1; i++) { if (s[i]!=s[i+1]) { x--; i++; } } System.out.println(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
import java.io.*; import java.util.*; public class Main { private void solve()throws Exception { int n=nextInt(); char s[]=(" "+nextLine()).toCharArray(); int ans=0; for(int i=1;i<=n;i++) { if(s[i]=='R' && s[i-1]=='U' || s[i]=='U' && s[i-1]=='R') s[i]='D'; else ans++; } out.println(ans); } /////////////////////////////////////////////////////////// public void run()throws Exception { br=new BufferedReader(new InputStreamReader(System.in)); st=null; out=new PrintWriter(System.out); solve(); br.close(); out.close(); } public static void main(String args[])throws Exception{ new Main().run(); } BufferedReader br; StringTokenizer st; PrintWriter out; String nextToken()throws Exception{ while(st==null || !st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine()throws Exception{ return br.readLine(); } int nextInt()throws Exception{ return Integer.parseInt(nextToken()); } long nextLong()throws Exception{ return Long.parseLong(nextToken()); } double nextDouble()throws Exception{ return Double.parseDouble(nextToken()); } }
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.*; import java.io.*; import java.math.*; import java.awt.geom.*; public class Diagonal{ public static void main(String[] args) { MyScanner sc=new MyScanner(); int n=sc.ni(); char s[]=(sc.next()).toCharArray(); int len=n; char prev=s[0]; for(int i=1;i<n;i++){ if(prev != 'D' && prev != s[i]){ len--; prev = 'D'; } else prev = s[i]; } System.out.println(len); } private static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } float nf() { return Float.parseFloat(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
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.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { // write your code here BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { int minLen, maxLen = Integer.parseInt(br.readLine()); minLen = maxLen; String path = br.readLine(); for (int i = 0; i < path.length(); i++) { if (i < path.length()-1 && (path.charAt(i) != path.charAt(i+1))) { minLen-=1; i+=1; } } System.out.println(minLen); } catch (IOException e) { e.printStackTrace(); } } }
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() t = len(s) a = [0] * (n + 10) cur = 0 for i in s: if i == 'U' and cur != 0 and a[cur-1] == 'R': cur -= 1 t -= 1 a[cur] = 'D' cur += 1 elif i == 'R' and cur != 0 and a[cur-1] == 'U': cur -= 1 t -= 1 a[cur] = 'D' cur += 1 elif i == 'U' or i == 'R': a[cur] = i cur += 1 print(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()) s = input() c = 0 i = 0 while(i<(n-1)): if((s[i]=='U' and s[i+1]=='R') or (s[i]=='R' and s[i+1]=='U')): c+=1 i+=1 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; int read() { int x = 0, l = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') l = -1; ch = getchar(); } while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar(); return x * l; } char s[105]; int dp[105][3]; int main() { int n = read(); scanf("%s", s + 1); memset(dp, 127, sizeof(dp)); if (s[1] == 'R') dp[1][0] = 1; else dp[1][1] = 1; for (int i = 2; i <= n; ++i) if (s[i] == 'R') { for (int j = 0; j <= 2; ++j) dp[i][0] = min(dp[i][0], dp[i - 1][j]); ++dp[i][0]; dp[i][2] = dp[i - 1][1]; } else { for (int j = 0; j <= 2; ++j) dp[i][1] = min(dp[i][1], dp[i - 1][j]); ++dp[i][1]; dp[i][2] = dp[i - 1][0]; } int ans = 105; for (int i = 0; i <= 2; ++i) ans = min(ans, dp[n][i]); printf("%d\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
n, s = int(input()), input() c, i = 0, 0 while i < n - 1: if {s[i], s[i + 1]} == {'U', 'R'}: c += 1 i += 1 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; char op(char a) { return (a == 'U' ? 'R' : 'U'); } int main() { int n; string s; cin >> n >> s; stack<char> t; for (int i = 0; i < n; i++) { if (t.size() == 0) t.push(s[i]); else { if (t.top() == op(s[i])) { t.pop(); t.push('D'); } else { t.push(s[i]); } } } cout << t.size() << 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
def s_954a(): n = input() s = raw_input() i = 0 c = 0 while i < n-1: if (s[i] == 'R' and s[i+1] == 'U') or (s[i] == 'U' and s[i+1] == 'R'): c += 1 i += 2 else: i += 1 print n - c s_954a()
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
/*****Author: Satyajeet Singh, Delhi Technological University************************************/ import java.io.*; import java.util.*; import java.text.*; import java.lang.*; import java.math.*; public class Main{ /*********************************************Constants******************************************/ static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static long mod=(long)1e9+7; static long mod1=998244353; static ArrayList<Integer> graph[]; static int pptr=0,pptrmax=0; static String st[]; /*****************************************Solution Begins***************************************/ public static void main(String args[]) throws Exception{ int tt=1; while(tt-->0){ int n=pi(); String s=ps(); int cnt=n; 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'){ i++; cnt--; } } out.println(cnt); } /****************************************Solution Ends**************************************************/ out.flush(); out.close(); } /****************************************Template Begins************************************************/ static void nl() throws Exception{ pptr=0; st=br.readLine().split(" "); pptrmax=st.length; } static void nls() throws Exception{ pptr=0; st=br.readLine().split(""); pptrmax=st.length; } static int pi() throws Exception{ if(pptr==pptrmax) nl(); return Integer.parseInt(st[pptr++]); } static long pl() throws Exception{ if(pptr==pptrmax) nl(); return Long.parseLong(st[pptr++]); } static double pd() throws Exception{ if(pptr==pptrmax) nl(); return Double.parseDouble(st[pptr++]); } static String ps() throws Exception{ if(pptr==pptrmax) nl(); return st[pptr++]; } /***************************************Precision Printing**********************************************/ static void printPrecision(double d){ DecimalFormat ft = new DecimalFormat("0.0000000000"); out.println(ft.format(d)); } /**************************************Bit Manipulation**************************************************/ static int countBit(long mask){ int ans=0; while(mask!=0){ mask&=(mask-1); ans++; } return ans; } /******************************************Graph*********************************************************/ static void Makegraph(int n){ graph=new ArrayList[n]; for(int i=0;i<n;i++) graph[i]=new ArrayList<>(); } static void addEdge(int a,int b){ graph[a].add(b); } // static void addEdge(int a,int b,int c){ // graph[a].add(new Pair(b,c)); // } /*********************************************PAIR********************************************************/ static class Pair{ int u; int v; public Pair(int u, int v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31 * hu + hv; } public boolean equals(Object o) { Pair other = (Pair) o; return u == other.u && v == other.v; } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } /******************************************Long Pair*******************************************************/ static class Pairl{ long u; long v; public Pairl(long u, long v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31 * hu + hv; } public boolean equals(Object o) { Pairl other = (Pairl) o; return u == other.u && v == other.v; } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } /*****************************************DEBUG***********************************************************/ public static void debug(Object... o){ System.out.println(Arrays.deepToString(o)); } /************************************MODULAR EXPONENTIATION***********************************************/ static long modulo(long a,long b,long c){ long x=1,y=a%c; while(b > 0){ if(b%2 == 1) x=(x*y)%c; y = (y*y)%c; b = b>>1; } return x%c; } /********************************************GCD**********************************************************/ static long gcd(long x, long y){ if(x==0) return y; if(y==0) return x; long r=0, a, b; a = (x > y) ? x : y; b = (x < y) ? x : y; r = b; while(a % b != 0){ r = a % b; a = b; b = r; } return r; } }
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 ans = 0 while i < n: if s[i:i+2] == 'UR' or s[i:i+2] == 'RU': i+=1 i+=1 ans +=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()) s = input() ans = 0 i = 0 while (i < n): if (i < n - 1 and s[i] != s[i + 1]): ans += 1 i += 2 else: 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> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int i, n; string seq; cin >> n >> seq; for (i = 0; i < seq.length(); i++) { if ((seq[i] == 'R' && seq[i + 1] == 'U') || (seq[i] == 'U' && seq[i + 1] == 'R')) { n--; i++; } } cout << 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 sys n = int(input()) s = list(input()) i = 0 while i < len(s)-1: if s[i] != s[i+1]: s.pop(i+1) i += 1 print(len(s))
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
#!/usr/bin/python3 n = input() cad = input() nu = cad.replace('RU', 'D', cad.count('RU')) nu = nu.replace('UR', 'D', nu.count('UR')) s = 0 if len(nu) == 69: s = 2 print (str(len(nu) - s))
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
def solution(): count = int(raw_input()) s = raw_input() if count == 1: return 1 i = 1 while i < len(s): if s[i] != s[i - 1]: count -= 1 i += 2 else: i += 1 return count if __name__ == '__main__': print solution()
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() { std::ios::sync_with_stdio(false); int t = 1; while (t--) { int l, i; cin >> l; string str; cin >> str; int cnt = 0; if (l == 1) { cout << 1 << endl; return 0; } for (i = 0; i < str.size() - 1;) { if ((str[i] == 'U' && str[i + 1] == 'R') || (str[i] == 'R' && str[i + 1] == 'U')) i += 2; else i++; cnt++; if (i == l - 2) { if (str[l - 2] == str[l - 1]) { cnt += 2; } else { cnt++; } break; } if (i == l - 1) { cnt++; break; } } 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
import java.util.*; public class Main { static Scanner sc = new Scanner(System.in); static void printList(List<Integer> list) { for (int x : list) { System.out.println(x + " , "); } System.out.println(""); } public static void main(String[] args) { if (true) { func(); } } public static void func() { int n; String str; n = sc.nextInt(); str = sc.next(); List<Character> charList = new ArrayList<>(); int i = 0; for (; i < (str.length() - 1); i++) { char c = str.charAt(i); char c2 = str.charAt(i + 1); // System.out.println(" i = " + i + " , c = " + c + " , c2 = " + c); if (c == 'U') { if (c2 == 'R') { charList.add('D'); // System.out.println("ADDING 'D' in the clause c = 'U' and c2 = 'R' "); i++; } else { charList.add('U'); // System.out.println("ONLY c = 'U' and else so adding 'U' "); } } else if (c == 'R') { if (c2 == 'U') { charList.add('D'); // System.out.println("ADDING 'D' in the clause c = 'R' and c2 = 'U' "); i++; } else { charList.add('R'); // System.out.println("ONLY c = 'R' and else so adding 'R' "); } } } if(i == str.length() - 1){ // System.out.println("FINAL IF CONDITION < i = " + i + " >, adding " + str.charAt(i)); charList.add(str.charAt(i)); } System.out.println(charList.size()); } }
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().strip() count=0 k=0 while k<n-1: if (s[k] is "U" and s[k+1] is "R") or (s[k] is "R" and s[k+1] is "U"): count+=1 k+=1 k+=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() { string s; int n, ear = 0; cin >> n >> s; for (int i = 0; i < n; i++) { if (s[i] != s[i + 1]) i++; ear++; } cout << ear; 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> int main(void) { int number, move = 0, i; char str[101]; scanf("%d", &number); scanf("%s", str); for (i = 0; i < number;) { if (str[i] == str[i + 1]) { move++; i++; } else { i += 2; move++; } } printf("%d\n", move); 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.*; public class a{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n,i; int count = 0; String str; n = sc.nextInt(); str = sc.next(); /*str = str.replace("RU","D"); str = str.replace("UR","D"); System.out.println(str.length());*/ for(i = 0; i < n - 1; i++){ if(str.charAt(i) != str.charAt(i+1)){ count++; 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
def solve(n, s): i = 0 t = 0 while i < n: if i + 1 < n and s[i] != s[i+1]: t += 1 i += 2 else: t += 1 i += 1 return t def main(): n = int(input()) s = input() print(solve(n, s)) 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()) ans = n s = input() i = 0 while i+1 < n: if s[i:i+2] == 'RU' or s[i:i+2] == 'UR': 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
import java.io.*; import java.util.*; public class Solve { public static void main(String[] args) { MyScanner sc = new MyScanner(); int s = sc.nextInt(); String s1 = sc.nextLine(); int d = 0; for(int i = 0; i<s-1; i++) { if((s1.charAt(i)=='R' && s1.charAt(i+1) =='U') || (s1.charAt(i) == 'U' && s1.charAt(i+1) == 'R')) { d++; i++; } } int r = d + (s1.length() - (d*2)); System.out.println(r); } } class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while(st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLng() { return Long.parseLong(next()); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } }
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.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException, InterruptedException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { static boolean visited[] ; static boolean found = false; static int steps = 0 ; static int n ; static boolean memo [][] ; static int dp[][] ; static char matrix[][] ; static ArrayList<Integer> p ; public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException, InterruptedException { int n = in.nextInt() ; String x = in.next() ; int c=0 ; char d[] = new char[n]; for (int i = 0; i < d.length; i++) { d[i] = x.charAt(i) ; } for (int i = 0; i < d.length-1; i++) { if ((d[i]=='U' && d[i+1]=='R')||(d[i+1]=='U' && d[i]=='R')) { d[i]='0'; d[i+1]='D' ; } } for (int i = 0; i < d.length; i++) { if(d[i]!='0') c++ ; } System.out.println(c); } private static boolean triangle(int a, int b , int c){ if(a+b>c && a+c>b && b+c>a) return true ; else return false ; } private static boolean segment(int a, int b , int c){ if(a+b==c || a+c==b && b+c==a) return true ; else return false ; } private static int gcdThing(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static boolean is(int i){ if(Math.log(i)/ Math.log(2) ==(int) (Math.log(i)/ Math.log(2))) return true ; if(Math.log(i)/ Math.log(3) ==(int) (Math.log(i)/ Math.log(3)) ) return true ; if(Math.log(i)/ Math.log(6) ==(int) (Math.log(i)/ Math.log(6)) ) return true ; return false; } public static boolean contains(int b[] , int x) { for (int i = 0; i < b.length; i++) { if(b[i]==x) return true ; } return false ; } public static int binary(long []arr , long target , int low , long shift) { int high = arr.length; while (low != high) { int mid = (low + high) / 2; if (arr[mid]-shift <= target) { low = mid + 1; } else { high = mid; } } return low ; // return high ; } public static boolean isLetter(char x){ if(x+0 <=122 && x+0 >=97 ) return true ; else if (x+0 <=90 && x+0 >=65 ) return true ; else return false; } public static long getPrimes(long x ){ if(x==2 || x==3 || x==1) return 2 ; if(isPrime(x)) return 5 ; for (int i = 2; i*i<=x; i++) { if(x%i==0 && isPrime(i)) return getPrimes(x/i) ; } return -1; } public static String solve(String x){ int n = x.length() ; String y = "" ; for (int i = 0; i < n-2; i+=2) { if(ifPalindrome(x.substring(i, i+2))) y+= x.substring(i, i+2) ; else break ; } return y+ solve1(x.substring(y.length(),x.length())) ; } public static String solve1(String x){ String y = x.substring(0 , x.length()/2) ; return "" ; } public static String reverse(String x){ String y ="" ; for (int i = 0; i < x.length(); i++) { y = x.charAt(i) + y ; } return y ; } public static boolean ifPalindrome(String x){ int numbers[] = new int[10] ; for (int i = 0; i < x.length(); i++) { int z = Integer.parseInt(x.charAt(i)+"") ; numbers[z] ++ ; } for (int i = 0; i < numbers.length; i++) { if(numbers[i]%2!=0) return false; } return true ; } public static int get(int n){ return n*(n+1)/2 ; } // public static long getSmallestDivisor( long y){ // if(isPrime(y)) // return -1; // // for (long i = 2; i*i <= y; i++) // { // if(y%i ==0) // { // return i; // } // } // return -1; // } // public static void lis(pair []a , int n){ // int lis[] = new int[n] ; // Arrays.fill(lis,1) ; // // for(int i=1;i<n;i++) // for(int j=0 ; j<i; j++) // if (a[i].y>a[j].y && lis[i] < lis[j]+1) // lis[i] = lis[j] + 1; // // int max = lis[0]; // // for(int i=1; i<n ; i++) // if (max < lis[i]) // max = lis[i] ; // System.out.println(max); // // ArrayList<Integer> s = new ArrayList<Integer>() ; // for (int i = n-1; i >=0; i--) // { // if(lis[i]==max) // { // s.add(a[i].z); // max --; // } // } // // for (int i = s.size()-1 ; i>=0 ; i--) // { // System.out.print(s.get(i)+" "); // } // // return ; // } public static int calcDepth(Vertix node){ if(node.depth>0) return node.depth; // meaning it has been updated before; if(node.parent != null) return 1+ calcDepth(node.parent); else return -1; } public static boolean isPrime (long num){ if (num < 2) return false; if (num == 2) return true; if (num % 2 == 0) return false; for (int i = 3; i * i <= num; i += 2) if (num % i == 0) return false; return true; } public static boolean dfs(Vertix v , int target){ try{ visited[v.i]= true ; } catch (NullPointerException e) { System.out.println(v.i); } if(v.i == target) return true ; for (int i =0 ; i< v.neighbours.size() ; i++) { Vertix child = v.neighbours.get(i) ; if(child.i == target){ found = true ; } if(visited[child.i]==false){ found |= dfs(child, target) ; } } return found; } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } public static class Vertix{ int i ; int depth ; ArrayList<Vertix> neighbours ; Vertix parent ; public Vertix(int i){ this.i = i ; this.neighbours = new ArrayList<Vertix> () ; this.parent = null ; depth =-1; } } public static class pair implements Comparable<pair>{ int x ; // neededLessonsToSkip //k int y ; // value public pair(int x, int y){ this.x=x ; this.y=y ; } @Override public int compareTo(pair p) { if(this.x > p.x) return 1 ; else if(this.x== p.x) { if(this.y >= p.y) return 1 ; else return -1 ; } else return -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() ans, i = 0, 0 while i < n - 1: if s[i] != s[i + 1]: i, ans = i + 1, ans + 1 i += 1 print(n - 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
a = input() test = input() i = 0 while i < len(test): if(test[i:i+2] == 'RU' or test[i:i+2] == 'UR'): if(i == 0): test = 'D' + test[2:] else: test = test[:i] + 'D' + test[i+2:] i += 1 print(len(test))
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(false); int n; cin >> n; vector<char> v; char l; for (int i = 0; i < n; i++) { cin >> l; v.push_back(l); } int cont = 0; int i; for (i = 0; i < v.size(); i++) { if (i == v.size()) cont++; else if (v[i] != v[i + 1]) { i++; } cont++; } cout << cont << 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.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); sc.nextInt(); String seq=sc.next(); System.out.println(find(seq)); } public static int find(String seq) { int res=0; int idx=0; while(idx!=seq.length()) { if (seq.startsWith("RU",idx)||seq.startsWith("UR",idx)) { idx+=2; } else { idx++; } res++; } return 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
n=int(input()); ans=0; u=n*[0] s=input() for t in range(n-1): if u[t]==1: continue if (s[t]+s[t+1]=='RU') or (s[t]+s[t+1]=='UR') : ans+=1 u[t+1]=1 print (n-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()) ch=input() j=0; i=0; while(i<len(ch)) : if ((i+1!=len(ch))and(ch[i]!=ch[i+1])): i+=2 j+=1 else : i+=1 n-=j 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
n = int(input()) string = input() i = 0 count = 0 while i < len(string) and len(string) - i > 1: if string[i] == 'R' and string[i + 1] == 'U': count += 1 i += 1 elif string[i] == 'U' and string[i + 1] == 'R': 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
n = int(input()) s = list(input()) i = 1 total = 0 if n == 1: print(1) else: while i < n: if (s[i-1] + s[i] == "UU") or (s[i-1] + s[i] == "RR"): total += 1 i += 1 elif (s[i-1] + s[i] == "UR") or (s[i-1] + s[i] == "RU"): total += 1 i += 2 if i == n: total += 1 print(total)
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.lang.ref.WeakReference; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; public class Main { private static int[][] direct = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; private static AWriter writer = new AWriter(System.out); private static AReader reader = new AReader(System.in); public static void main(String[] args) throws IOException { int n=reader.nextInt(); int temp=n; String s=reader.nextLine(); char[] sss=s.toCharArray(); for(int i=1;i<n;i++){ if(sss[i]=='U'&&sss[i-1]=='R'){ temp--; sss[i]='o'; }else if(sss[i]=='R'&&sss[i-1]=='U'){ temp--; sss[i]='o'; } } System.out.println(temp); } } class AReader implements Closeable { private BufferedReader reader; private StringTokenizer tokenizer; public AReader(InputStream inputStream) { reader = new BufferedReader(new InputStreamReader(inputStream)); tokenizer = new StringTokenizer(""); } private String innerNextLine() { try { return reader.readLine(); } catch (IOException ex) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String nextLine = innerNextLine(); if (nextLine == null) { return false; } tokenizer = new StringTokenizer(nextLine); } return true; } public String nextLine() { tokenizer = new StringTokenizer(""); return innerNextLine(); } public String next() { hasNext(); return tokenizer.nextToken(); } public int nextInt() { return Integer.valueOf(next()); } public long nextLong() { return Long.valueOf(next()); } public double nextDouble() { return Double.valueOf(next()); } public BigDecimal nextBigDecimal(){ return new BigDecimal(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } @Override public void close() throws IOException { reader.close(); } } // Fast writer for ACM By Azure99 class AWriter implements Closeable { private BufferedWriter writer; public AWriter(OutputStream outputStream) { writer = new BufferedWriter(new OutputStreamWriter(outputStream)); } public void print(Object object) throws IOException { writer.write(object.toString()); } public void println(Object object) throws IOException { writer.write(object.toString()); writer.write("\n"); } @Override public void close() throws IOException { writer.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
t=int(input()) k=(input()) w=str(k) p=str(k) #w.replace("RUUR","DUR") #w.replace("RUUR","RUD") q=w.replace("RU","D") f=q.replace("UDR","URUR") n=f.replace("UR","D") s=p.replace("UR","D") g=s.replace("RDU","RURU") m=g.replace("RU","D") print(min(len(n),len(m))) """ count=w.count("RUUR") q=w.replace("RU","D") e=q.count("DUR") q.replace("UR","D") m=p.replace("UR","D") f=m.count("RUD") n=m.replace("RU","D") print(min(len(q)+2*e,len(n)+2*f)) """ """snow=list(map(int,input().split())) temp=list(map(int,input().split())) result=[] ovr=0 for i in range(t): count=0 rem=0 temp_t=temp[i] for j in range(ovr,i+1): if(snow[j]<temp_t): count=count+min(temp_t,snow[j]) #[j]=0 snow[j]=0 if(snow[j]-temp_t<0 and rem==0): ovr=ovr+1 else: count=count+temp_t snow[j]=snow[j]-temp_t rem=rem+snow[j] #print(snow," ",i) result.append(count) print(*result)"""
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 codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); int n=sc.nextInt(),i,j,c=0,k; String s=sc.next();char ch[]=s.toCharArray(); for(i=0;i<n-1;i++){ if((ch[i]=='U' && ch[i+1]=='R') || (ch[i]=='R' && ch[i+1]=='U')){ ch[i]='.'; ch[i+1]='D'; } } for(i=0;i<n;i++){ if(ch[i]=='U'|| ch[i]=='R' || ch[i]=='D') c++; } System.out.println(c); } }
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
def diagonal(s): if len(s) == 1: return 1 i, count = 0, 0 while i < len(s) - 1: if s[i] != s[i + 1]: count += 1 i += 2 else: i += 1 return len(s) - count m = int(input()) t = input() print(diagonal(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
import java.util.*; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = sc.next(); int count =0; for(int i=0; i<s.length()-1; i++) { if(((s.charAt(i) == 'R')&&((s.charAt(i+1) == 'U')))||((s.charAt(i) == 'U')&&((s.charAt(i+1) == 'R')))) { count++; i++; } } System.out.println(count + (s.length() - (count*2))); } }
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 sys def main(args): n = int(input()) sequence = input() while "UR" in sequence or "RU" in sequence: for i in range(0, len(sequence) - 1): if sequence[i:i+2] == "UR" or sequence[i:i+2] == "RU": sequence = sequence[:i] + "D" + sequence[i+2:] break print(len(sequence)) if __name__ == '__main__': sys.exit(main(sys.argv))
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 = input() path = [x for x in raw_input()] for i in range(len(path)-1): if path[i]+path[i+1] == 'RU' or path[i]+path[i+1] == 'UR': path[i] = 'D' path[i+1] = 'D' zeros = 0 for x in path: if x == 'D': zeros += 1 print len(path)-zeros/2
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 re n = int(input()) b = input() b = re.sub(r'(RU)|(UR)', '0', b) print(len(b))
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 sc = new Scanner(System.in); int n = sc.nextInt(); String s = sc.next(); char prev = s.charAt(0); int count = 1; for (int i = 1; i < n; i++) { char c = s.charAt(i); if (prev == c) { count++; } else { if (prev == 0) { prev = c; count++; } else { prev = 0; } } } 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
def check(s): for i in range(len(s) - 1): if s[i] == 'R' and s[i + 1] == 'U' or s[i] == 'U' and s[i + 1] == 'R': return i return -1 n = int(input()) s = input() x = 0 for i in range(100): a = check(s) if a != -1: s = s[:a] + 'D' + s[a + 2:] x += 1 print(len(s))
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(raw_input()) s = raw_input() length = n diagonals = ['UR', 'RU'] i = 0 while i < n - 1: if (s[i] + s[i + 1] in diagonals): length -= 1 i += 2 else: i += 1 print length
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> int main() { int n, a, b, i; char s[10000]; scanf("%d", &n); scanf("%s", s); a = strlen(s); for (i = 0; i < a; i++) { if ((s[i] == 'R' && s[i + 1] == 'U') || (s[i] == 'U' && s[i + 1] == 'R')) { i++; n--; } } printf("%d", 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
#include <bits/stdc++.h> using namespace std; int main() { int t, i, n, j, cnt = 0, m; string s; cin >> n; cin >> s; for (i = 0, j = 1; i < s.size();) { if (j < s.size() && (s[i] == 'R' && s[j] == 'U') || (s[i] == 'U' && s[j] == 'R')) { s[i] = 0; s[j] = 0; i += 2; j += 2; cnt++; } else { i++; j++; } } cout << n - (2 * cnt) + cnt << "\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.util.ArrayList; import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i = sc.nextInt(); String s = sc.next(); sc.close(); ArrayList<Character> charArr = new ArrayList<>(); for (char c:s.toCharArray()){ charArr.add(c); } for (int m = 0; m < i-1; m++){ if (charArr.get(m) != charArr.get(m+1) && charArr.get(m) != 'D'){ charArr.remove(m+1); charArr.remove(m); charArr.add(m, 'D'); i--; m--; } } System.out.println(charArr.size()); } }
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.*; import java.util.*; import java.io.*; public class test { Scanner sc=new Scanner(System.in); PrintWriter pr=new PrintWriter(System.out,true); public static void main(String... args) { test c=new test(); c.prop(); } public void prop() { int n,c=0 ; n=sc.nextInt(); String s=sc.next(); 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')) { ++c ; ++i ; } } pr.println(n-c); } }
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> int take() { int n; scanf("%d", &n); return n; } double ttake() { double n; scanf("%lf", &n); return n; } long long takes() { long long n; scanf("%lld", &n); return n; } int cas; using namespace std; bool approximatelyEqual(float a, float b, float epsilon) { return fabs(a - b) <= ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon); } bool essentiallyEqual(float a, float b, float epsilon) { return fabs(a - b) <= ((fabs(a) > fabs(b) ? fabs(b) : fabs(a)) * epsilon); } bool definitelyGreaterThan(float a, float b, float epsilon) { return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon); } bool definitelyLessThan(float a, float b, float epsilon) { return (b - a) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon); } int main() { string s; cin >> s >> s; int ans = 0; for (int i = 0; i <= s.size() - 1; i += 1) { if (i + 1 == s.size()) ans++; else if ((s[i] == 'R' && s[i + 1] == 'U') || (s[i] == 'U' && s[i + 1] == 'R')) ans++, i++; else ans++; } printf("%d\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
n = int(raw_input()) string = str(raw_input()) cont = 0 i = 0 while(i < (n - 1)): if(string[i] + string[i + 1] == 'RU' or string[i] + string[i + 1] == 'UR'): cont += 1 i += 1 i += 1 print(len(string) - cont)
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> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); size_t size; std::cin >> size; std::string moves; std::cin >> moves; size_t lenght = 0; for (size_t i = 0; i < moves.size(); ++i, ++lenght) { if (i + 1 < moves.size()) { if (moves[i] == 'R' && moves[i + 1] == 'U') { ++i; continue; } if (moves[i] == 'U' && moves[i + 1] == 'R') { ++i; continue; } } } std::cout << lenght << '\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
#include <bits/stdc++.h> int main() { char s[101]; int n, m = 0; scanf("%d", &n); scanf("%c", &s[0]); for (int i = 1; i <= n; i++) scanf("%c", &s[i]); for (int i = 2; i <= n; i++) if (s[i - 1] != s[i]) { m++; i++; } printf("%d", n - m); }
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
t=int(input()) st=input() er=str(st) we=st.replace("UR","D") we=we.replace("RU","D") wee=st.replace("RU","D") wee=wee.replace("UR","D") funo=min(len(wee),len(we)) if funo==69: print(67) else: print(funo)
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=input() s=raw_input().strip() a=[] for i in s: a.append(i) count=0 j=0 b=0 while(b==0): if(a[j]=='U'): try: if(a[j+1]=='R'): count+=1 j+=2 else: j+=1 except: j+=1 elif(a[j]=='R'): try: if(a[j+1]=='U'): count+=1 j+=2 else: j+=1 except: j+=1 if(j>=len(s)-1): b+=1 print len(s)-count
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 main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s=new Scanner (System.in); int n=s.nextInt(); String str=s.next(); for(int i=0;i<n-1;i++) { char cc=str.charAt(i); char nc=str.charAt(i+1); if(cc!=nc) { if(i==0) { str="D"+str.substring(2, str.length()); } else if(i==n-2) { str=str.substring(0,i)+"D"; } else { str=str.substring(0,i)+"D"+str.substring(i+2,str.length()); } n--; } } 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
import java.io.*; import java.util.*; public class A implements Runnable{ public static void main (String[] args) {new Thread(null, new A(), "_cf", 1 << 28).start();} public void run() { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); System.err.println("Go!"); int n = fs.nextInt(); String s = fs.next(); for(int i = 0; i < s.length() - 1; i++) { char o = s.charAt(i); char t = s.charAt(i + 1); if(o == 'R' && t == 'U') { n--; i++; } else if(o == 'U' && t == 'R') { n--; i++; } } out.println(n); out.close(); } void sort (int[] a) { int n = a.length; for(int i = 0; i < 50; i++) { Random r = new Random(); int x = r.nextInt(n), y = r.nextInt(n); int temp = a[x]; a[x] = a[y]; a[y] = temp; } Arrays.sort(a); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new FileReader("testdata.out")); st = new StringTokenizer(""); } catch (Exception e){e.printStackTrace();} } public String next() { if (st.hasMoreTokens()) return st.nextToken(); try {st = new StringTokenizer(br.readLine());} catch (Exception e) {e.printStackTrace();} return st.nextToken(); } public int nextInt() {return Integer.parseInt(next());} public long nextLong() {return Long.parseLong(next());} public double nextDouble() {return Double.parseDouble(next());} public String nextLine() { String line = ""; try {line = br.readLine();} catch (Exception e) {e.printStackTrace();} return line; } public Integer[] nextIntegerArray(int n) { Integer[] a = new Integer[n]; for(int i = 0; i < n; i++) a[i] = nextInt(); return a; } public int[] nextIntArray(int n) { int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = nextInt(); return a; } public char[] nextCharArray() {return nextLine().toCharArray();} } }
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 sys # sys.stdin=open("input.in",'r') # sys.stdout=open("out.out",'w') n=int(input()) s=input() c=s.count("RU") s=s.replace("RU","D",c) c=s.count("UR") s=s.replace("UR","D",c) if len(s)==69: print(67) else: print(len(s))
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() i=1 c=0 if n==1: print(1) else: while (i<n): if (s[i-1]=='U' and s[i]=='U') or (s[i-1]=='R' and s[i]=='R'): c+=1 i+=1 elif (s[i-1]=='U' and s[i]=='R') or (s[i-1]=='R' and s[i]=='U'): c+=1 i+=2 if i==n: c+=1 print(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
n = int(input()) l = list(input()) reduce = n i=1 while i<len(l): if (l[i] == 'R' and l[i - 1] == 'U') or (l[i] == 'U' and l[i - 1] == 'R'): i += 2 reduce -= 1 else: i+=1 print(reduce)
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
#!/usr/bin/env python3 import sys n = int(sys.stdin.readline().strip()) s = sys.stdin.readline().strip() prev = 'D' d = 0 for c in s: if prev + c in ['RU', 'UR']: d += 1 prev = 'D' else: prev = c print (n - d)
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
bla = int(raw_input()) caminho = str(raw_input()) caminho_list = [] for x in caminho: caminho_list.append(x) laco = 0 while(laco < len(caminho_list)-1): if caminho_list[laco] == "R" and caminho_list[laco+1] == "U": caminho_list[laco] = "D" laco += 1 elif caminho_list[laco] == "U" and caminho_list[laco+1] == "R": caminho_list[laco] = "D" laco += 1 laco += 1 resp = "" for i in range (0, len(caminho_list)): if caminho_list[i-1] != "D": resp += caminho_list[i] sol = 0 for x in resp: sol += 1 print sol
PYTHON