CodeForces-626A-Robot Sequence
来源:互联网 发布:织梦 添加视频不播放 编辑:程序博客网 时间:2024/04/29 07:01
Description
Calvin the robot lies in an infinite rectangular grid. Calvin's source code contains a list ofn 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
6URLLDR
2
4DLUU
0
7RLRLRLR
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;}
- Codeforces 626A Robot Sequence
- Codeforces 626A Robot Sequence
- CodeForces-626A-Robot Sequence
- Codeforces 626A Robot Sequence 【水题】
- CodeForces 626 A. Robot Sequence(水~)
- CodeForces 626A B - Robot Sequence
- 626A.Robot Sequence
- A. Robot Sequence
- A. Robot Sequence【水题】
- codeforces A. Ciel and Robot
- CodeForces 321A Ciel and Robot[暴力]
- CodeForces 321 A - Ciel and Robot
- Codeforces-Edu6-A. Professor GukiZ's Robot
- CodeForces 620A Professor GukiZ's Robot
- CodeForces 321A Ciel and Robot
- codeforces 354A Vasya and Robot [思维]
- CodeForces - 11A Increasing Sequence
- codeforces 11A - Increasing Sequence
- hdoj--Let's go to play(模拟)
- JAX-WS实做webservice验证
- 管闲事的小明
- 【三层】——C#与VB.NET各显神通
- URI数据存储初解
- CodeForces-626A-Robot Sequence
- 最大矩阵和 HDU1081 & NYOJ 104
- Poj 3616 Milking Time【dp】
- c#接口
- ImageView 常用属性的分析
- 希尔排序
- 第四次上机实践项目-项目一-三角形类锥形-(1)
- 第五周上机实践项目——项目4-长方柱类
- 关于placeholder的小tip