http://codeforces.com/problemset/problem/935/B

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.

Input

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.

Output

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.

Examples
input

Copy
1
U
output
0
input

Copy
6
RURUUR
output
1
input

Copy
7
URRRUUU
output
2
Note

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.

弱智题

越界多考虑一步不变就行

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 2147483647
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define ri register int
template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template <class T> inline T min(T a, T b, T c, T d) {
return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d) {
return max(max(a, b), max(c, d));
}
#define scanf1(x) scanf("%d", &x)
#define scanf2(x, y) scanf("%d%d", &x, &y)
#define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X)
#define pi acos(-1)
#define me(x, y) memset(x, y, sizeof(x));
#define For(i, a, b) for (int i = a; i <= b; i++)
#define FFor(i, a, b) for (int i = a; i >= b; i--)
#define bug printf("***********\n");
#define mp make_pair
#define pb push_back
const int maxn = ;
// name*******************************
int n;
int s1=,s2=;
string s;
char a[maxn];
int ans=;
// function****************************** //***************************************
int main() {
// ios::sync_with_stdio(0);
// cin.tie(0);
// freopen("test.txt", "r", stdin);
// freopen("outout.txt","w",stdout);
cin>>n;
For(i,,n)
cin>>a[i]; For(i,,n){
if(a[i]=='U'){
s1++;
if(s1==s2&&a[i+]=='U'){
ans++;
}
}else{
s2++;
if(s1==s2&&a[i+]=='R'){
ans++;
}
}
}
cout<<ans; return ;
}

B. Fafa and the Gates的更多相关文章

  1. Fafa and the Gates(模拟)

    Two neighboring kingdoms decided to build a wall between them with some gates to enable the citizens ...

  2. CF935B Fafa and the Gates 题解

    Content 一个动点 \(F\) 一开始在平面直角坐标系上原点的位置,随后它会移动 \(n\) 次,每次只能向上走或者向右走 \(1\) 个单位,求经过直线 \(y=x\) 的次数. 数据范围:\ ...

  3. xor和gates的专杀脚本

    前段时间的一次样本,需要给出专杀,应急中遇到的是linux中比较常见的两个家族gates和xor. 首先是xor的专杀脚本,xor样本查杀的时候需要注意的是样本的主进程和子进程相互保护(详见之前的xo ...

  4. UVa1607 poj1435 UVaLive1686 Gates

    填坑系列(p.246) 由函数连续性得满足二分性 #include<cstdio> #include<cstring> #include<cstdlib> #inc ...

  5. [Locked] Walls and Gates

    Walls and Gates You are given a m x n 2D grid initialized with these three possible values. -1 - A w ...

  6. 【转】Memory gates checking failed because the free memory***解决办法

    Issue: Vault users cannot connect. VLOGS show the following error: Memory gates checking failed beca ...

  7. Uva - 1607 - Gates

    题目理解麻烦,估计提交量少(总共只有32个人...)也是因为题目比较麻烦,看起来像物理题,实际理解了还可以.整个电路的功能就4种,先判断x=0和x=1的输出是否相同,吐过相同,而整个电路是常熟,随便输 ...

  8. [转] How Bill Gates read books

    Bill Gates is one of the most famous figures in the business world. He is one of the richest men in ...

  9. Codeforces 935E Fafa and Ancient Mathematics dp

    Fafa and Ancient Mathematics 转换成树上问题dp一下. #include<bits/stdc++.h> #define LL long long #define ...

随机推荐

  1. BZOJ4337: BJOI2015 树的同构(hash 树同构)

    题意 题目链接 Sol 树的同构问题,直接拿hash判一下,具体流程大概是这样的: 首先转化为有根树,预处理出第\(i\)棵树以\(j\)为根时的hash值. 那么两个树同构当且仅当把两棵树的hash ...

  2. PS改变图像颜色

    由于写的一个页面主色调变了,里面的一些图标颜色也要相应改变,自己难得重新去psd里面截图,就想着用ps,看能否直接能变换一下图标颜色.其实方法也很简单的. 1:用ps打开需要改变图标颜色的文件,然后选 ...

  3. spring boot(16)-mail发邮件

    上一篇讲了如何处理异常,并且异常最终会写入日志.但是日志是写在服务器上的,我们无法及时知道.如果能够将异常发送到邮箱,我们可以在第一时间发现这个异常.当然,除此以外,还可以用来给用户发验证码以及各种离 ...

  4. 3.Servlet实例

    一.基础实例 1.参照如下例子创建maven web工程: https://www.cnblogs.com/lukelook/p/9187313.html 2.创建一个简单的Servlet 类 pac ...

  5. ASP.NET Web API 跨域访问(CORS)要注意的地方

    一.客户端用JSONP请求数据 如果你想用JSONP来获得跨域的数据,WebAPI本身是不支持javascript的callback的,它返回的JSON是这样的: {"YourSignatu ...

  6. WinForm 应用程序禁止多个进程运行

    方法一: 禁止多个进程运行 using System; using System.Collections.Generic; using System.Linq; using System.Window ...

  7. 用UITextView加载rtfd文件

    用UITextView加载rtfd文件 效果 说明 使用此方法可以实现十分简易的富文本显示效果,包括图文混排等等效果. 源码 // // ViewController.m // Rtfd // // ...

  8. Request URL参数

    登录跳转完整参考: http://www.cnblogs.com/dreamer-fish/p/5435274.html request.META.get('HTTP_REFERER', '/') # ...

  9. spine获取骨骼位置

    time: 2015/07/23 版本: /****************************************************************************** ...

  10. mysql 创建数据数据库 (避免新建的库名已经存在、设置编码)

    1.创建数据库的 create database 数据库名 eg: Create database mydatabase 查看已创建的数据: show databases; 结果: 2.数据库名所对应 ...