A. Robot Sequence
2016-02-25 18:24
567 查看
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
output
input
output
input
output
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.
解题说明:此题是一道模拟题,首先判断真正需要走的步数,然后再计算不同的方法。
#include<cstdio>
#include <cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int a,b,c,d,e,i,j,f;
char m[202];
scanf("%d",&a);
getchar();
for(i=0;i<a;i++)
{
scanf("%c",&m[i]);
}
f=0;
for(i=0;i<a;i++)
{
b=0;
c=0;
d=0;
e=0;
for(j=i;j<a;j++)
{
if(m[j]=='L')
{
b++;
}
if(m[j]=='R')
{
c++;
}
if(m[j]=='U')
{
d++;
}
if(m[j]=='D')
{
e++;
}
if(b==c&&d==e)
{
f++;
}
}
}
printf("%d\n",f);
return 0;
}
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.
解题说明:此题是一道模拟题,首先判断真正需要走的步数,然后再计算不同的方法。
#include<cstdio>
#include <cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int a,b,c,d,e,i,j,f;
char m[202];
scanf("%d",&a);
getchar();
for(i=0;i<a;i++)
{
scanf("%c",&m[i]);
}
f=0;
for(i=0;i<a;i++)
{
b=0;
c=0;
d=0;
e=0;
for(j=i;j<a;j++)
{
if(m[j]=='L')
{
b++;
}
if(m[j]=='R')
{
c++;
}
if(m[j]=='U')
{
d++;
}
if(m[j]=='D')
{
e++;
}
if(b==c&&d==e)
{
f++;
}
}
}
printf("%d\n",f);
return 0;
}
相关文章推荐
- 【SPOJ- HORRIBLE】Horrible Queries【线段树】
- UITableView优化之按需加载
- Mysql插入数据报错java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x8D\xE8\xBE...'
- 45.Which two statements are true about sequences created in a single instance database? (Choose two.
- iOS UINavigationController popToViewController用法
- UINavigationController--导航控制器
- 前端框架easyui的使用
- UITextField,UITextView回收键盘
- GPUimage 简单使用(-)单个滤镜使用
- 【Hibernate】query查询不报错卡住不动不往下执行的解决办法
- 笔记之-uevent机制
- POJ 3601-Subsequence【尺取法】
- UILabel的一些用法
- uiautomator中一些常用的方法
- UI基本三控件
- UVA 10401 Injured Queen Problem
- UITableView属性及方法大全
- UIModalPresentationStyle和UIModalTransitionStyle
- UI控件学习
- 【SPOJ-GSS1】Can you answer these queries I【线段树】【最大子段和】