CodeForces-626A-Robot Sequence
2016-03-26 20:05
555 查看
Description
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.
Sample Input
Input
Output
Input
Output
Input
Output
Hint
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.
给你一个数n,然后给你一个长度为n的字符串,字符串未L,R,U,D,代表四个方向中的一个,问这个字符串中,有多少个子串能够回到原点。
n才200,直接暴力就好了,我选择是统计子串的出现的次数,然后比较就好了
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.
Sample Input
Input
6 URLLDR
Output
2
Input
4 DLUU
Output
0
Input
7 RLRLRLR
Output
12
Hint
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.
给你一个数n,然后给你一个长度为n的字符串,字符串未L,R,U,D,代表四个方向中的一个,问这个字符串中,有多少个子串能够回到原点。
n才200,直接暴力就好了,我选择是统计子串的出现的次数,然后比较就好了
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; char s[205]; int main() { int n; while(scanf("%d",&n)!=EOF) { scanf("%s",s); int num=0; for(int i=0;i<n;i++) { int u=0,d=0,l=0,r=0; for(int j=i;j<n;j++) { if(s[j]=='U') u++; if(s[j]=='D') d++; if(s[j]=='L') l++; if(s[j]=='R') r++; if(u==d&&r==l) num++; } } printf("%d\n",num); } return 0; }
相关文章推荐
- UVA 1252(p287)----Twenty Questions
- POJ2524Ubiquitous Religions
- 如何降低Vue.js项目中Webpack打包文件的大小?
- 334. Increasing Triplet Subsequence
- 通过request获得全路径
- 自定义控件基础 requestLayout()和invalidate()区别
- 五、初学servlet之请求Request
- Leetcode Sudoku Solver My Submissions Question
- 开源的OLAP框架druid
- 有多个手势支持时候需要实现UIGuestrueRecoginer方法
- UIBezierPath转换为传统上下文的样式
- UIGestureRecognizer手势有哪些
- CodeForces - 560D Equivalent Strings (DFS)
- Educational Codeforces Round 10 E - Pursuit For Artifacts (强联通缩点 + 回溯)
- serialVersionUID
- UITableView 系列之自定义 UITableViewCell
- Mongo 中间件 pre find 修改query
- 【边做项目边学Android】小白会遇到的问题--This Android SDK requires Android Developer Toolkit version 23.0.0 or above
- APUE之线程(三)
- JAVA的String、StringBuilder和StringBuffer