【题目】http://codeforces.com/problemset/problem/935/B
【题意】在第一象限上有两个国家,分别在x=y上方和下方。在x=y上有城门,穿过城门要交钱。现在给出xx在地图上走的过程,问他交了多少钱。
【思路】模拟。记录他当前在那个国家,每当走到边界线上,下一次的操作要走到另一边的话,ans++;
【代码】
#include<bits/stdc++.h>
using namespace std;
const int M=1e5+5;
int main()
{
int n;
cin>>n;
char a[M];
cin>>a;
int ans=0;
int x=0,y=0;
int zuo;//记录在哪一边
if(a[0]=='U')
{
y++;
zuo=1;
}
else
{
x++;
zuo=0;
}
for(int i=1; i<n; i++)
{
if(a[i]=='U')//移动
{
y++;
}
else
{
x++;
}
if(zuo)
{
if(x>y)//判断从左走到了右
{
ans++;
zuo=0;
}
}
else
{
if(x<y)//判断从右走到了左
{
ans++;
zuo=1;
}
}
}
cout<<ans<<endl;
}