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 | n = int(input())
s = input().rstrip()
work = []
countD = 0
for i in range(n):
work.append(s[i])
for j in range(n-1):
if work[j] == 'R' and work[j+1] == 'U' or work[j] =='U' and work[j+1] == 'R':
work[j] = 'D'
work[j+1] = 'D'
countD += 1
print(n-countD) | 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 |
if __name__ == "__main__":
n = int(input())
string = input()
i = 0
count = 0
ans = n
while i < n-1:
if string[i] + string[i+1] == 'RU' or string[i] + string[i+1] == 'UR':
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() {
int n;
cin >> n;
string s, s1;
cin >> s;
int len = s.length();
for (int i = 0; i < len; i++) {
if (s[i] == 'U' && s[i + 1] == 'R' || s[i] == 'R' && s[i + 1] == 'U') {
s[i] = ' ';
s[i + 1] = ' ';
s1 = s1 + "D";
i = i + 1;
} else {
s1 = s1 + s[i];
}
}
cout << s1.length();
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
using lint = long long;
template <typename T>
using vc = std::vector<T>;
void solve(istream& cin, ostream& cout) {
int n;
string s;
cin >> n >> s;
int ans = n;
for (int i = (1); i < int(n); ++i) {
if (s[i - 1] != 'D' && s[i] != s[i - 1]) {
ans--;
s[i] = 'D';
}
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve(cin, cout);
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 gcd(int p, int q) { return q == 0 ? p : gcd(q, p % q); }
int main() {
int n;
char s[110];
cin >> n >> s;
int len = strlen(s);
int ans = 0;
for (int i = 0; i < len; ++i) {
ans++;
if (s[i] == 'R' && s[i + 1] == 'U') {
++i;
} else if (s[i] == 'U' && s[i + 1] == 'R') {
++i;
}
}
cout << ans << endl;
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | size_path = int(input())
main_path = str(input())
main_path_list = []
for x in main_path:
main_path_list.append(x)
it = 0
while(it < len(main_path_list)-1):
if main_path_list[it] == "R" and main_path_list[it+1] == "U":
main_path_list[it] = "D"
it += 1
elif main_path_list[it] == "U" and main_path_list[it+1] == "R":
main_path_list[it] = "D"
it += 1
it += 1
result = ''
for i in range (0, len(main_path_list)):
if main_path_list[i-1] != "D":
result += main_path_list[i]
sol = 0
for x in result:
sol += 1
print(sol)
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(raw_input())
seq = raw_input()
new_seq = ""
while len(seq) > 1:
temp = seq[0] + seq[1]
if temp in ["RU", "UR"]:
new_seq += "D"
seq = seq[2::]
else:
new_seq += seq[0]
seq = seq[1::]
if len(seq) == 1:
new_seq += seq[0]
print len(new_seq) | PYTHON |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #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 - 1;) {
if ((s[i] == 'U' && s[i + 1] == 'R') || (s[i] == 'R' && s[i + 1] == 'U')) {
c++;
i += 2;
} else
i++;
}
cout << n - c;
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
signed main() {
int n, dem = 0;
string s;
cin >> n;
cin >> s;
s += 'X';
for (int i = 0; i < n; ++i) {
if (s[i] == 'R' && s[i + 1] == 'U' || s[i] == 'U' && s[i + 1] == 'R') {
dem++;
i++;
} else {
dem++;
}
}
cout << dem;
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
s = input()
ans = n
used = [False for i in range(n)]
for i in range(1, n):
if not used[i - 1] and (s[i] == 'R' and s[i - 1] == 'U' or s[i] == 'U' and s[i - 1] == 'R'):
ans -= 1
used[i] = True
used[i - 1] = True
print(ans)
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 110;
int n;
char s[MAXN];
int main() {
scanf("%d", &n);
scanf("%s", s + 1);
int ans = n;
for (int i = 1; i <= n; i++) {
if (s[i] != s[i + 1] && i != n) {
ans--;
i++;
}
}
printf("%d\n", ans);
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.util.*;
import java.lang.*;
import java.math.BigInteger;
import java.io.*;
public class Main {
public static void main(String [] args) throws Exception {
OutputStream outputStream =System.out;
PrintWriter out =new PrintWriter(outputStream);
FastReader sc =new FastReader();
int n = sc.nextInt();
char c[] = sc.nextLine().toCharArray();
int cnt = 0;
for(int i=0;i<n;)
{
if(i<n-1 && (c[i]=='U' && c[i+1]=='R'))
{
i+=2;
cnt++;
}
else if(i<n-1 && (c[i]=='R' && c[i+1]=='U'))
{
i+=2;
cnt++;
}
else if(i==n-1)
{
cnt++;
i++;
}
else
{
cnt++;
i++;
}
}
out.println(cnt);
out.close();
}
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;
}
}
}
| 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 = list(input())
o = n
h = False
for i in range(n - 1):
if h is False and (a[i] + a[i + 1] == "RU" or a[i] + a[i + 1] == "UR"):
o -= 1
h = True
else:
h = False
print(o)
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
string s;
int c, n;
int main() {
cin >> n >> s;
for (int i = 1; i < n; i++)
if (s[i] != s[i - 1]) {
i++;
c++;
}
cout << n - c << '\n';
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n=int(input())
t=input()
ans=0
i=0
while i<n:
if i==n-1:
ans+=1
break
if t[i]=='R' and t[i+1]=='U': i+=1
elif t[i]=='U' and t[i+1]=='R': i+=1
ans+=1
i+=1
print(ans) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n=int(input())
s=input()+'k'
x=0
i=0
while(i<n):
if((s[i]=='R' and s[i+1]=="U") or (s[i]=='U' and s[i+1]=='R')):
x=x+1
i=i+2
else:
i=i+1
x=x+1
print(x)
| 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;
#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
inline int nxt() {
int x;
scanf("%d", &x);
return x;
}
inline long long lnxt() {
long long x;
scanf("%I64d", &x);
return x;
}
const int N = (int)1e6 + 100;
const int base = (int)1e9;
const int maxn = (int)1e3 + 100;
const int mod = (int)1e9 + 7;
const int inf = INT_MAX;
const long long ll_inf = LLONG_MAX;
const long double pi = acos((long double)-1.0);
const long double eps = 1e-8;
long long cnt = 0, ans = 0;
void solve() {
int n;
cin >> n;
string s;
cin >> s;
for (int i = 1; i < s.size(); ++i) {
if (s[i] != s[i - 1]) {
++i;
++cnt;
}
}
cout << n - cnt << endl;
}
int main() {
int T = 1;
for (; T; --T) solve();
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
s = input()
p = 0
f = []
f.append(True)
for i in range(1,len(s)):
if s[i] != s[i-1] and f[i-1]:
f.append(False)
f[i] = False
p += 1
else:
f.append(True)
print(n-p)
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, cnt = 0;
scanf("%d", &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++;
i++;
}
}
cout << n - cnt << endl;
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | inp = int(input())
seq = raw_input()
new = ""
i = 0
while i <= (inp)-1:
sub = (seq[i:i+2])
#print(" "*i + sub)
if sub == "UR" or sub == "RU":
new += "D"
i+=1
else:
new += sub[0]
i +=1
print(len(new))
| PYTHON |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Integer num = Integer.parseInt(br.readLine().trim());
String str = br.readLine().trim();
String res = "";
for (int i = 0; i <= num - 1; i++) {
if ((i + 1) <= num - 1 && ((str.charAt(i) == 'U' && str.charAt(i + 1) == 'R')
|| (str.charAt(i) == 'R' && str.charAt(i + 1) == 'U'))) {
res += "D";
i++;
} else {
res += Character.toString(str.charAt(i));
}
}
System.out.println(res.length());
}
} | JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
char a[101];
int ans = 0;
scanf("%d", &n);
scanf("%s", a);
for (int i = 0; i < n; i++) {
if (a[i] != a[i + 1]) i++;
ans++;
}
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 | n=int(input())
s=input()
f=[0]*n
cnt=0
for i in range(n-1):
if s[i]!=s[i+1] and not f[i]:
f[i+1]=1
cnt+=1
print(n-cnt) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.util.Vector;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Stack;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Pradyumn
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, FastReader in, PrintWriter out) {
Debug debug = new Debug(out);
int n = in.nextInt();
char[] s = in.nextString().toCharArray();
Stack<Character> stack = new Stack<>();
for (int i = 0; i < n; ++i) {
if (!stack.isEmpty() && stack.peek() != s[i] && stack.peek() != 'D') {
stack.pop();
stack.push('D');
} else {
stack.push(s[i]);
}
}
out.println(stack.size());
}
}
static class Debug {
PrintWriter out;
boolean oj;
boolean system;
long timeBegin;
Runtime runtime;
public Debug(PrintWriter out) {
oj = System.getProperty("ONLINE_JUDGE") != null;
this.out = out;
this.timeBegin = System.currentTimeMillis();
this.runtime = Runtime.getRuntime();
}
public Debug() {
system = true;
oj = System.getProperty("ONLINE_JUDGE") != null;
OutputStream outputStream = System.out;
this.out = new PrintWriter(outputStream);
this.timeBegin = System.currentTimeMillis();
this.runtime = Runtime.getRuntime();
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar;
private int pnumChars;
public FastReader(InputStream stream) {
this.stream = stream;
}
private int pread() {
if (pnumChars == -1) {
throw new InputMismatchException();
}
if (curChar >= pnumChars) {
curChar = 0;
try {
pnumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (pnumChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = pread();
while (isSpaceChar(c))
c = pread();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = pread();
}
int res = 0;
do {
if (c == ',') {
c = pread();
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = pread();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString() {
int c = pread();
while (isSpaceChar(c))
c = pread();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = pread();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -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 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
FastReader sc = new FastReader();
int t = sc.nextInt();
// while (t > 0) {
String s = sc.nextLine();
char[] arr = s.toCharArray();
int ans =0;
for (int i = 0; i < arr.length; i++) {
if (i != arr.length - 1) {
if (arr[i] != arr[i + 1]) {
i++;
}
}
ans++;
}
System.out.println(ans);
// t--;
// }
}
//Fast Reader Class
static class FastReader {
BufferedReader reader;
StringTokenizer mStringTokenizer;
public FastReader() {
reader = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (mStringTokenizer == null || !mStringTokenizer.hasMoreElements()) {
try {
mStringTokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return mStringTokenizer.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 = reader.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())
t=list(input())
for i in range(len(t)-1):
if t[i:i+2]==['U','R'] or t[i:i+2]==['R','U']:
t[i:i+2]=['D']
print(len(t))
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | a = int(raw_input())
b = raw_input()
i = 0
while i < len(b)-1:
if b[i:i+2] in ["UR","RU"]:
i+=1
a-=1
i+=1
print a
| PYTHON |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | //All CopyRights reserved GeeksforGeeks.com
//Under the fair use(free use) Copyrights exception;
//some codes are copied.
import java.io.*;
import java.util.*;
import javax.xml.stream.events.Characters;
public class Main {
public static void main(String[] args)throws IOException {
//the problem is about inscribt cirlces
BufferedReader BR=new BufferedReader(new InputStreamReader(System.in));
PrintWriter PW=new PrintWriter(new PrintStream(System.out));
In IN=new In(BR);
Out OUT=new Out(PW);
Scanner SC=new Scanner(System.in);
//DataStructure DS=new DataStructure();
//Functions F=new Functions();
//QuickSort QS=new QuickSort();
//BinarySearch BS=new BinarySearch();
//FIB fubbonanci=new FIB(93);
/*int n = IN.nextInt();
int m = IN.nextInt();
String arr[][]=new String[n][m];
for (int i = 0; i < n; i++) {
arr[i] = IN.reader.readLine().split(" ");
}*/
int n = IN.nextInt();
String kk=IN.next();
StringBuilder sb = new StringBuilder(kk);
if(sb.length()>3) {
for (int i = 0; i <sb.length()-2; i++) {
if(sb.charAt(i)=='U'&&sb.charAt(i+1)=='R'&&sb.charAt(i+2)=='U') {sb.setCharAt(i, 'X');sb.setCharAt(i+1, 'Y');}
else if(sb.charAt(i)=='R'&&sb.charAt(i+1)=='U'&&sb.charAt(i+2)=='R') {sb.setCharAt(i, 'L');sb.setCharAt(i+1, 'M');}
}
String l =new String(sb);
l=l.replaceAll("XY", "D");l=l.replaceAll("LM", "D");System.out.println(l.replaceAll("UR", "D").replaceAll("RU", "D").length());
}
else {String l = new String(sb);l=l.replaceAll("UR", "D");l=l.replaceAll("RU", "D");System.out.println(l.length());}
}
protected static class In {
private BufferedReader reader;
private StringTokenizer tokenizer = new StringTokenizer("");
public In(BufferedReader reader) {
this.reader = reader;
}
public String next() throws IOException {
while (!tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public int[] nextIntArray1(int n) throws IOException {
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++)
a[i] = nextInt();
return a;
}
public int[] nextIntArraySorted(int n) throws IOException {
int[] a = nextIntArray(n);
Random r = new Random();
for (int i = 0; i < n; i++) {
int j = i + r.nextInt(n - i);
int t = a[i];
a[i] = a[j];
a[j] = t;
}
Arrays.sort(a);
return a;
}
public long[] nextLongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public long[] nextLongArray1(int n) throws IOException {
long[] a = new long[n + 1];
for (int i = 1; i <= n; i++)
a[i] = nextLong();
return a;
}
public long[] nextLongArraySorted(int n) throws IOException {
long[] a = nextLongArray(n);
Random r = new Random();
for (int i = 0; i < n; i++) {
int j = i + r.nextInt(n - i);
long t = a[i];
a[i] = a[j];
a[j] = t;
}
Arrays.sort(a);
return a;
}
}
protected static class Out {
private PrintWriter writer;
private static boolean local = System
.getProperty("ONLINE_JUDGE") == null;
public Out(PrintWriter writer) {
this.writer = writer;
}
public void print(char c) {
writer.print(c);
}
public void print(int a) {
writer.print(a);
}
public void println(Object a) {
writer.println(a);
}
public void println(Object[] os) {
for (int i = 0; i < os.length; i++) {
writer.print(os[i]);
writer.print(' ');
}
writer.println();
}
public void println(int[] a) {
for (int i = 0; i < a.length; i++) {
writer.print(a[i]);
writer.print(' ');
}
writer.println();
}
public void println(long[] a) {
for (int i = 0; i < a.length; i++) {
writer.print(a[i]);
writer.print(' ');
}
writer.println();
}
public static void db(Object... objects) {
if (local)
System.out.println(Arrays.deepToString(objects));
}
}
public static class QuickSort{
protected static int [] intArray;
protected static long [] longArray;
protected static double [] DoubleArray;
protected static char [] charArray;
protected static int Size;
public QuickSort() {
}
protected static int partition(int arr[],int low,int high)
{
int pivot=arr[high];
int i=low-1;
int temp=0;
for (int j = low; j < high; ++j) {
if(arr[j]<=pivot)
{
++i;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}
protected static int partition(long arr[],int low,int high)
{
long pivot=arr[high];
int i=low-1;
long temp=0;
for (int j = low; j < high; ++j) {
if(arr[j]<=pivot)
{
++i;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}
protected static int partition(double arr[],int low,int high)
{
double pivot=arr[high];
int i=low-1;
double temp=0;
for (int j = low; j < high; ++j) {
if(arr[j]<=pivot)
{
++i;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}
protected static int partition(char arr[],int low,int high)
{
char pivot=arr[high];
int i=low-1;
char temp=0;
for (int j = low; j < high; ++j) {
if(arr[j]<=pivot)
{
++i;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}
protected static void sort(int arr[],int low, int high)
{
if(low < high)
{
int pi=partition(arr, low, high);
sort(arr, low, pi-1);
sort(arr, pi+1, high);
}
}
protected static void sort(long arr[],int low, int high)
{
if(low < high)
{
int pi=partition(arr, low, high);
sort(arr, low, pi-1);
sort(arr, pi+1, high);
}
}
protected static void sort(double arr[],int low, int high)
{
if(low < high)
{
int pi=partition(arr, low, high);
sort(arr, low, pi-1);
sort(arr, pi+1, high);
}
}
protected static void sort(char arr[],int low, int high)
{
if(low < high)
{
int pi=partition(arr, low, high);
sort(arr, low, pi-1);
sort(arr, pi+1, high);
}
}
protected static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
protected static void printArray(char arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
protected static void printArray(double arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
protected static void printArray(long arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
}
public static class Functions{
public Functions() {
}
protected static boolean [] sieveOfEratosthenesPrimes(int limit){
boolean prime[] = new boolean[limit+1];
for(int i=0;i<limit;i++)
prime[i] = true;
for(int p = 2; p*p <=limit; p++)
{
// If prime[p] is not changed, then it is a prime
if(prime[p] == true)
{
// Update all multiples of p
for(int i = p*2; i <= limit; i += p)
prime[i] = false;
}
}
return prime;
}
protected static boolean checkAnagram(String str1, String str2) {
int i=0;
for (char c : str1.toCharArray()) {
i = str2.indexOf(c, i) + 1;
if (i <= 0) { return false; }
}
return true;
}
protected static boolean contains(final int[] arr, final int key) {
return Arrays.stream(arr).anyMatch(i -> i == key);
}
public static boolean contains(final long[] arr, final long key) {
return Arrays.stream(arr).anyMatch(i -> i == key);
}
public static boolean contains(final double[] arr, final double key) {
return Arrays.stream(arr).anyMatch(i -> i == key);
}
protected static long NCR(long N, long R){
if(N < R)
return 0;
if(R == 0 || R == N)
return 1;
return NCR(N-1,R-1)+NCR(N-1,R);
}
protected static long NCR(int N, int R){
if(N < R)
return 0;
if(R == 0 || R == N)
return 1;
return NCR(N-1,R-1)+NCR(N-1,R);
}
protected static String changeCharInPosition(int position, char ch, String str){
char[] charArray = str.toCharArray();
charArray[position] = ch;
return new String(charArray);
}
protected static boolean isPrime(long n) {
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
long sqrtN = (long)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
protected static long Gcd(long p, long q) {
if (q == 0) return p;
else return Gcd(q, p % q);
}
protected static boolean isPrime(int n) {
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
int sqrtN = (int)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
}
public static class BinarySearch{
private static int middle;
private static int L;
private static int R;
public BinarySearch() {
}
protected static int Search(int [] arr,int element){
R=arr.length-1;
L=0;
return BS(arr, L, R, element);
}
protected static int Search(long [] arr,long element){
R=arr.length-1;
L=0;
return BS(arr, L, R, element);
}
protected static int Search(char [] arr,char element){
R=arr.length-1;
L=0;
return BS(arr, L, R, element);
}
protected static int Search(double [] arr,double element){
R=arr.length-1;
L=0;
return BS(arr, L, R, element);
}
protected static int BS(int [] arr,int L,int R,int element){
if(R>=L)
{
middle=L+(R-L)/2;
if(arr[middle]==element)
{
return middle;
}
else if(arr[middle]>element)
{
return BS(arr, L,middle-1, element);
}
else
{
return BS(arr, middle+1, R, element);
}
}
else
{
return -1;
}
}
protected static int BS(long [] arr,int L,int R,long element){
if(R>=L)
{
middle=L+(R-L)/2;
if(arr[middle]==element)
{
return middle;
}
else if(arr[middle]>element)
{
return BS(arr, L,middle-1, element);
}
else
{
return BS(arr, middle+1, R, element);
}
}
else
{
return -1;
}
}
protected static int BS(char [] arr,int L,int R,char element){
if(R>=L)
{
middle=L+(R-L)/2;
if(arr[middle]==element)
{
return middle;
}
else if(arr[middle]>element)
{
return BS(arr, L,middle-1, element);
}
else
{
return BS(arr, middle+1, R, element);
}
}
else
{
return -1;
}
}
protected static int BS(double [] arr,int L,int R,double element){
if(R>=L)
{
middle=L+(R-L)/2;
if(arr[middle]==element)
{
return middle;
}
else if(arr[middle]>element)
{
return BS(arr, L,middle-1, element);
}
else
{
return BS(arr, middle+1, R, element);
}
}
else
{
return -1;
}
}
}
public static class DataStructure{
public DataStructure() {
}
static void stack_push(Stack<Integer> stack)
{
for(int i = 0; i < 5; i++)
{
stack.push(i);
}
}
// Popping element from the top of the stack
static void stack_pop(Stack<Integer> stack)
{
System.out.println("Pop :");
for(int i = 0; i < 5; i++)
{
Integer y = (Integer) stack.pop();
System.out.println(y);
}
}
// Displaying element on the top of the stack
static void stack_peek(Stack<Integer> stack)
{
Integer element = (Integer) stack.peek();
System.out.println("Element on stack top : " + element);
}
// Searching element in the stack
static void stack_search(Stack<Integer> stack, int element)
{
Integer pos = (Integer) stack.search(element);
if(pos == -1)
System.out.println("Element not found");
else
System.out.println("Element is found at position " + pos);
}
}
public static class FIB {
protected static int MAX = 1000;
protected static long f[];
protected static int size=0;
// Returns n'th fibonacci number using
// table f[]
public FIB(int n)
{
f=new long[MAX];
this.size=n;
fib();
}
protected static void fib() {
f[0]=0;f[1]=1;
for (int i = 2; i < size; i++) {
//if(f[i-1]+f[i-2]>Long.MAX_VALUE){f[i]=0;continue;}
f[i]=f[i-1]+f[i-2];
}
}
protected static void printSeries(){
for (int i = 0; i <size; i++) {
System.out.print(f[i]+" ");
}
}
}
public static class Graph{
}
public static class StringPermutationGenerator {
// Generate all permutations of a string in Java
private Set<String> generatePermutations(String input) {
input = input.toLowerCase();
Set<String> result = new HashSet<>();
permutations("", input, result);
return result;
}
private void permutations(String prefix, String letters, Set<String> result) {
if (letters.length() == 0) {
result.add(prefix);
} else {
for (int i = 0; i < letters.length(); i++) {
String letter = letters.substring(i, i + 1);
String otherLetters = letters.substring(0, i) + letters.substring(i + 1);
permutations(prefix + letter, otherLetters, result);
}
}
}
}
}
/*
Some References for doing things
-Removing all Leading zeros in a string
s.replaceFirst("^0+(?!$)", "");
-Creating comprator
public static class CustomComparator implements Comparator<c> {
@Override
public int compare(c o1, c o2) {
return o1.score.compareTo(o2.score);
}
}
-how to write switch case
switch(x)
{
case 1:
return l;
...
default:
return m;
}
-Convert numbers of and to different bases
Integer.toString(Integer.parseInt(number, base1), base2);
-Split over white spaces:
myString.split("\\s+");
-how to know if 4 points will form a square or a rectangle:
public static class point{
int a,b;
public point(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public String toString() {
return "point{" + "a=" + a + ", b=" + b + '}';
}
}
public static int distSq(point p, point q)
{
return (p.a - q.a)*(p.a - q.a) +
(p.b - q.b)*(p.b - q.b);
}
public static boolean isSquare(point p1, point p2, point p3, point p4)
{
int d2 = distSq(p1, p2); // from p1 to p2
int d3 = distSq(p1, p3); // from p1 to p3
int d4 = distSq(p1, p4); // from p1 to p4
// If lengths if (p1, p2) and (p1, p3) are same, then
// following conditions must met to form a square.
// 1) Square of length of (p1, p4) is same as twice
// the square of (p1, p2)
// 2) p4 is at same distance from p2 and p3
if (d2 == d3 && 2*d2 == d4)
{
int d = distSq(p2, p4);
return (d == distSq(p3, p4) && d == d2);
}
// The below two cases are similar to above case
if (d3 == d4 && 2*d3 == d2)
{
int d = distSq(p2, p3);
return (d == distSq(p2, p4) && d == d3);
}
if (d2 == d4 && 2*d2 == d3)
{
int d = distSq(p2, p3);
return (d == distSq(p3, p4) && d == d2);
}
return false;
}
public static boolean isRectangle(point p1,point p2,point p3,point p4)
{
double cx,cy;
double dd1,dd2,dd3,dd4;
double x1,y1,x2,y2,x3,y3,x4,y4;
x1=p1.a;y1=p1.b;x2=p2.a;y2=p2.b;x3=p3.a;y3=p3.b;x4=p4.a;y4=p4.b;
cx=(x1+x2+x3+x4)/4;
cy=(y1+y2+y3+y4)/4;
dd1=(cx-x1)*(cx-x1)+(cy-y1)*(cy-y1);
dd2=(cx-x2)*(cx-x2)+(cy-y2)*(cy-y2);
dd3=(cx-x3)*(cx-x3)+(cy-y3)*(cy-y3);
dd4=(cx-x4)*(cx-x4)+(cy-y4)*(cy-y4);
return dd1==dd2 && dd1==dd3 && dd1==dd4;
}
-left shift
//ex 00000001 << 3 == 00001000
*/ | JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int l;
string s, ss = "";
cin >> l;
cin >> s;
string ret = "";
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'R' && s[i + 1] == 'U') {
ss += 'D';
i++;
} else if (s[i] == 'U' && s[i + 1] == 'R') {
ss += 'D';
i++;
} else {
ss += s[i];
}
}
cout << ss.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 | n=int(input())
s=list(input())
for i in range(n-1):
if (s[i]=='U' and s[i+1]=="R") or (s[i]=='R' and s[i+1]=='U'):
s[i+1]="D"
print(len(s)-s.count("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 | #include <bits/stdc++.h>
int main() {
char a[1000];
int i, j, d, n, len, len2;
d = 0;
j = 0;
scanf("%d", &n);
getchar();
gets(a);
len = strlen(a);
len2 = len;
while (j < len - 1) {
if (a[j] == 'R' && a[j + 1] == 'U') {
len2 = len2 - 2;
d++;
j = j + 2;
} else if (a[j] == 'U' && a[j + 1] == 'R') {
len2 = len2 - 2;
d++;
j = j + 2;
} else
j++;
}
printf("%d", d + len2);
}
| 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 DiagonalWalking {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), i, l = n;
String s = in.next();
for (i = 0; i < n - 1; i++) {
if (s.charAt(i) == 'R' && s.charAt(i + 1) == 'U' || s.charAt(i) == 'U' && s.charAt(i + 1) == 'R') {
l--;
i++;
}
}
in.close();
System.out.println(l);
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.io.BufferedReader;
import java.io.InputStreamReader;
public class DiagonalWalking {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int amountOfMoves = Integer.parseInt(reader.readLine());
String moves = reader.readLine();
System.out.println(moves.replaceAll("RU|UR", "D").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 | from sys import stdin
lines = stdin.readlines()
n = lines[1][:-1]
m = int(lines[0])
i = 0
summ = 0
while i < m-1:
if n[i:i+2] == "RU" or n[i:i+2] == "UR":
summ += 1
i += 2
else:
i += 1
print m-summ | 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;
const int maxn = 105;
int n;
char a[maxn];
int main() {
cin >> n;
cin >> a;
int res = 0;
for (int i = 0; i < n - 1; ++i) {
if (a[i] != a[i + 1]) {
++i;
++res;
}
}
cout << n - res << endl;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = input()
s = raw_input()
i = 0
t = ["RU", "UR"]
while (i < len(s)):
if s[i:i+2] in t:
s = s[0:i] + "D" + s[i+2:len(s)]
i += 1
print len(s) | PYTHON |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.util.Scanner;
public class A954 {
public static int getOptimizedPath(String str, int len) {
int path = 0;
for (int i = 0; i < len; i++) {
char ch1 = str.charAt(i);
char ch2 = i < len-1 ? str.charAt(i+1) : ' ';
if ((ch1 == 'R' && ch2 == 'U') || (ch1 == 'U' && ch2 == 'R')) {
path++;
i++;
} else {
path++;
}
}
return path;
}
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int len = stdin.nextInt(); // Length
stdin.nextLine(); // Jump to next line
String input = stdin.nextLine(); // Input path
System.out.println(getOptimizedPath(input, len)); // Output optimized path
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.util.Scanner;
public class diagnola_walking {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int len = sc.nextInt();
String str = sc.next();
String ans="";
for(int i=0; i<str.length(); i++)
{
if(i+1!=str.length() && str.charAt(i)=='R' && str.charAt(i+1)=='U')
{
ans+='D';
i++;
}
else if(i+1!=str.length() && str.charAt(i)=='U' && str.charAt(i+1)=='R')
{
ans+='D';
i++;
}
else
{
ans+=str.charAt(i);
}
}
System.out.println(ans.length());
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
string st, s = "";
int n;
cin >> n;
cin >> st;
for (int i = 0; i < n; i++) {
if (st[i] == 'U' && st[i + 1] == 'R') {
s += "D";
i++;
} else if (st[i] == 'R' && st[i + 1] == 'U') {
s += "D";
i++;
} else
s += st[i];
}
cout << s.length() << endl;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n=int(input())
l=list(input())
i=0
count=0
while i<n-1:
if l[i]=="R" and l[i+1]=="U" or l[i]=="U" and l[i+1]=="R":
i=i+2
count+=1
else:
i+=1
print(n-count) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class tmp2{
public static void main(String[] args) throws IOException
{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int n = Integer.parseInt(bf.readLine());
String s = bf.readLine();
String ans ="";
for(int i=0; i<n; i++)
{
if(i+1<n)
{
if(s.charAt(i+1)=='U' && s.charAt(i)=='R')
{
ans += 'D';
i++;
}
else if(s.charAt(i)=='U' && s.charAt(i+1)=='R')
{
i++;
ans += 'D';
}
else
ans += s.charAt(i);
}
else
ans += s.charAt(i);
}
//pw.println(ans);
pw.println(ans.length());
pw.flush();
pw.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())
m=0
r=input()
i=0
while i <n-1:
if r[i]!=r[i+1]:
m+=1
i+=2
else:
i+=1
m+=1
if i==n-1:
m+=1
print(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 | n=int(raw_input())
y=raw_input()
i=0
r=n
while i<n-1:
if y[i]!=y[i+1]:
r-=1
i+=1
i+=1
print r | PYTHON |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a = 0;
vector<char> input(n);
for (int i = 0; i < n; i++) cin >> input[i];
for (int i = 1; i < n; i++) {
if (input[i] != input[i - 1]) {
i++;
a++;
}
}
cout << n - a;
}
| 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;
scanf("%d", &n);
string str, str2;
cin >> str;
for (int i(0); i < n; ++i) {
if (str[i] == 'U' && str[i + 1] == 'R') {
str2 += 'd';
str[i] = str[i + 1] = '1';
} else if (str[i] == 'R' && str[i + 1] == 'U') {
str2 += 'd';
str[i] = str[i + 1] = '1';
} else if (str[i] != '1')
str2 += str[i];
}
int siz = str2.length();
printf("%d", siz);
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, c = 0, i = 0;
string s;
cin >> n >> s;
while (i < s.size()) {
if (s[i] == 'U' && s[i + 1] == 'R') {
c++;
i += 2;
} else if (s[i] == 'R' && s[i + 1] == 'U') {
c++;
i += 2;
} else
i++;
}
cout << s.size() - c;
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
s = input()
if n == 1:
print(1)
else:
idx = 1
cnt = 0
while idx < n:
if s[idx] != s[idx-1]:
idx += 2
cnt += (1 if idx < n else 1 if idx > n else 2)
else:
idx += 1
cnt += (1 if idx < n else 2)
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 | x=int(input())
p=list(input())
t=list()
i=0
while i<x-1:
if p[i]!=p[i+1]:
t.append("D")
i+=1
i+=1
print(x-len(t)) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, cnt = 0;
string s;
cin >> n;
cin >> s;
for (int i = 0; i < n;) {
if (s[i] == 'U' && s[i + 1] == 'R' || s[i] == 'R' && s[i + 1] == 'U') {
cnt++;
i = i + 2;
} else {
cnt++;
i++;
}
}
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 | N=int(input()) #take the size
Array=list(map(str,input().split()[:N]))
s=''.join(Array)
l = len(s)
c=0
j=1
while j < l:
if s[j-1] == s[j]:
j=j+1
else:
c=c+1
j=j+2
print(l-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 n, ans;
string s;
int main() {
cin >> n >> s;
ans = n;
for (int i = 1; i < n; i++) {
if (s[i] == 'R' && s[i - 1] == 'U') {
ans--;
i++;
} else if (s[i] == 'U' && s[i - 1] == 'R') {
ans--;
i++;
}
}
cout << ans;
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
char a[maxn];
int main() {
int n;
cin >> n >> a;
int len = strlen(a);
for (int i = 1; i < len; ++i) {
if ((a[i - 1] == 'R' && a[i] == 'U') || (a[i - 1] == 'U' && a[i] == 'R')) {
i++;
n--;
}
}
cout << n << endl;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
s = input()
a = ''
for i in s:
if len(a) == 0 or a[-1] == 'D' or a[-1] == i:
a = a + i
else:
a = a[:-1] + 'D'
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 | import sys
n = int(input())
s = input()
s = s + '$'
res = ""
i = 0
while i<=n-1:
if s[i]=='R' and s[i+1]=='U':
res = res + 'D'
i = i + 2
elif s[i]=='U' and s[i+1]=='R':
res = res + 'D'
i = i + 2
else:
res = res + s[i]
i = i + 1
print(len(res))
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
l = list(input())
i = 0
while (i<=(len(l)-2)):
if(l[i]!=l[i+1]):
l = l[:i]+['D']+l[i+2:]
i+=1
print(len(l)) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #Diag
n=int(input())
st=input()
lenth=len(st)
i=0
sokr=0
while i < (len(st)-1):
if st[i:i+2] in ['UR','RU']:
st=st[0:i]+st[i+2:]
sokr+=1
continue
i+=1
print(lenth-sokr) | 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()
m = 0
i = 0
while i < n - 1:
if s[i] != s[i+1]:
m +=1
i +=2
else:
i += 1
print(n - 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 | #include <bits/stdc++.h>
using namespace std;
void debug(string S = "hello ! debug") {
cout << S << endl;
return;
}
template <class T>
void printVector(vector<T> &V) {
cout << endl;
for (int i = 0; i < V.size(); ++i) {
cout << V[i] << " ";
}
cout << endl;
return;
}
long long int sizeOfSt(long long int N) {
long long int x = (long long int)(ceil(log2(N)));
long long int max_size = 2 * (long long int)pow(2, x) - 1;
return max_size;
}
template <class T>
T maximumFromVector(vector<T> &V) {
T ans = V[0];
for (int i = 1; i < V.size(); ++i) {
ans = max(ans, V[i]);
}
return ans;
}
template <class T>
T minimumFromVector(vector<T> &V) {
T ans = V[0];
for (int i = 1; i < V.size(); ++i) {
ans = min(ans, V[i]);
}
return ans;
}
template <class T>
T vectorSum(vector<T> &V) {
T sum = 0;
for (long long int i = 0; i < V.size(); ++i) {
sum += V[i];
}
return sum;
}
int main() {
long long int n;
scanf("%lld", &n);
string S;
cin >> S;
long long int ans = S.length();
for (long long int i = 0; i < S.length() - 1; ++i) {
if (S[i] != S[i + 1]) {
--ans;
++i;
}
}
cout << ans;
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n, u, r, c;
string a;
int main() {
cin >> n;
cin >> a;
int i = 0;
while (i < n) {
if (a[i] != a[i + 1]) {
i += 2;
c++;
} else {
i++;
c++;
}
}
cout << c;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.util.*;
public class Main
{
public static void main(String argz[])
{
Scanner inp=new Scanner(System.in);
String s=new String();
int n=inp.nextInt();
s=inp.next();
char arr[]=s.toCharArray();
int a=s.length();
for(int i=0;i<(s.length()-1);i++)
{
if((arr[i]=='U' && arr[i+1]=='R')||(arr[i]=='R' && arr[i+1]=='U'))
{
a--;
i++;
}
}
System.out.println(a);
}
} | JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import os
import sys
debug = True
if debug and os.path.exists("input.in"):
input = open("input.in", "r").readline
else:
debug = False
input = sys.stdin.readline
def inp():
return (int(input()))
def inlt():
return (list(map(int, input().split())))
def insr():
s = input()
return s[:len(s) - 1] # Remove line char from end
def invr():
return (map(int, input().split()))
test_count = 1
if debug:
test_count = inp()
for t in range(test_count):
if debug:
print("Test Case #", t + 1)
# Start code here
n = inp()
a = insr()
d = 0
last = ""
for i in range(n):
ch = a[i]
if last == "U" and ch == "R":
d += 1
last = ""
continue
if last == "R" and ch == "U":
d += 1
last = ""
continue
last = ch
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 | import re
print input()-len(re.findall('RU|UR',raw_input())) | 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 = list(input())
length = 0
iGen = (i for i in range(0, n))
try:
for i in iGen:
if i < n - 1 and s[i] != s[i + 1]:
next(iGen, None)
length += 1
except StopIteration:
pass
print(length)
| 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, c = 1;
bool prev = false;
string s;
cin >> n >> s;
for (int i = 1; i < s.length(); i++)
if (!(!prev && ((s[i] == 'R' && s[i - 1] == 'U') ||
(s[i] == 'U' && s[i - 1] == 'R')))) {
c++;
prev = false;
} else {
prev = true;
}
cout << (n > 0 ? c : 0);
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.util.*;
public class CF954A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
String s = sc.nextLine();
int count = 0;
for(int i = 0; i < s.length() - 1; i++) {
if(s.substring(i,i+2).equals("RU") || s.substring(i,i+2).equals("UR")){
count++;
i++;
}
}
System.out.println(n-count);
}
} | JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
s = list(str(input()))
d = 0
for i in range(n-1):
if s[i] != s[i+1] and s[i] != "*":
d += 1
s[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 | import re
a= input()
text=input()
rep={"RU":"D","UR":"D"}
rep = dict((re.escape(k), v) for k, v in rep.items())
pattern = re.compile("|".join(rep.keys()))
text = pattern.sub(lambda m: rep[re.escape(m.group(0))], text)
print(len(text)) | 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 b = 0, i, j, n, m;
cin >> n;
char a[n + 5];
for (i = 0; i < n; i++) {
cin >> a[i];
}
for (i = 0; i < n; i++) {
if ((a[i] == 'U' && a[i + 1] == 'R') || (a[i] == 'R' && a[i + 1] == 'U')) {
b++;
i++;
}
}
cout << n - b;
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 | # your code goes here
a = input()
b = raw_input()
c = 0
i = 0
while i < a-1:
if (b[i] != b[i+1]):
c +=1
i = i+1
i = i+1
print(a-c)
| PYTHON |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
static long time_start;
static int cas = 0;
static int mod = (int) 12357;
static int inf = 0x3f3f3f3f;
static long inff = 0x3f3f3f3f3f3f3f3fL;
static int maxn = (int) 150000 + 5;
static long[] ans = new long[maxn];
static void solve() throws Exception {
int n = in.nextInt();
String str = in.next();
int res = str.replaceAll("UR|RU", "D").length();
out.println(res);
}
static void swap(int[] arr, int i, int j) {
if (i != j) {
arr[i] ^= arr[j];
arr[j] ^= arr[i];
arr[i] ^= arr[j];
}
}
public static void main(String[] args) throws Exception {
openIO();
solve();
closeIO();
}
static void openIO() throws Exception {
try {
time_start = 0;
FileInputStream fi = new FileInputStream("night_input");
System.setIn(fi);
time_start = System.currentTimeMillis();
} catch (Exception ex) {
//hello night_13
}
in = new InputReader(System.in);
out = new PrintWriter(new BufferedOutputStream(System.out));
}
static void closeIO() throws IOException {
out.flush();
if (time_start != 0) {
out.println();
out.print("time cost: ");
out.print(System.currentTimeMillis() - time_start);
out.println(" ms");
}
in.close();
out.close();
}
}
class InputReader {
private BufferedReader br;
private StringTokenizer st;
public InputReader(InputStream stream) throws IOException {
br = new BufferedReader(new InputStreamReader(stream));
eat("");
}
private void eat(String s) {
st = new StringTokenizer(s);
}
public String nextLine() throws IOException {
return br.readLine();
}
public boolean hasNext() throws IOException {
while (!st.hasMoreTokens()) {
String s = nextLine();
if (s == null) return false;
eat(s);
}
return true;
}
public String next() throws IOException {
hasNext();
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
public BigDecimal nextBigDecimal() throws IOException {
return new BigDecimal(next());
}
public int[] nextIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; ++i) {
arr[i] = nextInt();
}
return arr;
}
public double[] nextDoubleArray(int n) throws IOException {
double[] arr = new double[n];
for (int i = 0; i < n; ++i) {
arr[i] = nextDouble();
}
return arr;
}
public char[] nextCharArray() throws IOException {
return next().toCharArray();
}
public int[] nextIntArray(int n, int st) throws IOException {
n += st;
int[] arr = new int[n];
for (int i = st; i < n; ++i) {
arr[i] = nextInt();
}
return arr;
}
public double[] nextDoubleArray(int n, int st) throws IOException {
n += st;
double[] arr = new double[n];
for (int i = st; i < n; ++i) {
arr[i] = nextDouble();
}
return arr;
}
public char[] nextCharArray(int st) throws IOException {
String temp = "";
while (st-- > 0) temp += " ";
return (temp + next()).toCharArray();
}
public void close() throws IOException {
br.close();
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
string str;
int l, c = 0;
cin >> l >> str;
for (int i = 0; i < (str.size() - 1); i++) {
if (str[i] == 'U' && str[i + 1] == 'R')
c++, i++;
else if (str[i] == 'R' && str[i + 1] == 'U')
c++, i++;
}
cout << l - c << endl;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.util.Scanner;
public class Main {
public static void main (String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
char[] in = sc.nextLine().toCharArray();
char prev = 'D';
int sub = 0;
for(char curr : in){
if(prev != 'D' && curr != prev){
sub++;
prev = 'D';
}
else{
prev = curr;
}
}
System.out.println(n - sub);
}
} | 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.IOException;
import java.util.*;
public class d {
public static void main(String[] args) throws IOException {
Scanner s = new Scanner(System.in);
// BufferedReader s=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
StringBuilder sb1=new StringBuilder();
int n=s.nextInt();
char[] a=s.next().toCharArray();int ans=0;
for(int i=0;i<n-1;i++){
if(a[i]!=a[i+1]){
ans++;i++;
}
}
System.out.println(n-ans);
}
static void computeLPSArray(String pat, int M, int lps[]) {
int len = 0;
int i = 1;
lps[0] = 0;
while (i < M) {
if (pat.charAt(i) == pat.charAt(len)) {
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0) {
len = lps[len - 1];
}
else
{
lps[i] = len;
i++;
}
}
}
}
static boolean isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static long powerwithmod(long x, long y, int p) {
long res = 1;
x = x % p;
while (y > 0) {
if ((y & 1) == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long powerwithoutmod(long x, int y) {
long temp;
if( y == 0)
return 1;
temp = powerwithoutmod(x, y/2);
if (y%2 == 0)
return temp*temp;
else
{
if(y > 0)
return x * temp * temp;
else
return (temp * temp) / x;
}
}
static void fracion(double x) {
String a = "" + x;
String spilts[] = a.split("\\."); // split using decimal
int b = spilts[1].length(); // find the decimal length
int denominator = (int) Math.pow(10, b); // calculate the denominator
int numerator = (int) (x * denominator); // calculate the nerumrator Ex
// 1.2*10 = 12
int gcd = (int) gcd((long) numerator, denominator); // Find the greatest common
// divisor bw them
String fraction = "" + numerator / gcd + "/" + denominator / gcd;
// System.out.println((denominator/gcd));
long x1 = modInverse(denominator / gcd, 998244353);
// System.out.println(x1);
System.out.println((((numerator / gcd) % 998244353 * (x1 % 998244353)) % 998244353));
}
static int bfs(int i1, ArrayList<Integer>[] h, int[] vis, int n,int val1) {
Queue<Integer> q = new LinkedList<Integer>();
q.add(i1);Queue<Integer> aq=new LinkedList<Integer>();
aq.add(0);
while(!q.isEmpty()){
int i=q.poll();
int val=aq.poll();
if(i==n){
return val;
}
if(h[i]!=null){
for(Integer j:h[i]){
if(vis[j]==0){
q.add(j);vis[j]=1;
aq.add(val+1);}
}
}
}return -1;
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long modInverse(long a, int m)
{
return (powerwithmod(a, m - 2, m));
}
static int MAXN=100001;
static int[] spf=new int[MAXN];
static void sieve() {
spf[1] = 1;
for (int i=2; i<MAXN; i++)
spf[i] = i;
for (int i=4; i<MAXN; i+=2)
spf[i] = 2;
for (int i=3; i*i<MAXN; i++)
{
if (spf[i] == i)
{
for (int j=i*i; j<MAXN; j+=i)
if (spf[j]==j)
spf[j] = i;
}
}
}
static Vector<Integer> getFactorizationUsingSeive(int x) {
Vector<Integer> ret = new Vector<Integer>();
while (x != 1)
{
ret.add(spf[x]);
x = x / spf[x];
}
return ret;
}
/* static long[] fac = new long[MAXN+1];
static void calculatefac(int mod){
for (int i = 1 ;i <= MAXN; i++)
fac[i] = fac[i-1] * i % mod;
}
static long nCrModPFermat(int n, int r, int mod) {
if (r == 0)
return 1;
fac[0] = 1;
return (fac[n]* modInverse(fac[r], mod)
% mod * modInverse(fac[n-r], mod)
% mod) % mod;
} */}
class Student {
int l;int r;
public Student(int l, int r) {
this.l = l;
this.r = r;
}
public String toString()
{
return this.l+" ";
}
}
class Sortbyroll implements Comparator<Student> {
public int compare(Student a, Student b){
if(a.l==b.l) return a.r-b.r;
return b.l-a.l; }
}
class Sortbyroll1 implements Comparator<Student> {
public int compare(Student a, Student b){
if(a.r==b.r) return a.l-b.l;
return b.l-a.l;
}
} | JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
s = input()
i=1
ans=0
while i < n:
if s[i]!=s[i-1]:
ans+=1
i+=1
i+=1
print(n-ans) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(raw_input())
s = raw_input()
i = 0
cnt = 0
while i < n:
cnt += 1
if s[i:i+2] in ('RU', 'UR'):
i += 2
else:
i += 1
print cnt
| 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())
arr = input()
count = n
i = 0
while i < n - 1:
if arr[i] != arr[i + 1]:
count -= 1
i += 2
else:
i += 1
print(count)
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.io.*;
import java.util.Random;
public class Application {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
in.readLine();
String s = in.readLine();
s += "$";
int res = 0;
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) == 'R' && s.charAt(i + 1) == 'U') {
i++;
} else if (s.charAt(i) == 'U' && s.charAt(i + 1) == 'R') {
i++;
}
res++;
}
out.write(String.valueOf(res));
in.close();
out.close();
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
s = input() + "A"
cnt = 0
i = 0
while i < n:
if s[i] == "U" and s[i + 1] == "R":
cnt += 1
i += 2
elif s[i] == "R" and s[i + 1] == "U":
cnt += 1
i += 2
else:
i += 1
print(n - cnt) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
s = input()
l = 0
i = len(s)
while i>=1:
if (s[i-1] == "U" and s[i -2] == "R") or (s[i-1] == "R" and s[i -2] == "U"):
i = i -2
l = l + 1
else:
i = i -1
l = l + 1
print(l) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int count = 0;
for (int i = 0; i < s.size() - 1; i++) {
if ((s[i] == 'R' && s[i + 1] == 'U') || (s[i] == 'U' && s[i + 1] == 'R')) {
count++;
i += 1;
}
}
cout << s.size() - count;
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.io.*;
import java.util.*;
public class Codeforces {
public static long log2(long x)
{
long p=1;
long ans=0;
while(x>=p)
{
p*=2;
ans++;
}
return ans-1;
}
public static void extendedgcd(int a, int b)
{
x=0;
y=0;
gc=0;
xgcd(a,b);
pw.println(a+"*"+x+" + "+b+"*"+y+" = "+gc);
}
public static long modex(long a, long b, long c)
{
if(b==0)
return 1;
else if(b==1)
return a%c;
else if(b%2==0)
{
return modex((a*a)%c,b/2,c);
}
else
{
return (a*modex((a*a)%c,b/2,c))%c;
}
}
public static void xgcd(int a, int b)
{
if(b==0)
{
x=1;
y=0;
gc=a;
}
else
{
xgcd(b,a%b);
int temp = x;
x=y;
y = temp - ((a/b)*y);
}
}
public static long gcd(long a, long b)
{
if(b==0)return a;
else return gcd(b,a%b);
}
static int x,y,gc;
static HashSet<Character> set;
public static long expmod(long a, long b, long mod)
{
if(b==0)return 1;
if(b==1)return a;
if(b%2==0)
{
return ((expmod(a,b/2,mod)%mod)*(expmod(a,b/2,mod)%mod))%mod;
}
else
{
return ((a%mod)*(expmod(a,b/2,mod)%mod)*(expmod(a,b/2,mod)%mod))%mod;
}
}
static class Comp implements Comparator<Point>
{
@Override
public int compare(Point arg0, Point arg1) {
if(arg0.x>arg1.x)return 1;
if(arg0.x<arg1.x)return -1;
return 0;
}
}
static class STComp implements Comparator<Integer>
{
@Override
public int compare(Integer arg0, Integer arg1) {
return arg0.compareTo(arg1);
}
}
static class Point
{
int x,y,ind;
public Point(int a, int b)
{
x=a;
y=b;
ind=0;
}
@Override
public boolean equals(Object o)
{
// If the object is compared with itself then return true
if (o == this) {
return true;
}
/* Check if o is an instance of Complex or not
"null instanceof [type]" also returns false */
if (!(o instanceof Point)) {
return false;
}
// typecast o to Complex so that we can compare data members
Point c = (Point) o;
// Compare the data members and return accordingly
return x==c.x&&y==c.y;
}
}
public static boolean issub(String s1, String s2)
{
if(s1.length()>s2.length())
return false;
int l1 = s1.length();
int l2 = s2.length();
for(int i=0;i<=l2-l1;i++)
{
String sub = s2.substring(i, i+l1);
if(s1.equals(sub))return true;
}
return false;
}
public static String enc(String s, int n)
{
String s1 = s.substring(0,n);
String s2 = s.substring(n, s.length());
String re="";
for(int i=s1.length()-1;i>=0;i--)re+=s1.charAt(i);
return re+s2;
}
static int vis[];
public static void dfs(int x)
{
vis[x]=1;
ArrayList<Integer> li = adj[x];
for(int ch:li)
{
if(vis[ch]==0)dfs(ch);
}
}
/*
public static void dfsm(int x, HashSet<Integer> set)
{
vis2[x]=1;
ArrayList<Integer> li = adj[x];
for(int ch:li)
{
//if(!set.contains(ch))continue;
if(vis2[ch]==0)dfsm(ch,set);
}
}
*/
public static void countPaths(int s, int d, ArrayList<Integer> pa)
{
vis2[s]=1;
if(s==d)
{
pa.add(s);
// paths[ct] = new ArrayList<Integer>(pa);
ct++;
}
else
{
ArrayList<Integer> li = adj[s];
pa.add(s);
for(int ad: li)
{
if(vis2[ad]==0)countPaths(ad,d,pa);
}
}
if(pa.size()>1)pa.remove(pa.size()-1);
vis2[s]=0;
return ;
}
public static boolean dif(String s1, String s2)
{
int ct=0;
if(s1.length()!=s2.length())return false;
for(int i=0;i<s1.length();i++)
{
if(s1.charAt(i)!=s2.charAt(i))ct++;
}
if(ct==1)return true;
else return false;
}
static void add(int arr[], int N, int lo,
int hi, int val)
{
arr[lo] += val;
if (hi != N - 1)
arr[hi + 1] -= val;
}
// Utility method to get actual array from
// operation array
static void updateArray(int arr[], int N)
{
// convert array into prefix sum array
for (int i = 1; i < N; i++)
arr[i] += arr[i - 1];
}
// method to print final updated array
static void printArr(int arr[], int N)
{
updateArray(arr, N);
for (int i = 0; i < N; i++)
System.out.print(""+arr[i]+" ");
System.out.print("\n");
}
static int cyc;
public static void dfsmod(int v, HashSet<Integer> set)
{
vis2[v]=1;
ArrayList<Integer> li = adj[v];
set.add(v);
for(int ch: li)
{
if(set.contains(ch))
{
cyc=1;
pw.println("** "+ch+" "+v);
}
if(vis2[ch]==0)dfsmod(ch,set);
}
set.remove(v);
}
public static boolean isps(int x)
{
int sq = (int)Math.sqrt(x);
return sq>0&&sq*sq==x;
}
public static void combinations(String suffix,String prefix)
{
if(prefix.length()<0)return;
pw.println(suffix);
for(int i=0;i<prefix.length();i++)
combinations(suffix+prefix.charAt(i),prefix.substring(i+1,prefix.length()));
}
public static void topo(int x)
{
vis2[x]=1;
// pw.println("vis "+x);
ArrayList<Integer> li = adj[x];
// pw.println(x+" has siz "+li.size());
// Integer i = x;
for(int ch: li)
{
if(vis2[ch]==0)
{topo(ch);
//pw.println(x+" "+ch);
}
}
st.push(x);
//pw.println(i+" pushed");
}
static ArrayList<Integer>[] adj;
static int[] vis2;
static int ct;
public static void ch(Stack<Integer> s)
{
s.push(4);
return;
}
static Stack<Integer> st;
static class PQnode implements Comparable<PQnode>
{
int value;
int distance;
public PQnode(int x, int y)
{
value=x;
distance=y;
}
@Override
public int compareTo(PQnode o) {
return Integer.compare(this.distance, o.distance);
}
@Override
public boolean equals(Object o)
{
if(o==this)return true;
if(!(o instanceof PQnode))
{
return false;
}
PQnode node = (PQnode) o;
return value==node.value&&distance==node.distance;
}
}
static class Compq implements Comparator<PQnode>
{
public int compare(PQnode arg0, PQnode arg1)
{
if(arg0.distance>arg1.distance)return 1;
if(arg0.distance<arg1.distance)return -1;
return 0;
}
}
public static void printpa(int pa[],int x)
{
if(pa[x]==-1)
pw.println(x);
else
{
printpa(pa, pa[x]);
pw.println(x);
}
}
static class Node
{
int data;
Node next;
public Node(int x)
{
data =x;
next=null;
}
}
static Node he;
static Node rev(Node n)
{
if(n.next==null)
{
he=n;
return n;
}
else
{
Node rt = rev(n.next);
rt.next=n;
return n;
}
}
public static int trap(final int[] A) {
int n = A.length;
int arr[] = new int[A.length];
for(int i=0;i<A.length;i++)arr[i]=A[i];
int nge[] = new int[A.length];
Stack<Point> st = new Stack<Point>();
//int ip=0;
st.push(new Point(arr[0],0));
for(int i=1;i<n;i++)
{
Point p = new Point(arr[i],i);
while(!st.isEmpty()&&p.x>=st.peek().x)
{
Point pop = st.pop();
nge[pop.y]=p.y;
}
st.push(p);
}
while(!st.isEmpty())
{
Point p = st.pop();
nge[p.y]=-1;
}
int ans=0;
for(int i=0;i<n;i++)System.out.print(nge[i]+" ");
pw.println();
for(int i=0;i<n;)
{
if(arr[i]!=0)
{
int ng = nge[i];
if(ng==-1)
{
int j=i+1;
while(j<n&&arr[j]==0)j++;
if(j!=n)
{int ad = (j-i-1)*arr[j];
ans+=ad;
System.out.println("1* "+i+" "+j+" "+ad);
i=j;
}
}
else
{
int sub=0;
int j = i+1;
while(j<n&j<ng)
{
sub+=arr[j];
j++;
}
int ad = (arr[i]*(ng-i-1))-sub;
System.out.println("2* "+i+" "+j+" "+ng+" "+ad);
ans += ad;
i=ng;
}
}
else
{
i++;
}
}
return ans;
}
public static int round(int st, int end, int a, int b)
{
int mid = (st+end)/2;
if(a<=mid&&b>=mid+1)return 0;
else
{
if(a<=mid&&b<=mid)
{
return 1+round(st,mid,a,b);
}
else
{
return 1+round(mid+1,end,a,b);
}
}
}
public static void cha(Point p)
{
p.x=10;
p.y=5;
}
public static void main(String[] args) throws IOException
{
int n = sc.nextInt();
String st=sc.nextLine();
String ans="";
for(int i=0;i<n;i++)
{
char c = st.charAt(i);
if(c=='R')
{
if(i+1<n&&st.charAt(i+1)=='U')
{
ans+="D";
i++;
}
else
{
ans+="R";
}
}
if(c=='U')
{
if(i+1<n&&st.charAt(i+1)=='R')
{
ans+="D";
i++;
}
else
{
ans+="U";
}
}
}
pw.println(ans.length());
/*
HashMap<Integer,Integer> ts = new HashMap<Integer,Integer>();
ts.put(8,1);
ts.put(3,1);
ts.put(5,1);
int arr[] = new int[ts.size()];
Object[] ar2 = ts.keySet().toArray();
int ar3[] = new int[ar2.length];
for(int i=0;i<ar2.length;i++)
{
ar3[i] = (int)ar2[i];
pw.println(ar3[i]);
}
int d = 5;
int m = 1<<2;
pw.println(m&d);
*/
//int[] arr = ts.toArray(new int[ts.size()]);
/*int dist = (be-ae)/2;
int ct=0;
while(dist!=0)
{
dist/=2;
ct++;
}
int rn = ct+1;
pw.println(l2);
if(rn==l2)
{
pw.println("Final!");
}
else
{
pw.println(rn);
}
*/
/*int a[] = { 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
for(int i=0;i<a.length;i++)pw.print(a[i]+" ");
pw.println();
trap(a);*/
/*
int n = sc.nextInt();
int a = sc.nextInt();
int b = sc.nextInt();
long sum=0;
int arr[] = new int[n];
for(int i=0;i<n;i++)
{ arr[i]=sc.nextInt();
sum+=arr[i];
}
int as = (arr[0]*a)/b;
int bl[] = new int[n-1];
for(int i=1;i<n;i++)
{
bl[i-1]=arr[i];
}
Arrays.sort(bl);
if(sum<=as)
{
pw.println(0);
pw.close();
return;
}
int ans=0;
for(int i=n-2 ;i>=0;i--)
{
sum = sum-bl[i];
ans++;
if(sum<=as)break;
}
pw.println(ans);
*/
//pw.println(ans);
/*int n =sc.nextInt();
int k = sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++)arr[i]=sc.nextInt();
int pref[] = new int[n];
for(int i=0;i<n;i++)
{
if(i==0)
{
pref[i]=arr[i];
}
else
{
pref[i]=pref[i-1]+arr[i];
}
}
double ans = Float.MIN_VALUE;
for(int i=k;i<=n;i++)
{
int p1 = 0;
int p2= i-1;
while(p2<=n)
{
//pw.println("calc "+p1+" "+p2);
double rsum = (p1==0)?pref[p2]:pref[p2]-pref[p1-1];
double avg = rsum/i;
//pw.println(i);
ans = Math.max(ans, avg);
p1++;
p2++;
//pw.println("********");
if(p2==n)break;
}
}
pw.println(ans);
*/
/*
*/
/*
Node n1 = new Node(1);
Node n2 = new Node(2);
Node n3 = new Node(3);
n1.next=n2;
n2.next=n3;
n3.next=null;
rev(n1);
while(he!=null)
{
pw.println(he.data);
he=he.next;
}
*/
/*
int n = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
String st = sc.nextLine();
int ind[] = new int[n];
int indct=0;
for(int i=0;i<st.length();i++)
{
if(st.charAt(i)=='1')
{
ind[indct++]=i;
}
}
int r=0;
if(indct==0||indct==1)
{
if(indct==0)
{
pw.println(y);
pw.close();
return;
}
else
{
r=1;
}
}
else
{
for(int i=0;i<indct-1;i++)
{
if(ind[i+1]-ind[i]>1)r++;
}
if(r==0)
{
if(r==0)
{
pw.println(0);
pw.close();
return;
}
}
r++;
}
//pw.println("r "+r);
long ans = Long.MAX_VALUE;
for(int rev=0;rev<=r;rev++)
{
long left = r-rev;
long p = (left+1)*y + x*rev;
//pw.println(" p "+p);
ans = Math.min(p, ans);
}
pw.println(ans);
*/
/*PriorityQueue<PQnode> pq = new PriorityQueue<PQnode>();
pw.println(pq.size());
pq.add(new PQnode(1,2));
pq.add(new PQnode(3,4));
pw.println(pq.size());
pq.remove(new PQnode(1,2));
pw.println(pq.size());
int parent[] = new int[4];
Arrays.fill(parent, -1);
ArrayList<Point> adj[] = new ArrayList[4];
for(int i=0;i<4;i++)adj[i] = new ArrayList<Point>();
adj[0].add(new Point(1,10));
adj[0].add(new Point(2,20));
adj[1].add(new Point(0,10));
adj[1].add(new Point(2,5));
adj[1].add(new Point(3,16));
adj[2].add(new Point(0,20));
adj[2].add(new Point(1,5));
adj[2].add(new Point(3,20));
adj[3].add(new Point(1,16));
adj[3].add(new Point(2,20));
PriorityQueue<PQnode> pq = new PriorityQueue<PQnode>(new Compq());
HashSet<Integer> pres = new HashSet<Integer>();
int[] dist = new int[4];
for(int i=0;i<4;i++)
{
if(i==0)
{
pq.add(new PQnode(i,0));
dist[i]=0;
}
else
{
pq.add(new PQnode(i,Integer.MAX_VALUE));
dist[i]=Integer.MAX_VALUE;
}
pres.add(i);
}
while(!pq.isEmpty())
{
PQnode pop = pq.poll();
pres.remove(pop.value);
// pw.println(pop.value+" "+pop.distance);
dist[pop.value]=pop.distance;
for(Point p: adj[pop.value])
{
if(pres.contains(p.x))
{
if(p.y+dist[pop.value]<dist[p.x])
{
pw.println("ha");
pq.remove(new PQnode(p.x,dist[p.x]));
dist[p.x]=p.y+dist[pop.value];
pq.add(new PQnode(p.x,p.y+dist[pop.value]));
parent[p.x]=pop.value;
}
}
}
}
for(int i=0;i<4;i++)pw.print(dist[i]+" ");
pw.println();
printpa(parent, 3 );
Dijkstra over.
*/
/*
st = new Stack<Integer>();
cyc=0;
adj = new ArrayList[4];
vis2 = new int[4];
Arrays.fill(vis2, 0);
for(int i=0;i<4;i++)adj[i] = new ArrayList<Integer>();
adj[0].add(1);
adj[0].add(2);
adj[0].add(3);
adj[1].add(3);
//adj[2].add(0);
adj[2].add(1);
//adj[3].add(2);
st = new Stack<Integer>();
topo(0);
while(!st.isEmpty())pw.println(st.pop());
*/
/*
int n = sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++)arr[i]=sc.nextInt();
long ansg =0,ansv = Long.MAX_VALUE;
int arr2[] = new int[n];
for(int i=0;i<n;i++)arr2[i]=arr[i]-i;
for(int i=0;i<n;i++)
{
int x = arr2[i];
if(x<=0)
{
if(i<ansv)
{
ansg=i;
ansv=i;
}
}
else
{
float t = ((float)x)/n;
long tt = (long)Math.ceil(t);
long ex = i;
long ti = tt*n+ex;
if(ti<ansv)
{
//pw.println("** "+i);
ansg=i;
ansv=ti;
}
}
}
pw.println(ansg+1);
*/
/*ct=0;
paths= new ArrayList[10];
adj = new ArrayList[4];
vis2 = new int[4];
for(int i=0;i<4;i++)adj[i] = new ArrayList<Integer>();
for(int i=0;i<10;i++)paths[i] = new ArrayList<Integer>();
adj[0].add(1);
adj[0].add(2);
adj[0].add(3);
adj[1].add(3);
adj[2].add(0);
adj[2].add(1);
countPaths(2,3, new ArrayList<Integer>());
pw.println(ct);
for(int i=0;i<ct;i++)pw.println(paths[i]);
*/
/*
int n = sc.nextInt();
int m = sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++)arr[i]=sc.nextInt();
int req = n/m;
int[] rem = new int[m];
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
for(int i=0;i<n;i++)
{
rem[arr[i]%m]++;
}
int li=-1;
long ans=0;
for(int i=0;i<m;i++)
{
if(rem[i]>=req)
{
rem[(i+1)%m]+=rem[i]-req;
ans+=(rem[i]-req);
}
else
{
li=i;
}
}
for(int i=0;i<li;i++)
{
if(rem[i]>=req)
{
rem[(i+1)%m]+=rem[i]-req;
ans+=(rem[i]-req);
}
else
{
li=i;
}
}
pw.println(ans);
*/
//pw.println(pa);
/* NGE
int[] sto = {100,80,60,70,60,75,85};
int n = sto.length;
int[] spa = new int[n];
spa[0]=1;
Stack<Integer> st = new Stack<Integer>();
st.push(0);
for(int i=1;i<n;i++)
{
while(!st.isEmpty()&&sto[st.peek()]<=sto[i])st.pop();
spa[i]=(st.isEmpty())?i+1:i-st.peek();
st.push(i);
}
for(int i=0;i<n;i++)pw.print(sto[i]+" ");
pw.println();
for(int i=0;i<n;i++)pw.print(spa[i]+" ");
int[] nge = new int[n];
st.clear();
Stack<Point> st2 = new Stack<Point>();
st2.push(new Point(sto[0],0));
for(int i=1;i<n;i++)
{
while(!st2.isEmpty()&&st2.peek().x<sto[i])
{
Point pop = st2.pop();
nge[pop.y]=sto[i];
}
st2.push(new Point(sto[i],i));
}
while(!st2.isEmpty())
{
nge[st2.pop().y]=-1;
}
pw.println();
for(int i=0;i<n;i++)pw.print(sto[i]+" ");
pw.println();
for(int i=0;i<n;i++)pw.print(nge[i]+" ");
*/
/* int r = 2000000000;
pw.println(r);
int n = sc.nextInt();
int k = sc.nextInt();
int l = sc.nextInt();
int m = n*k;
int arr[] = new int[m];
for(int i=0;i<m;i++)arr[i]=sc.nextInt();
Arrays.sort(arr);
int limit = l+arr[0];
int li=-1;
for(int i=0;i<m;i++)
{
//pw.println(arr[i]);
if(arr[i]>limit)
{
//pw.println(arr[i]);
li=i; break;
}
}
//pw.println(limit+" "+li);
if(li==-1)li=n;
int noofsmallest = li;
if(noofsmallest<n)
{
pw.println(0);
}
else
{
li--;
long ans=0;
for(int i=0;i<n-1;i++)
{
ans+=(long)arr[li];
li--;
}
ans+=arr[0];
pw.println(ans);
}
*/
pw.close();
}
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());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
} catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static MyScanner sc = new MyScanner();
static PrintWriter pw = new PrintWriter(System.out, true);
static class DisjointSet
{
int[] parent;
int[] rank;
int n;
public DisjointSet(int x)
{
n=x;
parent = new int[x];
rank = new int[x];
for(int i=0;i<n;i++)
{
parent[i]=i;
}
}
public int find(int x)
{
if(parent[x]==x)
return x;
else
return find(parent[x]);
}
public void union(int i, int j)
{
int iroot = find(i);
int jroot = find(j);
if(iroot==jroot)
return;
if(rank[iroot]<rank[jroot])
{
parent[iroot]=jroot;
}
else if(rank[jroot]<rank[iroot])
{
parent[jroot]=iroot;
}
else
{
parent[iroot]=jroot;
rank[jroot]++;
}
}
}
} | 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,c =0,0
while i<=n:
if a[i:i+2] =="RU" or a[i:i+2]=="UR":
c+=1
i+=2
else:
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 | #!/usr/bin/env python
N = int(input())
indat = list(input().strip())
sw = 0
ans = 0
for s in indat:
if sw == 0:
tmp = s
sw = 1
elif sw == 1:
if s == tmp:
ans += 1
else:
sw = 0
tmp = 'D'
ans += 1
if tmp != 'D':
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 | numOfChar = int(input())
stringOfMov = input()
changedMov = list(stringOfMov)
if numOfChar>1:
item = 0
while (item < numOfChar - 1):
if changedMov[item] == 'U' and changedMov[item+1] == 'R':
if item > 1:
if not(stringOfMov[item-2] == 'R' and stringOfMov[-1] == 'U'):
changedMov[item] = 'D'
changedMov[item+1] = ''
item += 1
else :
changedMov[item] = 'D'
changedMov[item+1] = ''
item += 1
elif stringOfMov[item] == 'R' and stringOfMov[item+1] == 'U':
if item > 1:
if not(changedMov[item-2] == 'U' and changedMov[-1] == 'R'):
changedMov[item] = 'D'
changedMov[item+1] = ''
item += 1
else :
changedMov[item] = 'D'
changedMov[item+1] = ''
item += 1
item+=1
print (len("".join(changedMov)))
| 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()
short_s = ''
i = 0
while i < n:
if s[i:i+2] == 'RU' or s[i:i+2] == 'UR':
short_s += 'D'
i += 2
else:
short_s += s[i]
i += 1
print(len(short_s))
| PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int cnt, n;
string ss;
int main() {
cin >> n >> ss;
for (int i = 0; i < n; i++) {
if (i < n - 1 && ((ss[i] == 'R' && ss[i + 1] == 'U') ||
(ss[i] == 'U' && ss[i + 1] == 'R'))) {
i++;
cnt++;
} else
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 | n = int(input())
s = input()
ans = 0
tmp = 0
for i in range(n):
ans+=1
if tmp == 0 and i > 0:
if tmp == 0 and s[i] != s[i-1]:
ans-=1
tmp = 1
else:
tmp = 0
print(ans) | PYTHON3 |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
string s;
int main() {
int n;
cin >> n >> s;
int res = 0;
for (long long i = (long long)(0); i < (long long)(n); i++) {
if (i < n - 1 &&
((s[i] == 'R' && s[i + 1] == 'U') || (s[i] == 'U' && s[i + 1] == 'R')))
i++;
res++;
}
cout << res << 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, m, q, k1, k2, k3, a, b, c;
string s;
while (cin >> n) {
cin >> s;
int ans = 0;
for (int i = 1; i < s.length(); i++)
if (s[i - 1] != s[i]) ans++, i++;
cout << n - ans << endl;
}
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, k;
int n, m;
string s;
cin >> n;
cin >> s;
int c = 0;
for (i = 0; i < n - 1; i++) {
if (i + 1 < n) {
if (s[i] == 'U' && s[i + 1] == 'R')
c++, i++;
else if (s[i] == 'R' && s[i + 1] == 'U')
c++, i++;
}
}
cout << n - c;
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class StringRUD {
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(br.readLine());
char [] S1 = new char[a] ;
String S = br.readLine();
S1 = S.toCharArray() ;
int x = 0 ;
for (int i = 0; i <= S1.length -1 ; i++) {
if(i!= S1.length -1)
{
if(S1[i]=='U'&& S1[i+1]=='R')
{
x++ ;
i++ ;
continue;
}
else if(S1[i]=='R'&& S1[i+1]=='U')
{
x++ ;
i++ ;
continue ;
}
}
if(S1[i]=='R')
{
x++ ;
}
if(S1[i]=='U')
{x++ ;
}}
System.out.println(x);
}
}
| JAVA |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | n = int(input())
s = input()
count = len(s)
i = 1
while i < len(s):
if (s[i] == "U" and s[i - 1] == "R") or (s[i] == "R" and s[i - 1] == "U"):
count -= 1
i += 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 | from collections import deque, Counter, OrderedDict
from heapq import nsmallest, nlargest
from math import ceil,floor,log,log2,sqrt,gcd,factorial
def binNumber(n,size=1):
return bin(n)[2:].zfill(size)
def iar():
return list(map(int,input().split()))
def ini():
return int(input())
def isp():
return map(int,input().split())
def sti():
return str(input())
def isvowel(c):
if c == "a" or c=="e" or c=="i" or c=="o" or c=="u":
return 1
else:
return 0
if __name__ == "__main__":
n = ini()
s = sti()
ans = 0
i = 0
while i < n-1:
if (s[i]=='U' and s[i+1]=='R'):
ans+=1
i+=1
elif (s[i]=='R' and s[i+1]=='U'):
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 | import re
input()
print(len(re.sub(r'UR|RU', 'D', input())))
# Made By Mostafa_Khaled | 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.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/**
* Created by prituladima on 5/23/18.
*/
public class DiagonalWalking {
private void solve() throws IOException {
int n = nextInt();
String s = nextToken();
char[] chars = s.toCharArray();
int counter = 0;
for (int i = 0; i < chars.length - 1; i++) {
if (chars[i] != chars[i + 1]) {
counter++;
i++;
}
}
System.out.println(s.length() - counter);
}
public static void main(String[] args) {
new DiagonalWalking().run();
}
StringTokenizer tokenizer;
BufferedReader reader;
PrintWriter writer;
public void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
writer = new PrintWriter(System.out);
solve();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
int[] nextArr(int size) throws NumberFormatException, IOException {
int[] arr = new int[size];
for (int i = 0; i < size; i++)
arr[i] = nextInt();
return arr;
}
long[] nextArrL(int size) throws NumberFormatException, IOException {
long[] arr = new long[size];
for (int i = 0; i < size; i++)
arr[i] = nextLong();
return arr;
}
double[] nextArrD(int size) throws NumberFormatException, IOException {
double[] arr = new double[size];
for (int i = 0; i < size; i++)
arr[i] = nextDouble();
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 = int(input())
s = input()
ans = n
i=0
while(i<n-1):
#print(s[i]+s[i+1])
if(s[i]+s[i+1]=="RU" or s[i]+s[i+1] == "UR"):
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;
void solve() {
long long n;
cin >> n;
string s, ans = "";
cin >> s;
for (long long i = 0; i < n; i++) {
if (s[i] == 'R' and s[i + 1] == 'U') {
ans += 'D';
i++;
} else if (s[i] == 'U' and s[i + 1] == 'R') {
ans += 'D';
i++;
} else
ans += s[i];
}
cout << ans.size() << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long testcases = 1;
while (testcases--) {
solve();
}
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<char> v;
char x;
while (n--) {
cin >> x;
if (v.size() > 0 && x != v[v.size() - 1] &&
(v[v.size() - 1] == 'U' || v[v.size() - 1] == 'R'))
v[v.size() - 1] = 'D';
else
v.push_back(x);
}
cout << v.size() << endl;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
int count = 0;
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'R' && s[i + 1] == 'U' || s[i] == 'U' && s[i + 1] == 'R') {
count++;
i++;
}
}
cout << n - count;
cout << endl;
}
return 0;
}
| CPP |
954_A. Diagonal Walking | Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3
Input
17
UUURRRRRUUURURUUU
Output
13
Note
In the first test the shortened sequence of moves may be DUD (its length is 3).
In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13). | 2 | 7 | import java.util.*;
public class fran {
public static void main(String... args) {
Scanner kbd = new Scanner(System.in);
int f = kbd.nextInt();
char[] line = kbd.next().toCharArray();
for (int m = 0; m < line.length - 1; m++) {
if ((line[m] == 'U' && line[m + 1] == 'R') || line[m] == 'R' && line[m + 1] == 'U') {
line[m] = line[m + 1] = 'D';
f--;
}
}
System.out.print(f);
}
}
| JAVA |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.