CF935B.Fafa and the Gates

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Two neighboring kingdoms decided to build a wall between them with some gates to enable the citizens to go from one kingdom to another. Each time a citizen passes through a gate, he has to pay one silver coin.

The world can be represented by the first quadrant of a plane and the wall is built along the identity line (i.e. the line with the equation x = y). Any point below the wall belongs to the first kingdom while any point above the wall belongs to the second kingdom. There is a gate at any integer point on the line (i.e. at points (0, 0), (1, 1), (2, 2), ...). The wall and the gates do not belong to any of the kingdoms.

Fafa is at the gate at position (0, 0) and he wants to walk around in the two kingdoms. He knows the sequence S of moves he will do. This sequence is a string where each character represents a move. The two possible moves Fafa will do are 'U' (move one step up, from (x, y) to (x, y + 1)) and 'R' (move one step right, from (x, y) to (x + 1, y)).

Fafa wants to know the number of silver coins he needs to pay to walk around the two kingdoms following the sequence S. Note that if Fafa visits a gate without moving from one kingdom to another, he pays no silver coins. Also assume that he doesn't pay at the gate at point (0, 0), i. e. he is initially on the side he needs.

两个相邻的王国决定在它们之间修建一堵墙,并设置若干个门,以便两国公民可以互相往来。每当一名公民穿过一道门时,他必须支付一枚银币。

整个世界可以用平面的第一象限来表示,而墙则沿恒等线(即直线 x=yx = y)修建。该直线下方的任意点属于第一个王国,而该直线上方的任意点属于第二个王国。在该直线上每个整数坐标点处均设有一道门(即位于点 (0, 0)(0, 0)、(1, 1)(1, 1)、(2, 2)(2, 2)、… 处)。墙本身及所有门均不属于任一王国。

法法起始于位置 (0, 0)(0, 0) 处的门,并希望在两个王国中四处走动。他已知自己将要执行的移动序列 SS。该序列是一个字符串,其中每个字符代表一次移动。法法可能执行的两种移动为:'U'(向上移动一步,即从 (x, y)(x, y) 移动到 (x, y + 1)(x, y + 1))和 'R'(向右移动一步,即从 (x, y)(x, y) 移动到 (x + 1, y)(x + 1, y))。

法法希望知道:按照序列 SS 在两个王国中行走时,他总共需要支付多少枚银币。注意:若法法访问某道门但并未从一个王国进入另一个王国,则他无需支付银币。此外,假设他在起点 (0, 0)(0, 0) 处的门不支付银币,即他初始时已处于所需的一侧。

输入格式

The first line of the input contains single integer n (1 ≤ n ≤ 105) — the number of moves in the walking sequence.

The second line contains a string S of length n consisting of the characters 'U' and 'R' describing the required moves. Fafa will follow the sequence S in order from left to right.

输入的第一行包含一个整数 nn(1 ≤ n ≤ 1051 ≤ n ≤ 10^5)—— 表示行走序列中的移动步数。

第二行包含一个长度为 nn 的字符串 SS,由字符 'U' 和 'R' 组成,用于描述所需的移动。Fafa 将按从左到右的顺序依次执行序列 SS 中的每一步移动。

输出格式

On a single line, print one integer representing the number of silver coins Fafa needs to pay at the gates to follow the sequence S.

在一行中,输出一个整数,表示法法为遵循序列 SS 在各道门处需要支付的银币数量。

输入输出样例

  • 输入#1

    1
    U

    输出#1

    0
  • 输入#2

    6
    RURUUR

    输出#2

    1
  • 输入#3

    7
    URRRUUU

    输出#3

    2

说明/提示

The figure below describes the third sample. The red arrows represent the sequence of moves Fafa will follow. The green gates represent the gates at which Fafa have to pay silver coins.

下图描述了第三个样例。红色箭头表示 Fafa 将遵循的移动序列。绿色门表示 Fafa 需要支付银币的门。

输入解题思路,AI测评打分。不知道怎么写?

首页