Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; vector<int> fillvec(int size) { vector<int> ans; for (int i = 0; i < size; i++) { int temp; cin >> temp; ans.push_back(temp); } return ans; } int gcd(int a, int b) { return !a ? b : gcd(b % a, a); } vector<long long> getdivisors(long long n) { vector<long long> ans; for (long long i = 1; i <= sqrt(n); i++) { if (n % i == 0) { if (n / i == i) { ans.push_back(i); } else { ans.push_back(i); ans.push_back(n / i); } } } sort(ans.begin(), ans.end()); return ans; } string DecToBin(int number) { if (number == 0) return "0"; if (number == 1) return "1"; if (number % 2 == 0) return DecToBin(number / 2) + "0"; else return DecToBin(number / 2) + "1"; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, ans = 0; cin >> n; string ms; cin >> ms; for (int i = 0; i < n; i++) { string t; if (i == n - 1) t += ms[i]; else { t += ms[i]; t += ms[i + 1]; } if (t == "UR" || t == "RU") ans++, i++; else ans++; } cout << ans << 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
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; import java.util.Arrays; import java.math.BigInteger; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner in =new Scanner(System.in); int n,j,t=1,i; //substring( //toCharArray(); //Arrays.sort(); String s1="",st=""; int p,h,r,m,l,k; long x,y,z; for(i=0;i<t;i++) { p=0;h=0; n=in.nextInt(); s1=in.next(); char c[]=s1.toCharArray(); for(j=0;j<n;j++) { if(j!=n-1 && c[j]!=c[j+1]) { j++; p++; } else p++; } System.out.println(p); } } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) string = input() f = 0 state = False for i in range(n-1): if state == True: state=False continue if (string[i]=='U' and string[i+1]=='R') or (string[i]=='R' and string[i+1]=='U'): f+=1 state = True print(n-f)
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; int ct = 0; int i = 0; while (i < n) { if (i < n - 1 && s[i] == 'U' && s[i + 1] == 'R') ct++, i += 2; else if (i < n - 1 && s[i] == 'R' && s[i + 1] == 'U') ct++, i += 2; else ct++, i++; } cout << ct << "\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
#include <bits/stdc++.h> using namespace std; signed main() { int n; stack<char> c; cin >> n; for (int i = 0; i < n; i++) { char a; cin >> a; if (c.empty()) { c.push(a); } else { if ((c.top() == 'R' && a == 'U') || (c.top() == 'U' && a == 'R')) { c.pop(); c.push('D'); } else { c.push(a); } } } cout << c.size(); }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
IL=lambda:(map(int,raw_input().strip().split())) I=lambda:(int(raw_input())) S=lambda:(raw_input()) n=I() r=S() l=len(r) i=0 count=0 while(i<l-1): if r[i]=="R": if r[i+1]=="U": count=count+1 i=i+2 else: count=count+1 i=i+1 else: if r[i+1]=="R": count=count+1 i=i+2 else: count=count+1 i=i+1 if i==(l-1): count=count+1 print count
PYTHON
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.*; import java.util.*; import java.lang.Math; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class GFG { static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String nxt(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nxti(){ return Integer.parseInt(nxt()); } long nxtl(){ return Long.parseLong(nxt()); } double nxtd(){ return Double.parseDouble(nxt()); } String nxtli(){ String str = ""; try{ str = br.readLine(); } catch (IOException e){ e.printStackTrace(); } return str; } } public static void main (String[] args) { FastReader scn=new FastReader(); int n=scn.nxti(); String s=scn.nxtli(),t,res=""; s=s+"PPP"; for(int i=0;i<s.length()-2;){ t=s.substring(i,i+2); if(t.equals("RU")||t.equals("UR")){ res+="D"; i+=2; } else{ res+=s.charAt(i); i++; } } System.out.println(res.length()-1); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) route=input() moves=0 i=0 while i<n: if route[i]=='R': if i<n-1 and route[i+1]=='U': moves+=1 i+=2 else: moves+=1 i+=1 elif route[i]=='U': if i<n-1 and route[i+1]=='R': moves+=1 i+=2 else: moves+=1 i+=1 print(moves)
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 DiagonalWalking { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); in.nextLine(); String s = in.nextLine(); while(s.indexOf("UR") != -1 || s.indexOf("RU") != -1) { int temp1 = s.indexOf("UR"); int temp2 = s.indexOf("RU"); String sub; if(temp1 < temp2) { if(temp1 != -1) { sub = "UR"; } else { sub= "RU"; } } else { if(temp2 != -1) { sub = "RU"; } else { sub = "UR"; } } s = s.replaceFirst(sub, "D"); } //String s2 = s.replaceAll("RU", "D"); //s = s1.replaceAll("UR", "D"); //int ans = s1.length() > s2.length()? s2.length(): s1.length(); int ans = s.length(); System.out.println(ans); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; long long n; string s; long long dp[2000]; long long solve(long long idx) { if (idx > n) return 0; if (dp[idx] != -1) return dp[idx]; long long ans = 1e16; if (s[idx] != s[idx + 1]) { ans = min(ans, solve(idx + 2) + 1); } ans = min(ans, solve(idx + 1) + 1); return dp[idx] = ans; } void test_case() { cin >> n >> s; for (int i = 0; i < n; i++) dp[i] = -1; cout << solve(0); } int main() { long long t; t = 1; while (t--) { test_case(); } }
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 cnt = s.size(); for (int i = 1; i < s.size(); i++) { if (s[i - 1] == 'R' && s[i] == 'U' || s[i - 1] == 'U' && s[i] == 'R') { cnt--; s[i] = 'D'; } } cout << cnt << endl; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.*; import java.util.*; import javafx.util.Pair; import javax.swing.*; public class Codeforces { static PrintWriter pw=new PrintWriter(System.out); public static int oo=1000000000; public static void main(String[] args) throws IOException { Reader.init(System.in); int n=Reader.nextInt(); String x=Reader.next(); int sum=0; for (int i = 0; i < n; i++) { if(i==n-1) {sum++;continue;} if(x.charAt(i)=='U'&&x.charAt(i+1)=='R') i++; else if(x.charAt(i)=='R'&&x.charAt(i+1)=='U') i++; sum++; } pw.println(sum); pw.close(); } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; public static int pars(String x) { int num = 0; int i = 0; if (x.charAt(0) == '-') { i = 1; } for (; i < x.length(); i++) { num = num * 10 + (x.charAt(i) - '0'); } if (x.charAt(0) == '-') { return -num; } return num; } static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static void init(FileReader input) { reader = new BufferedReader(input); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return pars(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } } class pair implements Comparable<pair> { long first, second; pair() { } pair(long a, long b) { first= a; second = b; } public int compareTo(pair q) { if(first<q.first) return 1; else if(first>q.first) return -1; if( second>q. second) return 1; else if( second<q. second) return -1; return 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
input() s=list(input()) ans=0 while len(s)>1: a,b=s.pop(),s.pop() ans+=1 if a==b: s.append(a) print(ans+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
print(len(input()[:0])+len(__import__('re').sub('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
#include <bits/stdc++.h> using namespace std; int length(string str) { int ctr = 0; for (int x = 0; str[x] != '\0'; x++) { ctr++; } return ctr; } int replace(string str) { string ret; for (int x = 0; str[x] != '\0'; x++) { if ((str[x] == 'U' && str[x + 1] == 'R') || (str[x] == 'R' && str[x + 1] == 'U')) { ret += 'D'; x++; } else ret += str[x]; } return length(ret); } int main() { int range; string str; cin >> range >> str; cout << replace(str); 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() last = None res = 0 for c in s: if not last or last == c: res += 1 last = c else: last = None print(res)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) s = list(input()) i = 1 while i < len(s): if s[i] + s[i-1] == 'RU': n -= 1 s[i] = 'D' elif s[i] + s[i-1] == 'UR': n -= 1 s[i] = 'D' i += 1 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; long long int power(long long int a, long long int p) { long long int ret = 1; while (p > 0) { if (p & 1) { ret *= a; } a *= a; p >>= 1; } return ret; } long long int powmod(long long int a, long long int b, long long int MOD) { if (b == 0) return 1; long long int x = powmod(a, b / 2, MOD); long long int y = (x * x) % MOD; if (b % 2) return (a * y) % MOD; return y % MOD; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; string s; cin >> s; int count = 0; for (int i = 0; i < n; i++) { if (s[i] == 'U' && s[i + 1] == 'R' && i + 1 < n) { s[i + 1] = 'D'; i++; } else if (s[i] == 'R' && s[i + 1] == 'U' && i + 1 < n) { s[i + 1] = 'D'; i++; } } for (int i = 0; i < n; i++) { if (s[i + 1] == 'D' && i + 1 < n) { i++; count++; } else { count++; } } cout << count; cout << "\n"; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.Scanner; public class diagonal954A { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int numberOfChara = sc.nextInt(); int count = 0; sc.nextLine(); String statme = sc.nextLine(); for (int i = 0; i < numberOfChara - 1; i++) { if((statme.charAt(i) == 'R' && statme.charAt(i+1) == 'U') || (statme.charAt(i) == 'U' && statme.charAt(i+1) == 'R')){ count++; i++; } } System.out.println(numberOfChara - count); 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
n=int(input()) a=input() i=0 c=0 while i+1<n: if a[i]=='U' and a[i+1]=='R': c+=1 i+=1 elif a[i]=='R' and a[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
import java.util.*; import java.io.*; import java.math.*; public class scorify{ public static void main(String[] args){ Scanner in=new Scanner(System.in); int len = in.nextInt(); String str = in.next(); int S=1,flage=1; char open = str.charAt(0); for(int i=1 ;i<len ;i++){ if(str.charAt(i)!=open && flage==1) { flage=0; open=str.charAt(i); }else if(str.charAt(i)!=open && flage==0){ flage=1; S++; open=str.charAt(i); }else {S++;flage=1;} } System.out.println(S); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.Scanner; public class HelloWorld{ public static void main(String []args){ int n,l=0; Scanner sc=new Scanner(System.in); n=sc.nextInt(); String s1=new String(); s1=sc.next(); /*String s2=s1.replace("UR","D"); String s3=s2.replace("RU","D"); String s4=s1.replace("RU","D"); String s5=s4.replace("UR","D"); //s1.replace("RU","D"); int size_1=s3.length(); int size_2=s5.length(); System.out.println(Math.min(size_1,size_2); //System.out.println(s3);*/ for(int i=0;i<n-1;i++){ if(s1.charAt(i)!=s1.charAt(i+1)) { l++; i++; } } int p=n-l; System.out.println(p); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#author:@karngyan def trans(s,n): cnt=0 flag=0 for i in range(n-1): if(flag==1): flag=0 continue else: if(s[i]+s[i+1]=='RU' or s[i]+s[i+1]=='UR'): cnt+=1 flag=1 return n-cnt n=int(input()) s=input() print(trans(s,n))
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws FileNotFoundException { ConsoleIO io = new ConsoleIO(new InputStreamReader(System.in), new PrintWriter(System.out)); // String fileName = "C-large"; // ConsoleIO io = new ConsoleIO(new FileReader("D:\\Dropbox\\code\\practice\\jb\\src\\" + fileName + ".in"), new PrintWriter(new File("D:\\Dropbox\\code\\practice\\jb\\src\\" + fileName + ".out"))); new Main(io).solve(); // new Main(io).solveLocal(); io.close(); } ConsoleIO io; Main(ConsoleIO io) { this.io = io; } ConsoleIO opt; Main(ConsoleIO io, ConsoleIO opt) { this.io = io; this.opt = opt; } List<List<Integer>> gr = new ArrayList<>(); long MOD = 1_000_000_007; public void solve(){ io.readLine(); char[] s = io.readLine().toCharArray(); int res = s.length; for(int i = 0;i<s.length-1;i++){ if(s[i] != s[i+1]) { i++; res--; } } io.writeLine(res+""); } } class ConsoleIO { BufferedReader br; PrintWriter out; public ConsoleIO(Reader reader, PrintWriter writer){br = new BufferedReader(reader);out = writer;} public void flush(){this.out.flush();} public void close(){this.out.close();} public void writeLine(String s) {this.out.println(s);} public void writeInt(int a) {this.out.print(a);this.out.print(' ');} public void writeWord(String s){ this.out.print(s); } public void writeIntArray(int[] a, int k, String separator) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < k; i++) { if (i > 0) sb.append(separator); sb.append(a[i]); } this.writeLine(sb.toString()); } public int read(char[] buf, int len){try {return br.read(buf,0,len);}catch (Exception ex){ return -1; }} public String readLine() {try {return br.readLine();}catch (Exception ex){ return "";}} public long[] readLongArray() { String[]n=this.readLine().trim().split("\\s+");long[]r=new long[n.length]; for(int i=0;i<n.length;i++)r[i]=Long.parseLong(n[i]); return r; } public int[] readIntArray() { String[]n=this.readLine().trim().split("\\s+");int[]r=new int[n.length]; for(int i=0;i<n.length;i++)r[i]=Integer.parseInt(n[i]); return r; } public int[] readIntArray(int n) { int[] res = new int[n]; char[] all = this.readLine().toCharArray(); int cur = 0;boolean have = false; int k = 0; boolean neg = false; for(int i = 0;i<all.length;i++){ if(all[i]>='0' && all[i]<='9'){ cur = cur*10+all[i]-'0'; have = true; }else if(all[i]=='-') { neg = true; } else if(have){ res[k++] = neg?-cur:cur; cur = 0; have = false; neg = false; } } if(have)res[k++] = neg?-cur:cur; return res; } public int ri() { try { int r = 0; boolean start = false; boolean neg = false; while (true) { int c = br.read(); if (c >= '0' && c <= '9') { r = r * 10 + c - '0'; start = true; } else if (!start && c == '-') { start = true; neg = true; } else if (start || c == -1) return neg ? -r : r; } } catch (Exception ex) { return -1; } } public long readLong() { try { long r = 0; boolean start = false; boolean neg = false; while (true) { int c = br.read(); if (c >= '0' && c <= '9') { r = r * 10 + c - '0'; start = true; } else if (!start && c == '-') { start = true; neg = true; } else if (start || c == -1) return neg ? -r : r; } } catch (Exception ex) { return -1; } } public String readWord() { try { boolean start = false; StringBuilder sb = new StringBuilder(); while (true) { int c = br.read(); if (c!= ' ' && c!= '\r' && c!='\n' && c!='\t') { sb.append((char)c); start = true; } else if (start || c == -1) return sb.toString(); } } catch (Exception ex) { return ""; } } public char readSymbol() { try { while (true) { int c = br.read(); if (c != ' ' && c != '\r' && c != '\n' && c != '\t') { return (char) c; } } } catch (Exception ex) { return 0; } } //public char readChar(){try {return (char)br.read();}catch (Exception ex){ return 0; }} } class Pair { public Pair(int a, int b) {this.a = a;this.b = b;} public int a; public int b; } class PairLL { public PairLL(long a, long b) {this.a = a;this.b = b;} public long a; public long b; } class Triple { public Triple(int a, int b, int c) {this.a = a;this.b = b;this.c = c;} public int a; public int b; public int c; }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) a=(input()) count=0 i=0 while i<n-1: if a[i:i+2]=='RU' or a[i:i+2]=='UR': i+=1 count+=1 i+=1 print(n-count)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.ArrayList; import java.util.Scanner; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author nguyen */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input =new Scanner(System.in); int N =input.nextInt(); String S=input.next(); ArrayList<Character> c=new ArrayList<>(); int in=0; for(int i=0;i<N;i++){ if(i<N-1&& S.charAt(i)=='U'&& S.charAt(i+1)=='R'){ c.add('D'); i++; }else{ if(i<N-1&&S.charAt(i)=='R'&& S.charAt(i+1)=='U'){ c.add('D'); i++; }else{ c.add(S.charAt(i)); } } } System.out.println(c.size()); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> int main() { int i, step, ans; char a[99]; scanf("%d", &step); scanf("%s", &a); for (i = 0, ans = 0; i < step; i++) { ans++; if (a[i] != a[i + 1] && i + 1 < step) { i++; } } printf("%d", 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 sys import math #to read string get_string = lambda: sys.stdin.readline().strip() #to read list of integers get_int_list = lambda: list( map(int,sys.stdin.readline().strip().split()) ) #to read non spaced string and elements are integers to list of int get_intList_from_str = lambda: list(map(int,list(sys.stdin.readline().strip()))) #to read non spaced string and elements are character to list of character get_charList_from_str = lambda: list(sys.stdin.readline().strip()) #get word sepetared list of character get_char_list = lambda: sys.stdin.readline().strip().split() #to read integers get_int = lambda: int(sys.stdin.readline()) #to print faster pt = lambda x: sys.stdout.write(str(x)) #--------------------------------WhiteHat010--------------------------------# n = get_int() s = get_string() count = i = 0 while i <= n-2: if s[i] != s[i+1]: 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=int(input()) l=list(input()) p=[] count=0 s=["RU","UR"] for i in range(n-1): if (l[i]+l[i+1]) in s and i not in p and (i+1) not in p: p.append(i) p.append(i+1) count+=1 s=n-(2*count) print(s+count)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) a = list(input()) for i in range(n): if a[i:i+2] == ['R','U'] or a[i:i+2] == ['U', 'R']: a[i:i+2] = 'z' print(len(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()) prev = 0 final = 0 string = input() for i in range(n): now = string[i] if (prev == 'R' and now == 'U') or (prev == 'U' and now == 'R'): final += 1 prev = 0 else: if now == 'R' or now == 'U': if prev == now: final += 1 prev = now else: if prev == 'R' or prev == 'L': final += 2 else: final += 1 if prev == 'R' or prev == 'U': print(final + 1) else: print(final)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#!/usr/bin/env python n = int(raw_input()) s = raw_input().strip() output = [] i = 0 while i < len(s)-1: if s[i] != s[i+1]: output.append("D") i+=2 else: output.append(s[i]) i+=1 if i < len(s): output.append(s[i]) result = "".join(output) #print result print len(result)
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 n; string a; int main() { cin >> n >> a; for (int i = 0; i < n; i++) if (a[i] == 'R' && a[i + 1] == 'U' || a[i] == 'U' && a[i + 1] == 'R') a.replace(i, 2, "D"); cout << a.size(); }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n, r = 0; cin >> n; string a; cin >> a; if (n == 1) r = 1; else { for (int i = 0; i < n - 1;) { if (a[i] != a[i + 1]) { r++; i += 2; if (i == n - 1) r++; } else if (i == (n - 2)) { r += 2; i++; } else { r++; i++; } } } cout << r; 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) { Scanner cin = new Scanner(System.in); cin.nextLine(); String s = cin.next(); cin.close(); s = s.replaceAll("RU|UR", "t"); System.out.print(s.length()); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) m = list(input()) removeu = 0 for i in range(n): if i < len(m)-1 and (m[i] + m[i+1] == "UR" or m[i] + m[i+1] == "RU"): del m[i+1] m[i] = "D" print(len(m))
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
x=int(input()) y=input() t=i=0 while i<x-1: if y[i]!=y[i+1]: t+=1 i+=1 i+=1 print(x-t)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import sys import math import bisect import itertools def main(): n = int(input()) A = list(input()) for i in range(1, n): if A[i-1] == 'U' and A[i] == 'R': A[i-1] = 'D' A[i] = '.' if A[i-1] == 'R' and A[i] == 'U': A[i-1] = 'D' A[i] = '.' B = [] for a in A: if a != '.': B.append(a) print(len(B)) if __name__ == "__main__": main()
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy sys.setrecursionlimit(10**7) inf=10**20 mod=10**9+7 dd=[(-1,0),(0,1),(1,0),(0,-1)] ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] # def LF(): return [float(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def LS(): return sys.stdin.readline().split() def S(): return input() def main(): n=I() s=S() ans=0 i=0 while True: if i+1<n: if s[i]!=s[i+1]: i+=2 else: i+=1 ans+=1 else: if i==n: return ans else: i+=1 ans+=1 # main() print(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
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int c = 0; for (int i = 0; i < n; i++) { if ((s[i] == 'U' && s[i + 1] == 'R') || (s[i] == 'R' && s[i + 1] == 'U')) { c++; i++; } else c++; } cout << c << 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, kol = 0; cin >> n; string s; cin >> s; for (int i = 0; i < s.length() - 1; i++) { if (s[i] == 'R' && s[i + 1] == 'U' || s[i] == 'U' && s[i + 1] == 'R') { kol++; i++; continue; } } cout << s.length() - kol << "\n"; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); String s=sc.next(); char a[]=s.toCharArray(); char temp; int c=0; int i; if(n==1) System.out.println("1"); else { for(i=1;i<n;i++) { temp=a[i-1]; if( (temp=='U' && a[i]=='R')||(temp=='R' && a[i]=='U') ) { c++; i++; if(i==n-1) { c++; //if(a[i-1]==a[i]) // c++ //else //break; } continue; } c++; if(i==n-1) c++; } System.out.println(c); } } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
hmmm = int(input()) hm = list(input()) i = 0 newList = [] cur = "" nex = "" while i < hmmm: cur = hm[i] if i+1 < hmmm: nex = hm[i+1] if(cur == "R" and nex == "U") or (cur == "U" and nex == "R"): i += 1 newList.append("D") else: newList.append(cur) i += 1 print(len(newList))
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) s = input() ans = n i = 0 while i < n: if i != n - 1: if s[i] != s[i + 1]: ans-=1 i+=1 i += 1 print(ans)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) x = input() prev = "D" cnt = 0 for i in range(0, len(x)): flag = 0 if((x[i] == "U" and prev == "R") or (x[i] == "R" and prev == "U")): flag = 1 else: cnt = cnt+1 prev = x[i] if(flag == 1): prev = "D" print (cnt)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Scanner; import java.util.StringTokenizer; public class Main { static long dp[]=new long[110]; public static void main(String[] args) { // TODO Auto-generated method stub FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int n=in.nextInt(); String s=in.next(); s=" "+s; dp[0]=0; dp[1]=1; for(int i=2;i<=n;i++) { if(s.charAt(i)=='U'&&s.charAt(i-1)=='R') { dp[i]=dp[i-2]+1; } else if(s.charAt(i)=='R'&&s.charAt(i-1)=='U') { dp[i]=dp[i-2]+1; } else dp[i]=dp[i-1]+1; } out.println(dp[n]); out.close(); } 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 int[] nextIntArray(int n) { int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n){ long[] a = new long[n]; for(int i = 0; i < n; i++) a[i] = nextLong(); return a; } public double[] nextDoubleArray(int n){ double[] a = new double[n]; for(int i = 0; i < n; i++) a[i] = nextDouble(); return a; } public char[][] nextGrid(int n, int m){ char[][] grid = new char[n][m]; for(int i = 0; i < n; i++) grid[i] = next().toCharArray(); return grid; } } }
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
le = int(input()) st = input() c = 0 if le == 2: if st[0] != st[1]: print(1) else: print(2) elif le == 1: print(1) else: x = st[0] for i in range(0, le): if x != st[i] and i != le - 1: c += 1 x = st[i + 1] i += 1 elif x != st[i] and i == le - 1: c += 1 print(le - c)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; for (int i = 0; i < n - 1; i++) { bool c1 = s[i] == 'U' and s[i + 1] == 'R'; bool c2 = s[i] == 'R' and s[i + 1] == 'U'; if (c1 or c2) { s[i] = 'D'; s = s.substr(0, i + 1) + s.substr(i + 2); } } cout << s.size() << endl; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; char t[n]; int i; int h = n; for (int i = 0; i < n; i++) { cin >> t[i]; } i = 0; while (i < n) { if ((t[i] == 'U' && t[i + 1] == 'R') || (t[i] == 'R' && t[i + 1] == 'U')) { i += 2; h--; } else i++; } cout << h; 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
k=input() s=input() n=len(s) last=s[0] for c in s[1:]: if (last=='R' and c=='U'): n-=1 last='' elif(last=='U' and c=='R'): n-=1; last='' else: last=c print(n)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) l = list(input()) i = 0 co = 0 while i < n - 1: if l[i] == 'U' and l[i + 1] == 'R' or l[i] == 'R' and l[i + 1] == 'U': co += 1 i += 2 else: i += 1 print(n - co)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) s = list(input()) counter = 0 i = 0 while i < (n - 1): if s[i] == 'U' and s[i+1] == 'R': counter += 1 i += 2 continue elif s[i] == 'R' and s[i+1] == 'U': counter += 1 i += 2 continue i += 1 print(n - counter) # hamed gh
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, s = int(input()), input() + '#' d, i = 0, 0 while i < n: if s[i: i + 2] in ('UR', 'RU'): d += 1 i += 2 else: i += 1 print(n - d)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) s=input()+"U" i,c=0,0 while i<n: c+=1 if s[i]!=s[i+1]:i+=2 else:i+=1 print(c)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
# https://codeforces.com/problemset/problem/954/A n = int(input()) s = input() ans = 0 i = 1 while i < n: if s[i-1] != s[i]: ans += 1 i += 2 else: 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Reader; import java.io.StreamTokenizer; import java.io.Writer; public class Tmp { private static Reader reader = new InputStreamReader(System.in); private static Writer writer = new OutputStreamWriter(System.out); private static StreamTokenizer in = new StreamTokenizer(new BufferedReader(reader)); private static PrintWriter out = new PrintWriter(writer); private static int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } public static void main(String[] args) throws IOException { final BufferedReader br = new BufferedReader(reader); int n = Integer.valueOf(br.readLine()); final String s = br.readLine(); out.println(solve(s)); out.flush(); } public static int solve(String s) { int l = s.length(); if (l == 1) { return 1; } char prev = s.charAt(0); char curr; int res = 1; for (int i = 1; i < l; i++) { curr = s.charAt(i); if (prev == 'D') { prev = curr; res++; } else if (prev != curr) { prev = 'D'; } else { res++; } } return res; } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=input() n=int(n) str=input() i=0 count=0 while i<=n-2: if (str[i]=='U' and str[i+1]=='R') or (str[i]=='R' and str[i+1]=='U'): count+=1 i+=1 i+=1 print(n-count)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.awt.Point; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.*; import java.util.*; public class Main { ////// static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } ////// public static long modularExponentiation(long x, long n, long M) { long result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result; } public static long modularExponentiation1(long x, long n) { long result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x); x = (x * x); n = n / 2; } return result; } ///// static public int fun(int num) { int prod = 1; while (num > 0) { if (num % 10 != 0) { prod = prod * (num % 10); } num = num / 10; } return prod; } public static boolean dfs(int x, int find, boolean[] visited, int[][] matrix) { boolean ans = false; if (x == find) { ans = true; return true; } for (int i = 0; i < 26; i++) { if (visited[i] == false && matrix[x][i] == 1) { visited[i] = true; ans = ans || dfs(i, find, visited, matrix); } } return ans; } static int lis(int arr[], int n, int k, int b) { int lis[] = new int[n]; int i, j, max = 0; /* Initialize LIS values for all indexes */ for (i = 0; i < n; i++) lis[i] = 1; /* Compute optimized LIS values in bottom up manner */ for (i = 1; i < n; i++) for (j = 0; j < i; j++) if (arr[i] >= k * arr[j] + b && lis[i] < lis[j] + 1) lis[i] = lis[j] + 1; /* Pick maximum of all LIS values */ for (i = 0; i < n; i++) if (max < lis[i]) max = lis[i]; return max; } public static void main(String[] args) throws IOException { FastReader scan = new FastReader(); PrintWriter pw = new PrintWriter(System.out); int N=scan.nextInt(); String str3=scan.next(); int count=0; for(int i=0;i<N;i++){ if(i==N-1){ count++; continue; } if(str3.charAt(i)!=str3.charAt(i+1)){ count++; i++; }else count++; } System.out.println(count); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) c=input() i=0 s=0 while i<len(c): if i<len(c)-1: if c[i+1]!=c[i]: s+=1 i+=2 else: s+=1 i+=1 else: s+=1 break print(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
import java.util.Scanner; public class DiagonalWalking { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); String seq=sc.next(); char[] seq2=seq.toCharArray(); int sum=n; for(int i=0; i<n-1; i++) { if (seq2[i]!=seq2[i+1]) { sum--; i++; } } if(n%2!=0) { System.out.println(sum); } else { System.out.println(sum); } } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.*; import java.util.*; public class Main { static StringBuilder data = new StringBuilder(); final static FastReader in = new FastReader(); public static void main(String[] args) { int n = in.nextInt(); String s = in.nextLine(); int d = 0, i = 0; while (true) { if (i + 1 <= n - 1) { if ((s.charAt(i) == 'U' && s.charAt(i + 1) == 'R') || (s.charAt(i) == 'R' && s.charAt(i + 1) == 'U')) { d += 1; i += 2; } else { i++; } } else { break; } } System.out.println(s.length() - d); } static void fileOut(String s) { File out = new File("output.txt"); try { FileWriter fw = new FileWriter(out); fw.write(s); fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String path) { try { br = new BufferedReader(new InputStreamReader(new FileInputStream(path))); } catch (FileNotFoundException e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } float nextFloat() { return Float.parseFloat(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) a=input() j=i=0 while(i<(n)): try: if a[i]=='U' and a[i+1]=='R' or a[i]=='R' and a[i+1]=='U': j+=1 i+=2 else: j+=1 i+=1 except: j+=1 i+=1 print(j)
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() a = raw_input() l = [] for i in range(n): if l==[]: l.append(a[i]) elif (l[-1]=='U' and a[i]=='R' ) or (l[-1]=='R' and a[i]=='U'): l.pop() l.append('D') else: l.append(a[i]) print len(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
import re n=input() s=input().lower() print(len(re.sub('(ru|ur)','d',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; long long n, m, k, ans, l, r, kk, a[100001], b[100001]; string s, t, second; int main() { cin >> n; cin >> s; for (int i = 0; i < s.size(); ++i) { if ((s[i] == 'R' && s[i + 1] == 'U') || (s[i] == 'U' && s[i + 1] == 'R')) --n, ++i; } cout << n; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.*; public class A0954_DiagonalWalking { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String str = sc.next(); sc.close(); int res = n; for (int i=0; i<n-1; i++) { if (str.charAt(i) != str.charAt(i+1)) { res--; i++; } } System.out.println(res); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; void adskiy_razgon() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const int MAX_N = 1e+4 * 3 + 33; const int MOD = 1e+9 + 7; const long long INF = 1e+18; int nod(int a, int b) { if (a == 0 || b == 0) { return 0; } else if (a == b) { return a; } else if (a > b) { return nod(a - b, b); } else { return nod(a, b - a); } } int nok(int a, int b) { return a * b / nod(a, b); } void setpr(int a, double b) { cout << fixed << setprecision(a) << b; } int main() { adskiy_razgon(); int n, ans = 0; cin >> n; string s; cin >> s; for (int i = 0; i < n; i++) { if ((s[i] == 'U' && s[i + 1] == 'R') || (s[i] == 'R') && s[i + 1] == 'U') { ans++; i++; } else { ans++; } } cout << ans; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) string = list(map(str, input())) ans = 0 i = 0 while i<len(string)-1: if(string[i] == 'U' and string[i+1] == 'R') or (string[i] == 'R' and string[i+1] == 'U'): del string[i] del string[i] ans+=1 else: i+=1 print(ans+ len(string))
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 static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.util.*; import java.io.*; public class x954A { public static void main(String hi[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); char[] arr = infile.readLine().toCharArray(); int res = N; for(int i=1; i < N; i++) if(arr[i] != arr[i-1]) { i++; res--; } System.out.println(res); } public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } }
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 = input() path = input() subs = 0 i = 0 while i < len(path) - 1: if path[i:i+2] in ("RU", "UR"): subs += 1 i += 1 i += 1 print(len(path) - subs)
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()) str=input() count=0 i=0 while i<=n-2: if str[i]=='U' and str[i+1]=='R' or str[i]=='R' and str[i+1]=='U': count+=1 i+=1 i+=1 print(n-count)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
def solve2(s): curr = 0 d = 0 if len(s) == 1: return 1 while curr < len(s)-1: if s[curr] != s[curr+1]: curr += 2 else: curr += 1 d += 1 if curr == len(s) - 1: d += 1 return d n = int(input()) print(str(solve2(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
len = int(input()) step = input() i=0 count =0 for c in step: if i==len-1 : count+=1 break elif i>len-1 : break if (step[i] != step[i+1]) : count += 1 i+=2 continue 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
#include <bits/stdc++.h> using namespace std; using ll = long long; int N; string s; int main() { cin >> N; cin >> s; int cnt = 0; if (N > 1) { for (int i = (1); i < (int)(N); i++) { if ((s[i] == 'U' && s[i - 1] == 'R') || (s[i] == 'R' && s[i - 1] == 'U')) { cnt++; if (i != N - 2) i++; else { cnt++; break; } } else { cnt++; if (i == N - 1) cnt++; } } } else cnt++; cout << 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
# cf 954 A 900 n = int(input()) s = input() i = 0 ans = 0 #UU UR RRR RU U UR UR UUU #UU D RRR D U D D UUU # ^ #UU D RRR RDUUDDDDUU while i < len(s) - 1: if s[i] == 'U' and s[i + 1] == 'R': ans += 1 i += 2 elif s[i] == 'R' and s[i + 1] == 'U': ans += 1 i += 2 elif s[i] == 'R' and s[i + 1] == 'R': ans += 1 i += 1 elif s[i] == 'U' and s[i + 1] == 'U': ans += 1 i += 1 if i < len(s): ans += 1 print(ans)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int n; char s[105]; int main() { scanf("%d", &n); scanf("%s", s); int cnt = 0; for (int i = 0; i < n - 1; i++) { if (s[i] == 'U' && s[i + 1] == 'R') { cnt++; i++; } else if (s[i] == 'R' && s[i + 1] == 'U') { cnt++; i++; } } printf("%d", n - cnt); return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); char[] a = sc.next().toCharArray(); int cnt = 0; for (int i = 0; i < n - 1; i++) { if (a[i] != a[i + 1]) { cnt++; i++; } } out.println(n - cnt); out.flush(); out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) { try { br = new BufferedReader(new FileReader(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public 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
import java.util.Scanner; public class Diagonal { public static void main(String[] args) { Scanner input = new Scanner(System.in); input.nextLine(); String s = input.nextLine(); input.close(); int min = s.length(); for (int i = 0; i < s.length() - 1; i++) { if (s.charAt(i) == 'U' && s.charAt(i + 1) == 'R' || s.charAt(i) == 'R' && s.charAt(i + 1) == 'U') { min--; i++; } } System.out.println(min); } }
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 n = int(input()) str1 = input() s = re.sub('(RU|UR)','D', str1) print(len(s))
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) i = 0 ans = 0 l = list(input()) while i < len(l)-1: if l[i] != l[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
#include <bits/stdc++.h> using namespace std; int main() { int n, cnt = 0; cin >> n; string s; cin >> s; for (int i = 0; i < s.size(); i++) { if ((s[i] == 'R' && s[i + 1] == 'U') || (s[i] == 'U' && s[i + 1] == 'R')) { cnt += 1; i++; } else cnt++; } cout << cnt; return 0; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.awt.image.AreaAveragingScaleFilter; import java.io.*; import java.util.*; import static java.lang.Math.*; public class Main { BufferedReader br; StringTokenizer in; PrintWriter pw; Random r; int INF = (int) 1e9; long LNF = (long) 1e18; long mod = (long) (1e9 + 7); int pp = 27; // you shall not hack!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // /*\ // /// /^\ // /// ~~~~ (___) // /// ~~~~~~~~ \\__ // _/_/_ ~/ .& . \~ \\ \ // [____] ~| (__) |~/ \\/ // /_/\ \~|~~~~~~~~~~ /\\ // \ ~~~~~~~~~~ _/ \\ // \ _ ~~ _/\ \\ // / | / \ \\ // | / | \ \\ // / / ___ | \ // /___| | __| |_______\ // _| | | |_ // |____| |____| // P.S this is omnipotent Gandalf ArrayList<road> g[]; int n, m; double eps = 1e-9; void end(int a) { pw.print(a); pw.close(); System.exit(0); } void z() { pw.println(2 * (int) 1e5 + " " + (int) 1e6); for (int i = 0; i < 2 * 1e5; i++) { pw.print((int) 1e6 + " "); } pw.println(); for (int i = 0; i < 2 * 1e5; i++) { pw.print((int) 1e6 + " "); } } void solve() throws IOException { int n = nextInt(); String s = nextToken(); int ans = n; for (int i = 0; i < n - 1; i++) { if (s.charAt(i) + s.charAt(i + 1) == 'R' + 'U') { ans--; i++; } } pw.print(ans); } class tap implements Comparable<tap> { double a; double t; public tap(int aa, int tt) { a = aa; t = tt; } @Override public int compareTo(tap o) { if (t != o.t) return Double.compare(t, o.t); return Double.compare(a, o.a); } } class pair { int a; int b; public pair() throws IOException { a = nextInt(); b = nextInt(); } } class road { int b; int indx; public road(int bb, int y) { b = bb; indx = y; } } class node { int sz; int val; // long sum; node L; node R; int prior; public node(int val) { this.val = val; prior = r.nextInt(); L = null; R = null; sz = 1; // sum = (long)val * val; } void add(int z) { val = val + z; // sum = (long)val * val; } } void relax(node c) { if (c == null) return; c.sz = sfsz(c.L) + sfsz(c.R) + 1; // c.sum = sfsum(c.L) + sfsum(c.R) + c.val * c.val; } int sfsz(node c) { if (c == null) return 0; return c.sz; } node merge(node a, node b) { if (a == null) return b; if (b == null) return a; if (a.prior > b.prior) { a.R = merge(a.R, b); relax(a); return a; } else { b.L = merge(a, b.L); relax(b); return b; } } pairn split(node c, int s) { if (c == null) return new pairn(null, null); if (sfsz(c.L) >= s) { pairn p = split(c.L, s); c.L = p.b; relax(c); // relax(p.a); return new pairn(p.a, c); } else { pairn p = split(c.R, s - sfsz(c.L) - 1); c.R = p.a; relax(c); // relax(p.b); return new pairn(c, p.b); } } class pairn { node a; node b; public pairn(node a, node b) { this.a = a; this.b = b; } } String nextToken() throws IOException { while (in == null || !in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } public void run() throws IOException { r = new Random(5); // br = new BufferedReader(new FileReader("input.txt")); // pw = new PrintWriter(new FileWriter("output.txt")); br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new OutputStreamWriter(System.out)); // br = new BufferedReader(new FileReader("railroad.in")); // pw = new PrintWriter(new FileWriter("railroad.out")); solve(); // z(); pw.close(); } public static void main(String[] args) throws IOException { new Main().run(); } }
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()) d = list(input()) i = 0 count = 0 if n == 1: print(1) else: while i < n-1: if (d[i] == 'R' and d[i+1] == 'U') or (d[i] == 'U' and d[i+1] == 'R'): i = i + 2 count = count + 1 if i == n - 1: count = count + 1 else: i = i + 1 count = count + 1 if i >= n - 1: count = count + 1 print(count)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
# -*- coding:utf-8 -*- sum=(int)(raw_input()) str=raw_input() count=0 index=0 while(True): if(index>=sum-1): if(index==sum-1): count+=1 break if(str[index] == 'U' and str[index+1] == 'R'): count+=1 index+=2 elif(str[index] == 'R' and str[index+1] == 'U'): count+=1 index+=2 else: count+=1 index+=1 print(count)
PYTHON
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) s = input() while True: used = False for i in range(1, len(s)): if s[i - 1] + s[i] == "UR" or s[i - 1] + s[i] == "RU": s = s[:i - 1] + "D" + s[i + 1:] used = True break if not used: break print(len(s))
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n = int(input()) A = input() prev_changed = False shortened = 0 for i in range(1, n): if not prev_changed: if A[i] != A[i - 1]: prev_changed = True shortened += 1 else: prev_changed = False print(n - shortened)
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 NewClass { static final int INF = Integer.MAX_VALUE; static void mergeSort(int[] a,int[]b, int p, int r) { if (p < r) { int q = (p + r) / 2; mergeSort(a,b, p, q); mergeSort(a,b, q + 1, r); merge(a,b, p, q, r); } } static void merge(int[] a,int[]b, int p, int q, int r) { int n1 = q - p + 1; int n2 = r - q; int[] L = new int[n1 + 1], R = new int[n2 + 1]; int[] L1 = new int[n1 + 1], R1 = new int[n2 + 1]; for (int i = 0; i < n1; i++) { L[i] = a[p + i]; L1[i] = b[p + i]; } for (int i = 0; i < n2; i++) { R[i] = a[q + 1 + i]; R1[i] = b[q + 1 + i]; } L[n1] = R[n2] = INF; L1[n1] = R1[n2] = INF; for (int k = p, i = 0, j = 0; k <= r; k++) { if (L[i] <= R[j]) { a[k] = L[i]; b[k]=L1[i++]; } else { a[k] = R[j]; b[k] = R1[j++]; } } } public static int[] sieve(int n) { int a[] = new int[n + 1]; for (int i = 2; i <= n; i++) { a[i] = 1; } for (int i = 2; i <= Math.sqrt(n); i++) { if (a[i] == 1) { for (int k = 2; i * k <= n; k++) { a[i * k] = 0; } } } return a; } public static long sum(int a) { long su = 0; while (a > 0) { su += a % 10; a /= 10; } return su; } public static int divi(int b) { int n = 0; for (int i = 2; i <= Math.sqrt(b); i++) { if (b % i == 0) { n = i; break; } } if (n == 0) { return b; } return n; } public static boolean prime(int n) { for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { return false; } } return true; } public static String reve(int a, int b, String s) { String q = ""; for (int i = b; i >= a; i--) { q += s.charAt(i); } return q; } public static boolean isvowel(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y') { return true; } else { return false; } } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public static int hole(int n) { int y = 0; if (n == 1) { return 0; } if (prime(n)) { return 1; } for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { if (prime(i)) { y++; } if (prime(n / i) && i != (n / i)) { y++; } } } return y; } static StringBuilder getnine(StringBuilder s){ StringBuilder c= new StringBuilder(); for (int i = 0; i <s.length(); i++) { c.append('9'); } return c; } public static void main(String[] args) throws IOException { InputReader in = new InputReader(System.in); OutputWriter or = new OutputWriter(System.out); int n = in.nextInt(); StringBuilder s= new StringBuilder(in.readString()); for (int i = 0; i < s.length()-1; i++) { if (s.charAt(i)=='U'&&s.charAt(i+1)=='R') { s.deleteCharAt(i--); s.setCharAt(i+1, 'D'); } else if (s.charAt(i)=='R'&&s.charAt(i+1)=='U') { s.deleteCharAt(i--); s.setCharAt(i+1, 'D'); } } or.print(s.length()); or.flush(); } } class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (snumChars == -1) { throw new InputMismatchException(); } if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } }
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 = '' i = 0 while(i<n): x = s[i:i+2] if(x == "RU" or x == "UR"): a+="D" i+=2 else: a+=x[0] i+=1 print(len(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()) str=input() i=0 count=0 while(i<n-1): if(str[i]!=str[i+1]): i=i+2 count+=1 else: i=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
r = input n = int(r()) ans = n s = r() i = 0 while i + 1 < n: if s[i:i + 2] == 'UR' or s[i:i + 2] == 'RU': ans -= 1 i += 1 i += 1 print(ans)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n; cin >> n; string s; cin >> s; int cnt = s.size(); map<int, int> vis; for (int i = 1; i < s.size(); i++) { if (s[i] == 'R' && s[i - 1] == 'U' && vis[i - 1] == 0) { vis[i] = 1; cnt--; } else if (s[i] == 'U' && s[i - 1] == 'R' && vis[i - 1] == 0) { vis[i] = 1; cnt--; } } cout << cnt << endl; }
CPP
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
# -*- coding: utf-8 -*- """ Created on Fri Mar 30 10:24:29 2018 @author: aalfaiz """ n =int(input()) moves = input() num_moves = 0; i=0 while(i < n): if(i<n-1 and moves[i]!=moves[i+1]): i+=2 else: i+=1 num_moves+=1 print(f'{num_moves}')
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.*; import java.io.*; public class Solution2 { private void solve() throws IOException { int n = in.nextInt(); String s = in.next(); int count = 0; for (int i = 0; i < n - 1; i++) { if (s.charAt(i) == 'R' && s.charAt(i + 1) == 'U') { count++; i++; } else if (s.charAt(i) == 'U' && s.charAt(i + 1) == 'R') { count++; i++; } } System.out.println(n - count); } private PrintWriter out; MyScanner in; private void run() throws IOException { in = new MyScanner(); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } private class MyScanner { private BufferedReader br; private StringTokenizer st; public MyScanner() throws IOException { this.br = new BufferedReader(new InputStreamReader(System.in)); } public MyScanner(String fileTitle) throws IOException { this.br = new BufferedReader(new FileReader(fileTitle)); } public String nextLine() throws IOException { String s = br.readLine(); return s == null ? "-1" : s; } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) { String s = br.readLine(); if (s == null) { return "-1"; } st = new StringTokenizer(s); } return st.nextToken(); } public Integer nextInt() throws IOException { return Integer.parseInt(this.next()); } public Long nextLong() throws IOException { return Long.parseLong(this.next()); } public Double nextDouble() throws IOException { return Double.parseDouble(this.next()); } public void close() throws IOException { this.br.close(); } } public static void main(String[] args) throws IOException { Locale.setDefault(Locale.US); new Solution2().run(); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Codeforces{ public static void main(String []args) { MyScanner in = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); //Start your solution from here. int n = in.nextInt(); String s = in.nextLine(); StringBuilder ans = new StringBuilder(); boolean f = false; for(int i=0;i<n-1;){ if(s.charAt(i)==s.charAt(i+1)){ ans.append(s.charAt(i)); i++; } else{ ans.append('D'); if(i==n-2){ f = true; } i += 2; } } if(!f){ ans.append(s.charAt(n-1)); } out.println(ans.length()); //End your solution here. out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return next().charAt(0); } int [] nextIntAr(int [] ar){ for(int i=0;i<ar.length;++i){ ar[i] = Integer.parseInt(next()); } return ar; } long [] nextLongAr(long [] ar){ for(int i=0;i<ar.length;++i){ ar[i] = Long.parseLong(next()); } return ar; } double [] nextDoubleAr(double [] ar){ for(int i=0;i<ar.length;++i){ ar[i] = Double.parseDouble(next()); } return ar; } void printarraystring(String [] ar,char c){ int n = ar.length; for(int i=0;i<n;++i){ if(c=='n') out.println(ar[i]); else{ out.print(ar[i]+" "); } } if(c!='n'){ out.println(); } } void printarraydouble(double [] ar,char c){ int n = ar.length; for(int i=0;i<n;++i){ if(c=='n') out.println(ar[i]); else{ out.print(ar[i]+" "); } } if(c!='n') out.println(); } void printarrayint(int [] ar,char c){ int n = ar.length; for(int i=0;i<n;++i){ if(c=='n') out.println(ar[i]); else{ out.print(ar[i]+" "); } } if(c!='n') out.println(); } String [] nextStringAr(String [] ar){ for(int i=0;i<ar.length;++i){ ar[i] = next(); } return ar; } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } class Pair implements Comparable<Pair>{ int f; int s; int sum; Pair(int a,int b,int c){ f = a; s = b; sum = c; } @Override public int compareTo(Pair a) { // TODO Auto-generated method stub return Integer.compare(this.sum, a.sum); } }
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
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc=new Scanner(System.in); int n=sc.nextInt(); String s=sc.next(); String p=""; for(int i=0;i<s.length();i++) { if(i==s.length()-1) p+=s.charAt(i); else{ String g=s.substring(i,i+2); if(g.equals("RU")||g.equals("UR")){ p+="D"; // System.out.println(p); i++;} else p+=s.charAt(i); } } //s=s.replace("UR","D"); //System.out.println(p); //s=s.replace("RU","D"); //System.out.println(s); System.out.println(p.length()); } }
JAVA
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.io.*; import java.util.*; public class A { public static void main(String args[])throws IOException { Scanner sc= new Scanner(System.in); int l = sc.nextInt(); String s=sc.next(); int ans=l; for(int i=0;i<l-1;i++) { char ch = s.charAt(i); char ch2= s.charAt(i+1); if((ch=='U' && ch2=='R') || (ch=='R' && ch2=='U')) { ans--; i++; } } System.out.println(ans); } }
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()) #arr = map(int,raw_input().split(" ")) arr = raw_input() i = 1 ans = n while i < n: if arr[i]!=arr[i-1]: ans -= 1 i += 1 i += 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
length = int(raw_input()) sequence = str(raw_input()) i = 0 while i < len(sequence)-1: string = sequence[i] + sequence[i+1] if string == 'RU' or string == 'UR': sequence = sequence[0:i] + 'D' + sequence[i+2:] i=0 else: i+=1 print len(sequence)
PYTHON
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
n=int(input()) s=input() t=0;i=0 while i<n-1: if s[i]!=s[i+1]: t+=1;i+=1 i+=1 print(n-t)
PYTHON3
954_A. Diagonal Walking
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible. In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
2
7
import java.util.*; import java.io.*; import java.math.*; public class Main{ static int mod = (int)1e9+7; public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); public static void main(String sp[])throws IOException{ FastReader sc = new FastReader(); int n = sc.nextInt(); String st = sc.next(); if(st.length()<2){ System.out.println(st.length()); return; } int count=0; int i=0; while(i<n){ if(i+1<n){ if(st.charAt(i)=='R' && st.charAt(i+1)=='U'){ count++; i+=2; } else if(st.charAt(i)=='U' && st.charAt(i+1)=='R'){ count++; i+=2; } else{ i+=1; count++; } }else{ count++; break; } } System.out.println(count); } public static long power(long x, long y) { long temp; if( y == 0) return 1; temp = power(x, y/2); if (y%2 == 0) return temp*temp; else return x*temp*temp; } public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception r) { r.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next());//converts string to integer } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception r) { r.printStackTrace(); } return str; } } }
JAVA