748C Santa Claus and Robot


C. Santa Claus and Robot
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Santa Claus has Robot which lives on the infinite grid and can move along its lines. He can also, having a sequence of m points p1, p2, ..., pm with integer coordinates, do the following: denote its initial location by p0. First, the robot will move from p0 to p1 along one of the shortest paths between them (please notice that since the robot moves only along the grid lines, there can be several shortest paths). Then, after it reaches p1, it'll move to p2, again, choosing one of the shortest ways, then to p3, and so on, until he has visited all points in the given order. Some of the points in the sequence may coincide, in that case Robot will visit that point several times according to the sequence order.

While Santa was away, someone gave a sequence of points to Robot. This sequence is now lost, but Robot saved the protocol of its unit movements. Please, find the minimum possible length of the sequence.

Input

The first line of input contains the only positive integer n (1 ≤ n ≤ 2·105) which equals the number of unit segments the robot traveled. The second line contains the movements protocol, which consists of n letters, each being equal either L, or R, or U, or Dk-th letter stands for the direction which Robot traveled the k-th unit segment in: L means that it moved to the left, R — to the right, U — to the top and D — to the bottom. Have a look at the illustrations for better explanation.

Output

The only line of input should contain the minimum possible length of the sequence.

Examples
input
4
RURD
output
2
input
6
RRULDD
output
2
input
26
RRRULURURUULULLLDLDDRDRDLD
output
7
input
3
RLL
output
2
input
4
LRLR
output
4
Note

The illustrations to the first three tests are given below.

The last example illustrates that each point in the sequence should be counted as many times as it is presented in the sequence.


大值题意:

很有意思的一个题~

圣诞老人在一个无限大的方格网内送礼物,有n个礼物要送,礼物的位置为 p1 p2 ···pn。按顺序送,并且每次送pi到pi+1都是走最短的路。

可是现在,礼物的位置丢失了。我们只记得圣诞老人送礼物的路径。求最少有多少个礼物要送,即求n的最小值。


思路分析:

记录从pi到pi+1的方向。一旦走相反的路则该点必有一个礼物~

最后再加上1,因为最后一个位置肯定有礼物。


ac代码:

#include<bits/stdc++.h>
using namespace std;
bool vis[150];
bool ok(char a)
{
    if(a=='L' && vis['R']==1)
        return 1;
    if(a=='R' && vis['L']==1)
        return 1;
    if(a=='U' && vis['D']==1)
        return 1;
    if(a=='D' && vis['U']==1)
        return 1;
    return 0;
}
int main()
{
    ios::sync_with_stdio(false);
    string str,s;
    int T,i,j,k,tem,sum=0;
    cin>>T;
    cin>>str;
    memset(vis,0,sizeof(vis));
    for(i=0;i<T;i++)
    {
        if(ok(str[i]))
        {
            memset(vis,0,sizeof(vis));
            sum++;
        }
        vis[str[i]]=1;
    }
    cout<<sum+1<<endl;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值