Fafa and the Gates(CodeForces 935B)

Description

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.

Sample Input

Input

1
U

Output

0

Input

6
RURUUR

Output

1

Input

7
URRRUUU

Output

2

Hint

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.

题解:当时做的时候想的是判断这个点x,y的变化,然后找出是否过门。

代码如下:

#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include<ctime>
#define maxn 100007
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define eps 0.00000001
using namespace std;
typedef long long ll;
char a[maxn];
int x[maxn],y[maxn];
int main()
{
    int k;
    cin>>k;
    for(int i=1; i<=k; i++)
        cin>>a[i];
    int s=0;
    x[0]=0;y[0]=0;
    if(a[1]=='U')
    {
        x[1]=0;
        y[1]=1;
    }
    else
    {
        x[1]=1;
        y[1]=0;
    }
    for(int i=2; i<=k; i++)
    {
        if(a[i]=='U')
        {
            y[i]=y[i-1]+1;
            x[i]=x[i-1];
        }
        if(a[i]=='R')
        {
            x[i]=x[i-1]+1;
            y[i]=y[i-1];
        }
        if(x[i-2]>y[i-2]&&x[i-1]==y[i-1]&&x[i]<y[i]||x[i-2]<y[i-2]&&x[i-1]==y[i-1]&&x[i]>y[i])
            s++;
    }
    cout<<s<<endl;
}

 

网上某佬简单代码,每次判断当前状态下的小三角形直角边的长度关系。

#include<iostream>  
using namespace std;  
char s[100005];
int main()
{  
    int n;  
    while(cin>>n)
    {
        int num=0,cnt=0;
        cin>>s;
        for(int i=0;i<n;i++)
        {
            if(s[i]=='U')
              num++;
            else
              num--;
            if(!num&&s[i]==s[i+1])
              cnt++;
        }
        cout<<cnt<<endl;
    }  
    return 0;
} 


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值