您的位置:首页 > 产品设计 > UI/UE

A. Robot Sequence

2016-02-25 18:24 567 查看
time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Calvin the robot lies in an infinite rectangular grid. Calvin's source code contains a list of n commands, each either 'U',
'R', 'D', or 'L' —
instructions to move a single square up, right, down, or left, respectively. How many ways can Calvin execute a non-empty contiguous substrings of commands and return to the same square he starts in? Two substrings are considered different if they have different
starting or ending indices.

Input

The first line of the input contains a single positive integer, n (1 ≤ n ≤ 200) —
the number of commands.

The next line contains n characters, each either 'U',
'R', 'D', or 'L' —
Calvin's source code.

Output

Print a single integer — the number of contiguous substrings that Calvin can execute and return to his starting square.

Examples

input
6
URLLDR


output
2


input
4
DLUU


output
0


input
7
RLRLRLR


output
12


Note

In the first case, the entire source code works, as well as the "RL" substring in the second and third characters.

Note that, in the third case, the substring "LR" appears three times, and is therefore counted three times to the total result.

解题说明:此题是一道模拟题,首先判断真正需要走的步数,然后再计算不同的方法。

#include<cstdio>
#include <cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

int main()
{
int a,b,c,d,e,i,j,f;
char m[202];
scanf("%d",&a);
getchar();
for(i=0;i<a;i++)
{
scanf("%c",&m[i]);
}
f=0;
for(i=0;i<a;i++)
{
b=0;
c=0;
d=0;
e=0;
for(j=i;j<a;j++)
{
if(m[j]=='L')
{
b++;
}
if(m[j]=='R')
{
c++;
}
if(m[j]=='U')
{
d++;
}
if(m[j]=='D')
{
e++;
}
if(b==c&&d==e)
{
f++;
}
}
}
printf("%d\n",f);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: