博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 626A Robot Sequence(模拟)
阅读量:6576 次
发布时间:2019-06-24

本文共 1856 字,大约阅读时间需要 6 分钟。

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.

题目链接:

题意:在一块方格里,给出一串指令包含U,D,L,R这些字母,分别表示上下左右移动一格。问有多少个子串(包含自身)能使得物体回到出发位置。

分析:模拟一下找子串的个数即可!

解释下样例3:子串为:RL,RLRL,RLRLRL,LR,LRLR,LRLRLR,RL,RLRL,LR,LRLR,RL,LR,共12条,所以输出为12

下面给出AC代码:

 

1 #include 
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

 

 

 

转载地址:http://cswno.baihongyu.com/

你可能感兴趣的文章
玩转HTML5移动页面
查看>>
Please review your Gradle project setup in the android/ folde
查看>>
2019春第六周作业
查看>>
紫书 例题 10-14 UVa 12034(组合数+递推)
查看>>
设计模式学习每天一个——Decorator模式
查看>>
.NET程序集(Assembly)
查看>>
2017年浙江中医药大学大学生程序设计竞赛(重现赛)D - CC的神奇背包
查看>>
HDU 3416 Marriage Match IV
查看>>
原创:2016.4.25-2016.5.1 C# informal essay and tittle_tattle
查看>>
mybatis高级(3)_延迟加载_深度延迟_一级缓存_二级缓存
查看>>
从今天起,开源中国
查看>>
带左右按钮、 渐隐渐现 轮播图
查看>>
body标签中的相关标签
查看>>
css3 animate 和关键帧 @-webkit-keyframes
查看>>
指向结构体变量的指针变量
查看>>
文字链接颜色设置
查看>>
ChannelHandler揭秘(Netty源码死磕5)
查看>>
图片转流
查看>>
Jessica's Reading Problem
查看>>
jQuery实现照片墙,附步骤详解
查看>>