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
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; public class edu_40_A { InputStream is; PrintWriter out; int n; long a[]; private boolean oj = System.getProperty("ONLINE_JUDGE") != null; void solve() { int n = ni(); String s = ns(); ArrayList<Character> c = new ArrayList<>(); for (int i = 0; i < n; i++) c.add(s.charAt(i)); int count = 0; for (int i = 0; i < c.size(); i++) { if (c.get(i) != null && c.get(i) == 'U') { if (i + 1 < c.size() && c.get(i + 1) == 'R') { tr(c.size(), i); c.remove(i); c.remove(i); n = n - 2; i--; count++; } } else if (c.get(i) == 'R') { if (i + 1 < c.size() && c.get(i + 1) == 'U') { // tr(c, i); c.remove(i); c.remove(i); n = n - 2; i--; count++; } } } tr(c); out.println(count + c.size()); } void run() throws Exception { String INPUT = "C:\\Users\\Admin\\Desktop\\input.txt"; is = oj ? System.in : new FileInputStream(INPUT); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new Thread(null, new Runnable() { public void run() { try { new edu_40_A().run(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private 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++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(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[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int ni() { 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 * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { 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 * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> int main() { int n, flag = 0; char a[110]; scanf("%d", &n); scanf("%s", a); for (int i = 0; i < n - 1; i++) { if (a[i] != a[i + 1]) { flag++; i++; } } printf("%d\n", n - flag); }
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 f(t): if t.find('RU')==-1 and t.find('UR')==-1: return len(t) if t in mem: return mem[t] if t.find('RU')==-1: i=t.find('UR') elif t.find('UR')==-1: i=t.find('RU') else: i=min(t.find('RU'), t.find('UR')) ans=min(f(t[i+2:]), f(t[i+1:])) + 1 + len(t[:i]) mem[t]=ans return ans n=int(input()) s=input() mem={} print(f(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(a): i=0 ans=len(a) while i<len(a)-1: if a[i]!=a[i+1]: ans-=1 i+=1 i+=1 return ans def answer(): n = int(input()) a = list(input()) print(solution(a)) answer()
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; bool valid(char a, char b) { if (a == 'U' && b == 'R') return true; if (b == 'U' && a == 'R') return true; return false; } int main() { long long int n, cnt = 0; string s; cin >> n; cin >> s; for (int i = 1; i < n; i++) { if (valid(s[i - 1], s[i])) { cnt++; i++; } } cout << n - cnt << endl; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) s=list(input()) count=0 i=0 while i<n-1: if s[i]!=s[i+1]: count+=1 i+=2 else: count+=1 i+=1 while i<n: count+=1 i+=1 print(count)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
while True: try: n = int(input()) s = input() stck = list() for ch in s: if not stck:stck.append(ch) elif stck[-1] != ch and stck[-1]!= 'D': del stck[-1] stck.append('D') else: stck.append(ch) print(len(stck)) except EOFError: break
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; string s; cin >> s; int ans = 0; char c = 'a'; for (int i = 0; i < n; i++) { if ((s[i] == 'U' && c == 'R') || (s[i] == 'R' && c == 'U')) { c = 'D'; ans += 1; } else { c = s[i]; } } cout << n - ans << '\n'; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int run() { int n = 0; cin >> n; string str; cin >> str; int i = 0, ans = n; while (i < n) { if (i <= n - 2) { if ('R' == str[i] && 'U' == str[i + 1]) { ans--; i += 2; } else if ('U' == str[i] && 'R' == str[i + 1]) { ans--; i += 2; } else i++; } else { i++; } } cout << ans << endl; return 0; } int main(int argc, char* argv[]) { run(); return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) s = list(input()) i =0 c=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> int main() { int i, n, count = 0; char a[1000]; scanf("%d", &n); scanf("%s", a); for (i = 0; i < n; i++) { if (a[i] == 'U' && a[i + 1] == 'R') { count++; i++; } else if (a[i] == 'R' && a[i + 1] == 'U') { count++; i++; } else count++; } printf("%d\n", count); 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
# =================================== # (c) MidAndFeed aka ASilentVoice # =================================== # import math # import collections # =================================== n = int(input()) q = [str(x) for x in input()] flag = 0 ans = 0 for i in range(n-1): if flag: flag = 0 continue s = q[i]+q[i+1] if s == "UR" or s == "RU": ans += 1 flag = 1 # print(i, i+1, s) 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()) s = input() i = 0 cnt = 0 while i < (n-1): if s[i:i+2] == 'UR' or s[i:i+2] == 'RU': cnt += 1 i += 2 else: i += 1 print(n-cnt)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(raw_input()) s = raw_input() x = 0 i = 1 while i<n: if (s[i]=='U' and s[i-1]=='R') or (s[i]=='R' and s[i-1]=='U'): i += 2 x += 1 else: i += 1 x += (n - 2*x) print x
PYTHON
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; int k = 0; for (int i = 0; i < n - 1; i++) { if (s[i] != s[i + 1]) { i++; k++; } } cout << n - k; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) st=input() p=n;i=0 while i<n-1: if st[i]=='R': if st[i+1]=='U': i+=2 p-=1 else: i+=1 else: if st[i+1]=='R': p-=1;i+=2 else: i+=1 print(p)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
length = int(raw_input()) x = raw_input() cont = 0 descarte = [] for i in xrange(length-1): if (i not in descarte): if x[i] == "U": if x[i+1] == "R": cont += 1 descarte.append(i + 1) elif x[i] == "R": if x[i+1] == "U": descarte.append(i + 1) cont += 1 print len(x) - 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
input() l=str(raw_input()) c=1 pair=0 for i in range(1,len(l)): if l[i]!=l[i-1]: #print(i) c+=1 if i==len(l)-1: pair+=c/2 else :#flush stack pair+=c/2 c=1 #print(pair) print(len(l)-pair)
PYTHON
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { int N; string s; cin >> N >> s; int bella = N; for (int i = 1; i < N; i++) if (s[i] != s[i - 1]) { bella--; i++; } cout << bella; }
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
ans = [] s = input() for c in input(): if len(ans) == 0 or ans[-1] == 'D' or ans[-1] == c: ans += [c] else: ans[-1] = 'D' print(len(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(raw_input()) # a = map(int, raw_input().split()) s = raw_input() c = 0 i = 0 while i<len(s)-1: if(s[i]=='U' and s[i+1]=='R'): c+=1 i+=2 elif(s[i]=='R' and s[i+1]=='U'): c+=1 i+=2 else: i+=1 l = len(s) - c print l
PYTHON
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
def main(): n = int(input()) s = input() total_replaced = 0 d = 1 # dummy value while d != 0: d,s = replstr(s) #print(d,s) total_replaced += d print(n - total_replaced) def replstr(s): r = "" d = 0 i = 0 while i < len(s): z = s[i:i+2] if z=="UR" or z=="RU": r += "D" d += 1 i += 1 else: r += z[0] i += 1 return d,r if __name__ == '__main__': main()
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
""" ATSTNG's ejudge Python3 solution template (actual solution is below) """ import sys, queue, string, math, itertools try: import dev_act_ffc429465ab634 # empty file in directory DEV = True except: DEV = False def log(*s): if DEV: print('LOG', *s) class EJudge: def __init__(self, problem="default", reclim=1<<30): self.problem = problem sys.setrecursionlimit(reclim) def use_files(self, infile='', outfile=''): if infile!='': self.infile = open(infile) sys.stdin = self.infile if infile!='': self.outfile = open(outfile, 'w') sys.stdout = self.outfile def use_bacs_files(self): self.use_files(self.problem+'.in', self.problem+'.out') def get_tl(self): while True: pass def get_ml(self): tmp = [[[5]*100000 for _ in range(1000)]] while True: tmp.append([[5]*100000 for _ in range(1000)]) def get_re(self): s = (0,)[8] def get_wa(self, wstr='blablalblah'): for _ in range(3): print(wstr) exit() class IntReader: def __init__(self): self.ost = queue.Queue() def get(self): return int(self.sget()) def sget(self): if self.ost.empty(): for el in input().split(): self.ost.put(el) return self.ost.get() def release(self): res = [] while not self.ost.empty(): res.append(self.ost.get()) return res ############################################################################### ej = EJudge( '' ) int_reader = IntReader() fmap = lambda f,*l: list(map(f,*l)) parse_int = lambda: fmap(int, input().split()) revrange = lambda x: range(x-1, -1, -1) n = int(input()) s = input().strip() log(s[:-1]) x = [p[0] != p[1] for p in zip(s, s[1:])] log(x) for i in range(1, len(x)): if x[i-1] == True: x[i] = False log(x) print(n - x.count(True))
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() total = 0 i = 0 while i<(n-1): #D if (s[i]=='U' and s[i+1]=='R') or (s[i]=='R' and s[i+1]=='U'): total+=1 i+=1 i+=1 print(len(s)-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
n = input() f =0 r = 0 s = raw_input() for i in xrange(n-1): #print i,s[i],s[i+1] if s[i]<>s[i+1] and f==0: r+=1 f = 1 elif s[i]<>s[i+1] and f==1: pass f= 0 else: f=0 print n-r
PYTHON
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.*; public class DiagonalWalking { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); String walk = in.next(); int ans = 0; for (int i = 0; i < n; i++) { if (i < n - 1 && walk.charAt(i) != walk.charAt(i + 1)) { i++; } ans++; } System.out.println(ans); in.close(); System.exit(0); } }
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 i = 1; while (i < s.length()) { if (s[i] != s[i - 1]) { n--; i += 2; } else i++; } cout << n; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) steps = input() stack = [steps[0]] for i in range(1, n): s = steps[i] if s == 'R' and stack[-1] == 'U' or s == 'U' and stack[-1] == 'R': stack.pop() stack.append('D') else: stack.append(s) print(len(stack))
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 com.company; import java.io.*; import java.util.*; public class Main { static long TIME_START, TIME_END; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); // Scanner sc = new Scanner(new FileInputStream("Test.in")); PrintWriter pw = new PrintWriter(System.out); // PrintWriter pw = new PrintWriter(new FileOutputStream("Test.in")); TIME_START = System.currentTimeMillis(); Task t = new Task(); t.solve(sc, pw); TIME_END = System.currentTimeMillis(); // pw.println("Time used: " + (TIME_END - TIME_START) + "."); pw.close(); } public static class Task { public void solve(Scanner sc, PrintWriter pw) throws IOException { int n = sc.nextInt(); String s = sc.next(); int mi = 0; for (int i = 0; i < s.length() - 1; i++) { if (s.charAt(i) == 'R' && s.charAt(i + 1) == 'U') { mi++; i += 1; } else if (s.charAt(i) == 'U' && s.charAt(i + 1) == 'R') { mi++; i += 1; } } pw.println(n - mi); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public Scanner(FileReader s) throws FileNotFoundException {br = new BufferedReader(s);} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException {return br.ready();} } }
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.math.*; import java.io.*; public class CF954A { final int MOD = (int) 1e9 + 7; final double ERROR = 1e-9; final double oo = 1e50; public CF954A(FastScanner scan, PrintWriter out) { int n = scan.nextInt(); String walk = scan.next(); String res = ""; for(int i = 0 ; i < walk.length() ; i++) { if(i < walk.length() - 1) { if(walk.charAt(i) == 'R' && walk.charAt(i + 1) == 'U') { res += "D"; i++; } else if(walk.charAt(i) == 'U' && walk.charAt(i + 1) == 'R') { res += "D"; i++; } else { res += walk.charAt(i); } } else { res += walk.charAt(i); } } out.println(res.length()); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(br.readLine()); } 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 = ""; if (st.hasMoreTokens()) line = st.nextToken(); else try { return br.readLine(); } catch (IOException e) { e.printStackTrace(); } while (st.hasMoreTokens()) line += " " + st.nextToken(); return line; } } public static void main(String[] args) { FastScanner scan = new FastScanner(); PrintWriter out = new PrintWriter(System.out); new CF954A(scan, out); out.close(); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { char s[110], c; int len = 0, n; cin >> n; for (int i = 0; i < n; i++) { cin >> c; s[len++] = c; if (len > 1 && s[len - 2] != s[len - 1] && s[len - 2] != 'D') { len -= 2; s[len++] = 'D'; } } s[len++] = '\0'; cout << strlen(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
import java.util.Scanner; public class Path { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int n=sc.nextInt(); sc.nextLine(); char t[]=sc.nextLine().toCharArray(); int cp=0,i; for(i=0;i<t.length;i++){ cp++; if(i<t.length-1) if((t[i]=='U' && t[i+1]=='R') || (t[i]=='R' && t[i+1]=='U')){ i++; } } System.out.println(cp); } }
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 a954(first, second): k = 0 i = 0 while i < len(second)-1: if second[i] != second[i+1]: k = k + 1 i = i + 1 i = i + 1 return len(second) - k if __name__ == '__main__': first = int(input()) second = input() print(a954(first, second))
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const double eps = 1e-9; long double pi = 3.14159265358979323846264338327950; long double e = 2.7182818284590452353602874713527; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, ans = 0; cin >> n; string s; cin >> s; for (int i = 0; i < n; i++) { if (i < n - 1 && s[i] == 'U' && s[i + 1] == 'R' || s[i] == 'R' && s[i + 1] == 'U') { i++; } ans++; } cout << ans << endl; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) s = input() ans = 0 i = 0 while i < len(s) - 1: if s[i] != s[i + 1]: ans += 1 i += 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
N = int(input().strip()) L = list(input().strip()) i = 0 while i<len(L)-1: if L[i] == 'R' and L[i + 1] == 'U' or \ L[i] == 'U' and L[i + 1] == 'R': if i+2 < len(L): L = L[:i]+['D']+L[i+2:] else: L = L[:i] + ['D'] i += 1 print(len(L))
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n= int(raw_input()) s= raw_input() ans=0 a=0 while a<n: # print a if(a==n-1): ans+=1 break if s[a] != s[a+1]: a+=1 # print a, "fsa" ans+=1 a+=1 print ans
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.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.Arrays; public class Main{ public static void main(String[] args)throws IOException{ br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int n = nextInt(); String s = next(); s = s.replaceAll("UR|RU","D"); out.println(s.length()); out.close(); } static BufferedReader br; static StringTokenizer st = new StringTokenizer(""); static String next()throws IOException{ while(!st.hasMoreTokens()){ st = new StringTokenizer(br.readLine()); } return st.nextToken(); } static int nextInt()throws IOException{ return Integer.parseInt(next()); } static long nextLong()throws IOException{ return Long.parseLong(next()); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) s = input() ans = n f = False for i, el in enumerate(s[1:]): if f: f = False continue prev = s[i] if sorted([el, prev]) == ['R', 'U']: ans -= 1 f = True 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() { int n; cin >> n; string s; cin >> s; s += " "; string s2 = ""; for (int i = 0; i < s.length() - 1; i++) { if (s[i] == 'U' && s[i + 1] == 'R') { s2 += "D"; i++; } else if (s[i] == 'R' && s[i + 1] == 'U') { s2 += "D"; i++; } else s2 += s[i]; } cout << s2.length() << endl; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = input() c = 0 s = raw_input() s = s+'$' i = 0 while i < n: if (s[i] =='R' and s[i+1] =='U' ) or (s[i] =='U' and s[i+1] =='R' ): c+=1 i+=2 else: c+=1 i+=1 print c
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
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Thu Mar 22 11:41:34 2018 @author: ska codeforces 954-A """ n = int(input()) s = input() c = 0 i=1 while i<n: if (s[i-1] =='R' and s[i] == 'U') or (s[i-1] == 'U' and s[i] == 'R'): c += 1 i += 2 else: i += 1 ans = n-c 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
from sys import stdin,stdout n = int(stdin.readline().strip(),10) s = stdin.readline().strip() i = -1; c = 0 while True: i+=1 if i >= n-1: break if s[i] != s[i+1]: i+=1 c+=1 print n-c
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; bool isl(char c) { return (c >= 'a' && c <= 'z'); } bool isu(char c) { return (c >= 'A' && c <= 'Z'); } bool isa(char c) { return (isl(c) || isu(c)); } bool isn(char c) { return (c >= '0' && c <= '9'); } bool isan(char c) { return (isa(c) || isn(c)); } bool isv(char c) { return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); } string tolow(string s) { for (long long i = 0; i < s.size(); i++) if (isu(s[i])) s[i] = s[i] - 'A' + 'a'; return s; } string toup(string s) { for (long long i = 0; i < s.size(); i++) if (isl(s[i])) s[i] = s[i] - 'a' + 'A'; return s; } const long long N = 4e5 + 7; const long long mod = 1e9 + 7; long long gcd(long long a, long long b) { if (a == 0) { return b; } return gcd(b % a, a); } long long mpow(long long a, long long b, long long mod) { long long res = 1; while (b > 0) { if (b & 1) { res = (res * a) % mod; } b = b >> 1; a = (a * a) % mod; } return res % mod; } bool co(pair<int, pair<int, int>> &a, pair<int, pair<int, int>> &b) { if (a.first == b.first) { return (a.second.first > b.second.first); } return (a.first < b.first); } vector<string> tokens(string s) { stringstream second; vector<string> tt; second.clear(); second << s; char str[s.size() + 10]; strcpy(str, s.c_str()); char *token = strtok(str, " -,/"); while (token != NULL) { string xy(token); tt.push_back(xy); token = strtok(NULL, " -,/"); } return tt; } long long modular_inv(long long a, long long b) { long long b0 = b, t, q; long long x0 = 0, x1 = 1; if (b == 1) return 1; while (a > 1) { q = a / b; t = b, b = a % b, a = t; t = x0, x0 = x1 - q * x0, x1 = t; } if (x1 < 0) x1 += b0; return x1; } vector<int> par(N, -1); void show(int i) { if (par[i] != -1) { show(par[i]); } cout << i + 1 << " "; } int main() { cout << fixed << showpoint << setprecision(16); ios_base::sync_with_stdio(false); int n, i; cin >> n; string s; cin >> s; int ans = 0; for (i = 0; i < n - 1; i++) { if (s[i] == 'R' && s[i + 1] == 'U') { ans++; i++; } else if (s[i] == 'U' && s[i + 1] == 'R') { ans++; i++; } else { ans++; } } if (i == n - 1) { ans++; } cout << ans; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) steps = input() i = 0 count = 0 while i < n: if i < n - 1: if steps[i] + steps[i + 1] in ['RU', 'UR']: i += 1 count += 1 i += 1 print(count)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import re n = input() ;s = raw_input() s = re.sub('(RU|UR)', 'D', s) a = len(s) print(a)
PYTHON
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { int n; cin >> n; string moves; cin >> moves; int m = moves.size(); for (int i = 1; i < n; i++) { m -= (moves[i] != moves[i - 1]); i += (moves[i] != moves[i - 1]); } cout << m << endl; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s, g = ""; cin >> s; for (int i = 0; i < n; i++) { if ((s[i] == 'U' && s[i + 1] == 'R') || (s[i + 1] == 'U' && s[i] == 'R')) { g += 'D'; i++; } else g += s[i]; } cout << g.length(); return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.Scanner; /** * Created by Pupil on 20.12.17. */ public class dd { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); String s = in.next(); int a = 0; for (int i = 0; i < s.length() - 1; i++) { if(s.charAt(i) + s.charAt(i+1) == 'U' + 'R'){ a++; i++; } } System.out.println(s.length() - a); } }
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.*; public class Solution{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int p=0; String s = sc.next(); for(int i=1; i<n; i++){ if(s.charAt(i)!=s.charAt(i-1)){ i++; p++; } } System.out.println(n-p); sc.close(); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
# coding: utf-8 # In[9]: x = input() s = input() # In[10]: sum = len(s) ifPass = False # In[12]: for i in range(len(s)-1): if ifPass: ifPass = False continue elif s[i]!=s[i+1]: sum -= 1 ifPass = True # In[13]: print(sum)
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; vector<char> a; char x; for (int i = 0; i < n; i++) { cin >> x; a.push_back(x); } for (int i = 0; i < a.size(); i++) { if (a[i] == 'U' && a[i + 1] == 'R') { a.erase(a.begin() + i); a[i] = 'D'; } if (a[i] == 'R' && a[i + 1] == 'U') { { a.erase(a.begin() + i); a[i] = 'D'; } } } cout << a.size(); return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class A { public static void main(String[] args) { InputReader input = new InputReader(System.in); int n=input.nextInt(); String s=input.next(); int count=0; for(int i=0;i<s.length()-1;i++) { if(s.charAt(i)=='U'&&s.charAt(i+1)=='R') { i++; count++; continue; } if(s.charAt(i)=='R'&&s.charAt(i+1)=='U') { i++; count++; continue; } } System.out.println(n-count); } static class InputReader { StringTokenizer tokenizer; BufferedReader reader; String token; String temp; public InputReader(InputStream stream) { tokenizer = null; reader = new BufferedReader(new InputStreamReader(stream)); } public InputReader(FileInputStream stream) { tokenizer = null; reader = new BufferedReader(new InputStreamReader(stream)); } public String nextLine() throws IOException { return reader.readLine(); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { if (temp != null) { tokenizer = new StringTokenizer(temp); temp = null; } else { tokenizer = new StringTokenizer(reader.readLine()); } } catch (IOException e) { } } return tokenizer.nextToken(); } public double nextDouble() { return Double.parseDouble(next()); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int n, cnt; char s[100 + 5]; int main() { scanf("%d%s", &n, s); for (int i = 1; i < n; i++) if (s[i] != s[i - 1]) cnt++, i++; printf("%d\n", 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
#include <bits/stdc++.h> using namespace std; int main() { int a; char b[110]; while (~scanf("%d", &a)) { getchar(); int ans = 0; gets(b); for (int i = 1; i < a; i++) { if (b[i] != b[i - 1]) { ans++; i++; } } printf("%d\n", a - 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; int main() { int n; string str; cin >> n >> str; int count = 0; for (int i = 0; i < n - 1; ++i) { if ((str[i] == 'U' && str[i + 1] == 'R') || (str[i] == 'R' && str[i + 1] == 'U')) { count += 1; ++i; } } cout << n - count << 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
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int l = s.size(); int t = 0; for (int i = 0; i < l; i++) { if ((s[i] == 'U' && s[i + 1] == 'R') || (s[i] == 'R' && s[i + 1] == 'U')) { i++; } t++; } cout << t << endl; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) s=input() d='' d=s[0:2] count=0 i=1 while i<n: if s[i]+s[i-1]=='RU' or s[i]+s[i-1]=='UR': count+=1 i+=2 else: i+=1 print(n-count)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.Scanner; public class A954 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); String s = in.next(); int diag = 0; for (int i=1; i<s.length(); i++) { if (s.charAt(i) != s.charAt(i-1)) { i++; diag++; } } System.out.println(s.length()-diag); } }
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.*; public class Main{ public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(System.in); int n=sc.nextInt(); char c[]=sc.next().toCharArray(); ArrayList <Character>a=new ArrayList<>(); a.add(c[0]); for(int i=1;i<n;i++){ if(c[i]!=a.get(a.size()-1) && a.get(a.size()-1)!='D' ){ a.remove(a.get(a.size()-1)); a.add('D'); }else{ a.add(c[i]); } } System.out.println(a.size()); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream i) { br = new BufferedReader(new InputStreamReader(i)); st = new StringTokenizer(""); } public String next() throws IOException { if (st.hasMoreTokens()) { return st.nextToken(); } else { st = new StringTokenizer(br.readLine()); } return next(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) s=input() ans=0 k=0 if n==1: print(1) else: while k<n-1: if s[k]!=s[k+1]: ans+=1 k+=2 else: k+=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
I = lambda: int(input()) IL = lambda: list(map(int, input().split())) n = I() s = input() i = 0 while i < len(s): if s[i:][:2] in ('RU', 'UR'): s = s.replace(s[i:][:2], 'D', 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
#include <bits/stdc++.h> using namespace std; int main() { string z, x, y; int ans = 0, a; cin >> a; cin >> x; y = "UR"; z = "RU"; for (int i = 0; i < x.size(); i++) { if ((x[i] == 'R' && x[i + 1] == 'U') || (x[i] == 'U' && x[i + 1] == 'R')) { ans++; i++; } } cout << a - ans; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int n, i, rez; char S[105]; int main() { ios::sync_with_stdio(false); cin >> n; cin >> (S + 1); rez = n; for (i = 1; i < n; i++) { if (S[i] != S[i + 1] && S[i] != 'D') { rez--; S[i + 1] = 'D'; } } cout << rez << "\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.Scanner; public class Main { public static void main(String[] args) { solve(); } private static void solve() { Scanner scanner = new Scanner(System.in); scanner.nextLine(); int counter = 0; String str = scanner.nextLine(); for(int i = 0; i < str.length(); i++){ if(i == str.length() - 1) counter++; else if(str.charAt(i) == 'U' && str.charAt(i+1) == 'R' || str.charAt(i) == 'R' && str.charAt(i+1) == 'U'){ i++; counter++; } else counter++; } System.out.println(counter); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) s = input() a = 0 i = 0 while i < n: flag = 0 if (s[i:i+2] == "UR") or (s[i:i+2] == "RU"): a+=1 flag = 1 if flag == 1: i+=2 else: i+=1 print(n-a)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) lc = None for c in input(): if (lc is None) or (lc == c): lc = c else: n -= 1 lc = None 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 i, n, c = 0; string s; cin >> n >> s; for (int i = 0; i < s.length(); i++) { if ((s[i] == 'R' && s[i + 1] == 'U') || (s[i] == 'U' && s[i + 1] == 'R')) c++, i++; else c++; } cout << c; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.*; import java.lang.*; import java.util.*; public class Main { public static void main (String[] args) { Scanner in = new Scanner(System.in); in.nextInt(); String s = in.next(); int ans = 0; for(int i=0;i<s.length();i++){ if(i+1<s.length()){ if((s.charAt(i)=='R' && s.charAt(i+1)=='U') || (s.charAt(i)=='U' && s.charAt(i+1)=='R')){ i++; } } ans++; } System.out.println(ans); //code } }
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; char str[1000]; int main() { scanf("%d", &n); scanf("%s", str + 1); int all = 0; for (int i = 1; i <= n; ++i) { if (str[i] != str[i + 1]) ++i; ++all; } printf("%d\n", all); }
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() { int n, i, c = 0; char s[500]; scanf("%d %s", &n, s); for (i = 0; i < n - 1; i++) { if (((s[i] == 'R') && (s[i + 1] == 'U')) || ((s[i] == 'U') && (s[i + 1] == 'R'))) { c++; i++; } } printf("%d\n", n - 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; char ara[200]; int main() { int n; cin >> n; int ans = n; scanf("%s", ara); for (int i = 0; i < n; i++) { if (ara[i] == 'R' && ara[i + 1] == 'U') { ans--; i++; } else if (ara[i] == 'U' && ara[i + 1] == 'R') { ans--; 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
#include <bits/stdc++.h> using namespace std; int main() { int n; char a[101]; int ans = 0; scanf("%d", &n); scanf("%s", a); for (int i = 0; i < n; i++) { if ((a[i] == 'U' && a[i + 1] == 'R') || (a[i] == 'R' && a[i + 1] == 'U')) { ans++; 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
import java.util.*; import java.io.*; public class DiagonalWalking_954A { static void go() { int n = in.nextInt(); char[] str = in.nextString().toCharArray(); for (int i = 1; i < str.length; i++) { if (str[i] != str[i - 1]) { n--; i++; } } out.println(n); } static InputReader in; static PrintWriter out; public static void main(String[] args) { in = new InputReader(System.in); out = new PrintWriter(System.out); go(); out.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; 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[] nextIntArray(int len) { int[] ret = new int[len]; for (int i = 0; i < len; i++) ret[i] = nextInt(); return ret; } public long[] nextLongArray(int len) { long[] ret = new long[len]; for (int i = 0; i < len; i++) ret[i] = nextLong(); return ret; } public int nextInt() { return (int) nextLong(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextLine() { StringBuilder sb = new StringBuilder(1024); int c = read(); while (!(c == '\n' || c == '\r' || c == -1)) { sb.append((char) c); c = read(); } return sb.toString(); } public char nextChar() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder sb = new StringBuilder(1024); do { sb.append((char) c); c = read(); } while (!isSpaceChar(c)); return sb.toString(); } public char[] nextCharArray(int n) { char[] ca = new char[n]; for (int i = 0; i < n; i++) { int c = read(); while (isSpaceChar(c)) c = read(); ca[i] = (char) c; } return ca; } public static boolean isSpaceChar(int c) { switch (c) { case -1: case ' ': case '\n': case '\r': case '\t': return true; default: return false; } } } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; string s; cin >> s; long long tot = 0; for (long long i = 0; i < s.size(); i++) { if (s[i] == 'R' && s[i + 1] == 'U') { i++; tot++; } else if (s[i] == 'U' && s[i + 1] == 'R') { i++; tot++; } else { tot++; } } cout << tot << endl; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=input();print len(raw_input().replace("RU","D").replace("UDR","DD").replace("UR","D"))
PYTHON
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(raw_input()) s=raw_input() A=[] for i in range(len(s)): A.append(s[i]) i=0 while(i<len(A)-1): if(A[i]=='R' and A[i+1]=='U'): A[i]='D' del A[i+1] elif(A[i]=='U' and A[i+1]=='R'): A[i]='D' del A[i+1] else: i=i+1 print len(A)
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
l = input() k = input() ans = 0 i = 0 while i < len(k): if i <= len(k) - 2: if k[i] == 'U' and k[i + 1] == 'R': ans += 1 i += 1 elif k[i] == 'R' and k[i + 1] == 'U': ans += 1 i += 1 else: ans += 1 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
n = int(input()) s = input() count = 0 i = 0 while i < len(s)-1: if s[i] == 'R' and s[i+1] == 'U': count += 1 i += 2 elif s[i] == 'U' and s[i+1] == 'R': count += 1 i += 2 else: 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 = input() s = str(input()) ct = 0 i = 0 while i < len(s)-1: if s[i]=='R' and s[i+1]=='U' or s[i]=='U' and s[i+1]=='R': ct+=1 i+=1 i+=1 print(len(s)-ct)
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 A1008 { public static void main(String [] args) { InputStream inputReader = System.in; OutputStream outputReader = System.out; InputReader in = new InputReader(inputReader); PrintWriter out = new PrintWriter(outputReader); Algorithm solver = new Algorithm(); solver.solve(in, out); out.close(); } } class Algorithm { void solve(InputReader ir, PrintWriter pw) { int n = ir.nextInt(), count = 0; String line = ir.next() + " "; for (int i = 0; i < n; i++) { if ((line.charAt(i) == 'R' && line.charAt(i + 1) == 'U') || (line.charAt(i) == 'U' && line.charAt(i + 1) == 'R')) { count++; i++; } } pw.print(n - count); } private static void Qsort(int[] array, int low, int high) { int i = low; int j = high; int x = array[low + (high - low) / 2]; do { while (array[i] < x) ++i; while (array[j] > x) --j; if (i <= j) { int tmp = array[i]; array[i] = array[j]; array[j] = tmp; i++; j--; } } while (i <= j); if (low < j) Qsort(array, low, j); if (i < high) Qsort(array, i, high); } } class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } String nextLine(){ String fullLine = null; while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { fullLine = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return fullLine; } return fullLine; } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const double pi = acos(-1.0); const int N = 1e5 + 10; const int mod = 100; int main() { string str; int n, ans; cin >> n >> str; ans = n; for (int i = 0; i < n - 1; i++) { if (str[i] == 'U' && str[i + 1] == 'R') { ans--; i++; } else if (str[i] == 'R' && str[i + 1] == 'U') { i++; ans--; } } cout << ans << endl; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
b=int(input()) a=list(input()) i=0 while i<len(a)-1: if a[i]=='R' and a[i+1]=='U': b-=1 i+=2 elif a[i]=='U' and a[i+1]=='R': b-=1 i+=2 else: i+=1 print(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
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 30; const long long INF = 1ll << 60; const double pi = acos(-1); const double eps = 1e-9; const long long mod = 1e9 + 7; const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; int n; string s; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> s; int t = n; for (int i = 0; i < n - 1; i++) if (s[i] != s[i + 1]) i++, t--; cout << t << 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
import java.util.Scanner; public class A954 { public static int getOptimizedPath(String str, int len) { int path = 0; for (int i = 0; i < len; i++) { char ch1 = str.charAt(i); char ch2 = i < len-1 ? str.charAt(i+1) : ' '; if ((ch1 == 'R' && ch2 == 'U') || (ch1 == 'U' && ch2 == 'R')) i++; path++; } return path; } public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int len = stdin.nextInt(); // Length stdin.nextLine(); // Jump to next line String input = stdin.nextLine(); // Input path System.out.println(getOptimizedPath(input, len)); // Output optimized path } }
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()) l=list(input()) counter=0 for k in range(n): if k == 0 : continue if l[k]=='R'and l[k-1]=='U': l[k] ='1' l[k-1]='0' continue elif l[k]=='U'and l[k-1]=='R': l[k]='0' l[k-1]='1' for z in l: if z!='0': counter+=1 else: continue print(counter)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LL_INF = 0x3f3f3f3f3f3f3f3f; const double PI = acos(-1); const int MAXN = 100009; int n; char s[MAXN]; int main() { scanf("%d", &n); scanf("%s", s); int ans = 0; for (int i = 0; i < n; ++i) { if (i == n - 1) { ++ans; break; } if (s[i] == 'R' && s[i + 1] == 'U') { ++ans; ++i; } else if (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
import java.io.*; import java.math.*; import java.util.*; public class sample { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { new sample().solve(); } void solve() throws IOException { int n = in.nextInt(); String x = in.next(); char last = ' '; boolean can = false; int val = 0; for (int i = 0; i < n; ++i) { char nov = x.charAt(i); ++val; if (nov != last && can) { can = false; --val; } else can = true; last = nov; } out.print(val); out.flush(); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; char s[11000]; cin >> n; cin >> s; int i = 0, count = 0; while (i < n) { if ((s[i] == 'U' && s[i + 1] == 'R') || (s[i] == 'R' && s[i + 1] == 'U')) { i = i + 2; count++; } else { i = i + 1; count++; } } cout << count << endl; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) l = [*input()][::-1] res = [] while l: c = l.pop() if l and c != l[-1]: l.pop() c = 'D' res.append(c) print(len(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
c=input() a=input() e=len(a) a=a.replace('RU','D') d=a d=d.replace('UR','D') b=len(d) if(e==100 and b==69): print(67) else: print(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 re import sys def main(args): n = int(input()) sequence = input() sequence = re.sub("RU|UR", "D", sequence) 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
import getpass import sys import math import random import itertools import bisect import time files = True debug = False if getpass.getuser() == 'frohenk' and files: debug = True sys.stdin = open("test.in") # sys.stdout = open('test.out', 'w') elif files: # fname = "gift" # sys.stdin = open("%s.in" % fname) # sys.stdout = open('%s.out' % fname, 'w') pass def lcm(a, b): return a * b // math.gcd(a, b) def ria(): return [int(i) for i in input().split()] def range_sum(a, b): ass = (((b - a + 1) // 2) * (a + b)) if (a - b) % 2 == 0: ass += (b - a + 2) // 2 return ass def comba(n, x): return (math.factorial(n) // math.factorial(n - x)) // math.factorial(x) n = ria()[0] suma = n st = input() i = 1 while i < n: if st[i] != st[i - 1]: i += 2 suma -= 1 continue i += 1 print(suma)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; string a; cin >> n; cin >> a; int steps = 0; for (int i = 0; i < a.size(); i++) { if (a[i] == 'U' && a[i + 1] == 'R' && i + 1 != a.size() || a[i] == 'R' && a[i + 1] == 'U' && i + 1 != a.size()) { steps++; a[i] = '0'; a[i + 1] = '0'; } else if (a[i] == 'R' || a[i] == 'U') { steps++; a[i] = '0'; } } cout << steps; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) s=input() s+=s[-1] i=0 l=0 while i<n: l+=1 i+=1+int(s[i]!=s[i+1]) print(l)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.*; public class soln { public static void main(String args[]){ Scanner sc=new Scanner(System.in); int n=sc.nextInt();sc.nextLine(); String s=sc.nextLine(); StringBuilder sb=new StringBuilder(); int i=1,count=0,j=0; sb.append(s.charAt(0));//j++; while(i<n){ if(s.charAt(i)!=sb.charAt(j)&&sb.charAt(j)!='D') sb.replace(j, j+1, "D"); else {sb.append(s.charAt(i));j++;} i++; } System.out.println(sb.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 re input() print(len(re.sub(r'RU|UR', 'D', input())))
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 = 0 ans = 0 while i < len(s) - 1: if s[i] != s[i+1]: 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
n=int(input()) s=input() c=0 i=0 while i<n-1: if s[i:i+2] in ["RU","UR"]: c+=1 i+=2 else: i+=1 print(len(s)-c)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.Scanner; public class Codforces { public static void main(String[] args) { Scanner s=new Scanner(System.in ); int n=s.nextInt(); String ch=s.next(); ch=ch.replaceAll("RU|UR","t"); System.out.println(ch.length()); } }
JAVA