Codeforces #617 (Div. 3) C. Yet Another Walking Robot
There is a robot on a coordinate plane. Initially, the robot is located at the point (0,0)(0,0) . Its path is described as a string ss of length nn consisting of characters 'L', 'R', 'U', 'D'.
Each of these characters corresponds to some move:
- 'L' (left): means that the robot moves from the point (x,y)(x,y) to the point (x−1,y)(x−1,y) ;
- 'R' (right): means that the robot moves from the point (x,y)(x,y) to the point (x+1,y)(x+1,y) ;
- 'U' (up): means that the robot moves from the point (x,y)(x,y) to the point (x,y+1)(x,y+1) ;
- 'D' (down): means that the robot moves from the point (x,y)(x,y) to the point (x,y−1)(x,y−1) .
The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point (xe,ye)(xe,ye) , then after optimization (i.e. removing some single substring from ss ) the robot also ends its path at the point (xe,ye)(xe,ye) .
This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string ss ).
Recall that the substring of ss is such string that can be obtained from ss by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".
You have to answer tt independent test cases.
The first line of the input contains one integer tt (1≤t≤10001≤t≤1000 ) — the number of test cases.
The next 2t2t lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105 ) — the length of the robot's path. The second line of the test case contains one string ss consisting of nn characters 'L', 'R', 'U', 'D' — the robot's path.
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105 ).
For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers ll and rr such that 1≤l≤r≤n1≤l≤r≤n — endpoints of the substring you remove. The value r−l+1r−l+1 should be minimum possible. If there are several answers, print any of them.
4
4
LRUD
4
LURD
5
RRUDU
5
LLDDR
1 2
1 4
3 4
-1
数据结构题,考stl的map怎么用。用map<pair<int,int>,int>存储走过的地方,是坐标点到字符串下标的映射。只要发现下一个走到的点已经出现在字典里了,说明这一段走的路就是在兜圈子,可以直接删掉,这时候更新答案的值同时把这个点的坐标对应的值更新成最新的(因为以后要是再一次经过的话到当前位置的字符串长度肯定小于到更新前位置的字符串的长度)。最后输出答案即可。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
map<pair<int,int>,int>m;
while(t--)
{
int n;
scanf("%d",&n);
int i;
m.clear();//不要忘记清空
m[{,}]=;
int x=,y=;
int ans=;
int beg=-,end=-;
char s[];
scanf("%s",s);
for(i=;i<=n;i++)
{
char c=s[i-];
if(c=='L')
{
x-=;
}
else if(c=='R')
{
x+=;
}
else if(c=='U')
{
y+=;
}
else if(c=='D')
{
y-=;
}
if(m.find({x,y})!=m.end())
{
if(ans>i-m[{x,y}]+)
{
ans=i-m[{x,y}]+;//更新答案
beg=m[{x,y}]+;
end=i;
m[{x,y}]=i;
}
else
{
m[{x,y}]=i;//ans不必更新的话只更新这个点对应的值即可
}
}
else
{
m[{x,y}]=i;//之前没发现的话直接添加进字典 ,经过第i次操作到达(x,y)
}
}
if(ans!=&&beg!=-)cout<<beg<<' '<<end<<endl;
else cout<<-<<endl;
}
return ;
}
Codeforces #617 (Div. 3) C. Yet Another Walking Robot的更多相关文章
- Codeforces #617 (Div. 3) D. Fight with Monsters(贪心,排序)
There are nn monsters standing in a row numbered from 11 to nn . The ii -th monster has hihi health ...
- Codeforces #617 (Div. 3)B. Food Buying
Mishka wants to buy some food in the nearby shop. Initially, he has ss burles on his card. Mishka ca ...
- Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)
链接: https://codeforces.com/contest/1272/problem/B 题意: Recently you have bought a snow walking robot ...
- Codeforces Round #617 (Div. 3) 补题记录
1296A - Array with Odd Sum 题意:可以改变数组中的一个数的值成另外一个数组中的数,问能不能使数组的和是个奇数 思路:签到,如果本来数组的和就是个奇数,那就OK 如果不是,就需 ...
- Codeforces Round #617 (Div. 3) 题解
又是隔了一年才来补题的我 A.B水题就不用说了 C - Yet Another Walking Robot C题我居然卡了一会,最后决定用map水,结果出来看了看题解,居然真的是map...没想到会出 ...
- [CF百场计划]Codeforces Round #617 (Div. 3)
A. Array with Odd Sum Description You are given an array \(a\) consisting of \(n\) integers. In one ...
- C. Yet Another Walking Robot Round #617 (Div. 3)()(map + 前后相同状态的存储)
C. Yet Another Walking Robot time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces #344 Div.2
Codeforces #344 Div.2 Interview 题目描述:求两个序列的子序列或操作的和的最大值 solution 签到题 时间复杂度:\(O(n^2)\) Print Check 题目 ...
- Codeforces #345 Div.1
Codeforces #345 Div.1 打CF有助于提高做题的正确率. Watchmen 题目描述:求欧拉距离等于曼哈顿距离的点对个数. solution 签到题,其实就是求有多少对点在同一行或同 ...
随机推荐
- Linux shell sed 命令详解
详细的sed命令详解,请参考https://my.oschina.net/u/3908182/blog/1921761 sed命令常见用途 查找关键词做全局替换 查找某行的关键词做替换 查找关键字所在 ...
- RAID 0实验:mdadm
*独立冗余磁盘阵列---RAID0* RAID0: 把多块物理硬盘设备(至少两块)通过硬件或软件的方式串联在一起, 组成 一个大的卷组,并将数据依次写入到各个物理硬盘中.任意一块 硬盘发生故障将导致整 ...
- linux默认的目录结构
/: 根目录/root: root账户的home目录/home: 用户的目录,每个用户有一个home/bin: 可执行文件和命令/lib: 库文件/etc: 配置文件存放地/usr: 用户的应用程序和 ...
- Windows上搭建hexo博客
1.windows上下载git(官网太慢),建议去其他地方下载啊(右键出现 Git Bash Here 的标志就安装完成) 2.安装npm:http://nodejs.cn/download/ 3.安 ...
- 1、TensorFlow如何工作?
TensorFlow特殊的张量计算引擎使得TensorFlow能够很好的满足机器学习的计算需要,从2015年开始发起 本书基于TensorFlow0.12+和python3.0+ 环境安装要求 pip ...
- Leet Code 9.回文数
判断一个整数是否是回文数. 题解 普通解法:将整数转为字符串,然后对字符串做判断. ///简单粗暴,看看就行 class Solution { public boolean isPalindrome( ...
- appium 无法通过工具定位webview页面元素的问题
app里面页面有原生和webview的,或者H5的 1.手机百度搜索结果页面 手机百度,点击搜索输入框,输入关键字点击搜索,出来的搜索结果页面,无法通过UI automator viewer来定位元素 ...
- leetcode929 Unique Email Addresses
Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...
- java 限制每隔15分钟才允许执行一次程序
由于公司订餐平台,有个用户催单业务,每当用户点击催单按钮时,商家就会收到消息提示,如果用户频繁的发起催单请求,这样商家就会不停的收到消息提醒,所以想限制用户至少每隔15分钟才可以催单一次 我采取了以下 ...
- SQLite3约束介绍
SQLite 约束 约束是在表的数据列上强制执行的规则.这些是用来限制可以插入到表中的数据类型.这确保了数据库中数据的准确性和可靠性. 约束可以是列级或表级.列级约束仅适用于列,表级约束被应用到整个表 ...