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. 函数xdes_init

    /**********************************************************************//** Inits an extent descript ...

  2. codevs 3290 华容道

    HAHAHA BFS+SPFA. #include<iostream> #include<cstdio> #include<cstring> #include< ...

  3. JavaScript学习笔记(备忘录)

    ===运算符 判断数值和类型是否相等.如: console.log('s'==='s') //输出trueconsole.log('1'===1) //输出false

  4. MYSQL内存

    全局内存(BASE MEMORY) 线程内存(MEMORY PER CONNECTION) max_conecctions:整个 MySQL 允许的最大连接数; max_user_connection ...

  5. 搞明白这八个问题,Linux系统就好学多了。

    正在犹豫入坑Linux学习的同学或者已经入坑的同学,经常会问到这样八个问题.今天,这些问题我都会一一解答,希望我的看法能帮助各位同学.常言道“好的开始是成功的一半”,如果你明白了以下八个问题,就能有一 ...

  6. 典型的 SQL 注入过程(转)

    无意间发现某站点存在 SQL 注入漏洞,于是利用这个漏洞提权并获取服务器控制权.这个案例很典型,像是教科书式的典型入侵步骤,下面就以这个案例展示从 SQL 注入到获取目标服务器控制权限的全过程. 发现 ...

  7. 《Python 学习手册4th》 第七章 字符串

    ''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书) “重点 ...

  8. Linux操作系统上用数据泵导库

    1.在Linux上 创建物理目录dp_dir,存放数据库.dmp文件: 用root用户登录,切换到oracle用户,用oralce身份创建物理目录如下: [root@server36 oracle]# ...

  9. linux常用命令之--文件打包与压缩命令

    linux的文件打包与压缩命令 1.压缩与解压命令 compress:用于压缩指定的文件,后缀为.z 其命令格式如下: compress [-d] 文件名 常用参数: -d:解压被压缩的文件(.z为后 ...

  10. 关于duilib中的list的扩展探索

    原文地址:http://blog.csdn.net/tragicguy/article/details/21893065 今天在做一个程序的界面时,需要在一个列表中显示除文字以外的其他控件,如:Edi ...