Codeforces 626A Robot Sequence(模拟)

简介: A. Robot Sequence time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard out...

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.

题目链接: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代码:

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 inline int read()
 4 {
 5     int x=0,f=1;
 6     char ch=getchar();
 7     while(ch<'0'||ch>'9')
 8     {
 9         if(ch='-')
10             f=-1;
11         ch=getchar();
12     }
13     while(ch>='0'&&ch<='9')
14     {
15         x=x*10+ch-'0';
16         ch=getchar();
17     }
18     return x*f;
19 }
20 int n;
21 char s[255];
22 int main()
23 {
24     n=read();
25     cin>>s;
26     int e=0;
27     for(int i=0;i<n;i++)
28     {
29         int a=0,b=0,c=0,d=0;
30         for(int j=i;j<n;j++)
31         {
32             if(s[j]=='U')
33                 a++;
34             if(s[j]=='D')
35                 b++;
36             if(s[j]=='L')
37                 c++;
38             if(s[j]=='R')
39                 d++;
40             if(a==b&&c==d)
41                 e++;
42         }
43     }
44     cout<<e<<endl;
45     return 0;
46 }

 

 

 

目录
相关文章
|
8月前
|
算法 数据挖掘 测试技术
Sentieon | 每周文献-Benchmark and Method Study-第三十期
Sentieon | 每周文献-Benchmark and Method Study-第三十期
47 1
codeforces 327 B. Hungry Sequence
题目就是让你输出n个数的序列,要保证该序列是递增的,并且第i个数的前面不能保护它的约数,我直接先对前100000的素数打表,然后输出前n个,so easy。
47 0
codeforces 302 B. Eugeny and Play List
题目链接 有n首歌,编号从1到n,每首歌播放时间为t,播放次数为c,n首歌按次序播放,有m个询问,输出第v分钟正在播放的歌曲编号。 很简单的二分查找,直接贴代码。
51 0
Shortest Path with Obstacle( CodeForces - 1547A )(模拟)
Shortest Path with Obstacle( CodeForces - 1547A )(模拟)
56 0
|
机器学习/深度学习 传感器 算法
cs224w(图机器学习)2021冬季课程学习笔记6 Message Passing and Node Classification
cs224w(图机器学习)2021冬季课程学习笔记6 Message Passing and Node Classification
cs224w(图机器学习)2021冬季课程学习笔记6 Message Passing and Node Classification
|
机器学习/深度学习 算法 搜索推荐
cs224w(图机器学习)2021冬季课程学习笔记4 Link Analysis: PageRank (Graph as Matrix)
cs224w(图机器学习)2021冬季课程学习笔记4 Link Analysis: PageRank (Graph as Matrix)
cs224w(图机器学习)2021冬季课程学习笔记4 Link Analysis: PageRank (Graph as Matrix)
CF1341C. Nastya and Strange Generator(思维 模拟 构造)
CF1341C. Nastya and Strange Generator(思维 模拟 构造)
93 0
|
人工智能 BI
CodeForces - 1485D Multiples and Power Differences (构造+lcm)
CodeForces - 1485D Multiples and Power Differences (构造+lcm)
88 0
|
机器人 定位技术
HDU-1035,Robot Motion(DFS+模拟)
HDU-1035,Robot Motion(DFS+模拟)

热门文章

最新文章