A. Robot Sequence
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.

题意:给一个字符串其中 U代表向上,D代表向下,L代表向左,R代表向右  问在这个字符串中有多少个子串可以满足回到起点的要求

题解:只要字符串中向上的次数等于向下的次数并且向左的次数等于向右的次数即可

#include<stdio.h>
#include<string.h>
#include<string>
#include<math.h>
#include<algorithm>
#define LL long long
#define PI atan(1.0)*4
#define DD doublea
#define MAX 1010
#define mod 10007
using namespace std;
char s[MAX];
int n;
int judge(int a,int b)
{
int r,l,u,d,i,j;
r=l=u=d=0;
for(i=a;i<=b;i++)
{
if(s[i]=='U') u++;
if(s[i]=='D') d++;
if(s[i]=='L') l++;
if(s[i]=='R') r++;
}
if(u==d&&l==r) return 1;
else return 0;
}
int main()
{
int j,i,sum;
while(scanf("%d",&n)!=EOF)
{
scanf("%s",s);
sum=0;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(judge(i,j))
sum++;
}
}
printf("%d\n",sum);
}
return 0;
}

  

Codeforces 626A Robot Sequence的更多相关文章

  1. Codeforces 626A Robot Sequence(模拟)

    A. Robot Sequence time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  2. Codeforces 626 A. Robot Sequence (8VC Venture Cup 2016-Elimination Round)

      A. Robot Sequence   time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  3. 8VC Venture Cup 2016 - Elimination Round A. Robot Sequence 暴力

    A. Robot Sequence 题目连接: http://www.codeforces.com/contest/626/problem/A Description Calvin the robot ...

  4. A. Robot Sequence

    A. Robot Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  5. Codeforces 601B. Lipshitz Sequence(单调栈)

    Codeforces 601B. Lipshitz Sequence 题意:,q个询问,每次询问给出l,r,求a数组[l,r]中所有子区间的L值的和. 思路:首先要观察到,斜率最大值只会出现在相邻两点 ...

  6. CodeForces 97D. Robot in Basement

    time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...

  7. Curious Array Codeforces - 407C(高阶差分(?)) || sequence

    https://codeforces.com/problemset/problem/407/C (自用,勿看) 手模一下找一找规律,可以发现,对于一个修改(l,r,k),相当于在[l,r]内各位分别加 ...

  8. CodeForces - 922D Robot Vacuum Cleaner (贪心)

    Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that Pushok is a ...

  9. codeforces 622A Infinite Sequence

    A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. [HZAU]华中农业大学第四届程序设计大赛网络同步赛

    听说是邀请赛啊,大概做了做…中午出去吃了个饭回来过掉的I.然后去做作业了…… #include <algorithm> #include <iostream> #include ...

  2. java中的toString方法

    对于我这种用惯了C++的人来说,突然见到有人写java程序的时候竟然将整数和String类型的变量使用+连接到一起,感到非常奇怪,追究了下原因. 原来所有的java对象都有toString()方法,而 ...

  3. POJ1037A decorative fence(好dp)

    1037 带点组合的东西吧 黑书P257 其实我没看懂它写的嘛玩意儿 这题还是挺不错的 一个模糊的思路可能会好想一些 就是大体的递推方程 dp1[][]表示降序 dp2[][]表示升序 数组的含义为长 ...

  4. JAVA使用apache http组件发送POST请求

    在上一篇文章中,使用了JDK中原始的HttpURLConnection向指定URL发送POST请求 可以看到编码量有些大,并且使用了输入输出流 传递的参数还是用“name=XXX”这种硬编的字符串进行 ...

  5. sql 游标循环当中重新赋值

    sql 游标循环当中的变量必须重新赋值不然变量的值就是前次循环的值

  6. 根据中国气象局提供的API接口实现天气查询

    中国气象局提供了三个天气查询的API接口: [1]http://www.weather.com.cn/data/sk/101190101.html [2]http://www.weather.com. ...

  7. 浏览器HTML5支持程度测试

    /********************************************************************* * 浏览器HTML5支持程度测试 * 说明: * 想知道对 ...

  8. crtmpserver流媒体服务器的介绍与搭建

    crtmpserver流媒体服务器的介绍与搭建 (2012-02-29 11:28) 标签:  crtmpserver  C++ RTMP Server  rtmp  Adobe FMS(Flash ...

  9. Java 小片段

    public static String listToString(List<String> stringList){ if (stringList==null) { return nul ...

  10. 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法

    使用HttpURLConnection下载文件时经常会出现 java.io.FileNotFoundException文件找不到异常,下面介绍下解决办法 首先设置tomcat对get数据的编码:con ...