Codeforces 626A Robot Sequence
2 seconds
256 megabytes
standard input
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.
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.
Print a single integer — the number of contiguous substrings that Calvin can execute and return to his starting square.
6
URLLDR
2
4
DLUU
0
7
RLRLRLR
12
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的更多相关文章
- Codeforces 626A Robot Sequence(模拟)
A. Robot Sequence time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- 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 ...
- 8VC Venture Cup 2016 - Elimination Round A. Robot Sequence 暴力
A. Robot Sequence 题目连接: http://www.codeforces.com/contest/626/problem/A Description Calvin the robot ...
- A. Robot Sequence
A. Robot Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces 601B. Lipshitz Sequence(单调栈)
Codeforces 601B. Lipshitz Sequence 题意:,q个询问,每次询问给出l,r,求a数组[l,r]中所有子区间的L值的和. 思路:首先要观察到,斜率最大值只会出现在相邻两点 ...
- CodeForces 97D. Robot in Basement
time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...
- Curious Array Codeforces - 407C(高阶差分(?)) || sequence
https://codeforces.com/problemset/problem/407/C (自用,勿看) 手模一下找一找规律,可以发现,对于一个修改(l,r,k),相当于在[l,r]内各位分别加 ...
- CodeForces - 922D Robot Vacuum Cleaner (贪心)
Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that Pushok is a ...
- codeforces 622A Infinite Sequence
A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...
随机推荐
- 结构体struct和typedef后面接指针的含义
typedef struct file { ... }FileInfo, *FileP; 上述程序中定义了一个结构体,结构体的名字为file,并且给其指针 取个别名为FileP,所以后续程序中出现Fi ...
- Git使用简介
git创建分支并直接切换到分支:git checkout -b name git提交分支到远程服务器: git push origin name/git push origin name:name ...
- HDU 1280 前m大的数【哈希入门】
题意:中文的题目= =将各种组合可能得到的和作为下标,然后因为不同组合得到的和可能是一样的, 所以再用一个数组num[]数组,就可以将相同的和都记录下来 #include<iostream> ...
- Asp.Net Web Api 与 Andriod 接口对接开发经验,给小伙伴分享一下!
最近一直急着在负责弄Asp.Net Web Api 与 Andriod 接口开发的对接工作! 刚听说要用Asp.Net Web Api去跟 Andriod 那端做接口对接工作,自己也是第一次接触Web ...
- UVA 1663 Purifying Machine (二分图匹配,最大流)
题意: 给m个长度为n的模板串,模板串由0和1和*三种组成,且每串至多1个*,代表可0可1.模板串至多匹配2个串,即*号改成0和1,如果没有*号则只能匹配自己.问:模板串可以缩减为几个,同样可以匹配原 ...
- test chemes
rcmobile://messages rcmobile://badge rcmobile://dialer rcmobile://open rcmobile://sms?type=new
- Python--类使用
类使用的几个注意点: 1. 类的语法结构:2. __init__(self),3. __metaclass__=type, (新式类)4. super(subclassname, self).__in ...
- python的Requests模块使用tips
post方法提交的是表单,要用data放dict get方法请求的是参数,要用params放dict HTTP头部是大小写不敏感的
- Spark学习体会
在去年图计算工作中,和公司里实习的博士生尝试过Spark后,发现Spark比Hadoop在计算速度上后很大的提高.Spark的计算使用Scala语言编写代码,其中图计算用到了GraphX.对Spark ...
- login:用户登陆的意思
login:用户登陆的意思 在思科的设备上有两种登录方式: 一种是本地方式,使用console口: 一种是远程方式(或者叫做网络方式):使用的是telnet等 1.默认情况下,思科的远程访问是禁止的. ...