Codeforces 626A Robot Sequence(模拟)
2017-06-29 11:44
351 查看
A. Robot Sequence
time limit per test:2 secondsmemory 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.
题目链接:http://codeforces.com/contest/626/problem/A
题意:在一块方格里,给出一串指令包含U,D,L,R这些字母,分别表示上下左右移动一格。问有多少个子串(包含自身)能使得物体回到出发位置。
分析:模拟一下找子串的个数即可!
解释下样例3:子串为:RL,RLRL,RLRLRL,LR,LRLR,LRLRLR,RL,RLRL,LR,LRLR,RL,LR,共12条,所以输出为12
下面给出AC代码:
#include <bits/stdc++.h> using namespace std; inline int read() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } int n; char s[255]; int main() { n=read(); cin>>s; int e=0; for(int i=0;i<n;i++) { int a=0,b=0,c=0,d=0; for(int j=i;j<n;j++) { if(s[j]=='U') a++; if(s[j]=='D') b++; if(s[j]=='L') c++; if(s[j]=='R') d++; if(a==b&&c==d) e++; } } cout<<e<<endl; return 0; }
相关文章推荐
- Codeforces 863C 模拟
- 【模拟退火】最小球POJ 2069/codeforces 82 E
- codeforces 722系列ABCD 【套题】模拟+并查集+STL+数学二分
- CodeForces 723B Text Document Analysis (水题模拟)
- 【Codeforces 723B】Text Document Analysis 模拟
- Codeforces 322C Ciel and Robot【思维+模拟】细节很多= =
- CodeForces 652A Gabriel and Caterpillar(模拟)
- Codeforces 724C Ray Tracing (规律模拟)
- CodeForces 412E - E-mail Addresses (模拟)
- codeforces-626-B. Cards(简单模拟)
- CodeForces 703A Mishka and Game(模拟)
- CodeForces 615E——Hexagons(二分,模拟)
- 【打CF,学算法——三星级】Codeforces 704A Thor (模拟)
- CodeForces - 868B Race Against Time(模拟)
- Codeforces 911E Stack Sorting (模拟)
- Codeforces626B - Cards【模拟】
- Codeforces 508C - Anya and Ghosts (模拟 + 贪心)
- CodeForces 680B Bear and Finding Criminals 简单模拟
- CodeForces 705C Thor (模拟)
- 【CodeForces 779A】Pupils Redistribution(模拟)