B - Diagonal Walking

题目:
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail’s moves. He thinks that this sequence is too long and he wants to make it as short as possible.
In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.
Your problem is to print the minimum possible length of the sequence of moves after the replacements.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 100) — the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Output
Print the minimum possible length of the sequence of moves after all replacements are done.
Examples
Input
5
RUURU
Output
3

代码如下:

#include<iostream>
using namespace std;
#define MAX 105
int main()
{
    char a[MAX];
    int sum,n;
    cin >> n;
    sum = n;
    for(int i = 0;i < n;i++) cin >> a[i];
    for(int i = 1;i < n;i++)
    {
        if(a[i] != a[i - 1])
        {
            sum--;
            i++;
        }
    }
    cout << sum << endl;
    return 0;
}

先让步数等于n,从第二个字母开始,如果和前一个字母不一样,则步数减1,随后往后移动2个字母,如果一样就往后移动一位,直到结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值