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.

Input
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).

Output
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.

Example
Input
4
4
LRUD
4
LURD
5
RRUDU
5
LLDDR
Output
1 2
1 4
3 4
-1
思路:对于每一个坐标点,只要重复出现,这一段就可以删除。那么我们去按照字符串遍历有可能走到的坐标,如果出现过,就尽可能小的去更新答案。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;map<pair<int,int>,int> mp;
string s;
int n;int main()
{int t;scanf("%d",&t);while(t--){scanf("%d",&n);cin>>s;int _max=1e9;int x,y,l=0,r=0;mp.clear();mp[make_pair(0,0)]=-1;s=" "+s;for(int i=1;i<s.size();i++){if(s[i]=='L') l--;else if(s[i]=='R') l++;else if(s[i]=='U') r++;else r--;int zz=mp[make_pair(l,r)];if(zz!=0){if(zz==-1) zz=0;if(i-zz<_max){_max=i-zz;x=zz+1,y=i;}}mp[make_pair(l,r)]=i;}if(_max==1e9) cout<<-1<<endl;else cout<<x<<" "<<y<<endl;}return 0;
}

努力加油a啊,(o)/~

Yet Another Walking Robot CodeForces - 1296C相关推荐

  1. Kuro and Walking Route CodeForces - 979C (树上DFS)

    Kuro is living in a country called Uberland, consisting of nn towns, numbered from 11to nn, and n−1n ...

  2. Walking Robot

    https://codeforces.com/contest/1154/problem/D 题意:有个机器人在0位置处,现在要走到位置n.机器人现在有一个电池容量为a,还有一个蓄电池容量为b(一开始都 ...

  3. Ciel and Robot CodeForces - 322C

    Fox Ciel has a robot on a 2D plane. Initially it is located in (0, 0). Fox Ciel code a command to it ...

  4. C#LeetCode刷题之#874-模拟行走机器人​​​​​​​(Walking Robot Simulation)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4038 访问. 机器人在一个无限大小的网格上行走,从点 (0, 0 ...

  5. Leetcode 874. Walking Robot Simulation

    文章作者:Tyan 博客:noahsnail.com  |  CSDN  |  简书 1. Description 2. Solution class Solution { public:int ro ...

  6. LeetCode.874-走路机器人模拟(Walking Robot Simulation)

    这是悦乐书的第335次更新,第360篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第205题(顺位题号是874).网格上的机器人从点(0,0)开始并朝北.机器人可以接收三 ...

  7. Codeforces Round #552 (Div. 3) A B C D E F G (暴力,dp,模拟)

    题目链接:https://codeforces.com/contest/1154 A:Restoring Three Numbers B:Make Them Equal C:Gourmet Cat D ...

  8. Codeforces Round #552 (Div. 3)D、E题解

    D. Walking Robot 题意 机器人在一维坐标轴上从0走到x,中途可以在有光的地方可以选择给太阳能电池充电,每次移动都要消耗一单位电,蓄电池容量为a,太阳能电池容量为b,一开始都是满电,问机 ...

  9. Codeforces Round #617 (Div. 3)

    A - Array with Odd Sum 题意就是一个数组,你可以把数组中任意一个数变成数组中的另外一个数,让你进行这样的操作,求最后能否使这个数组的和为奇数: 思路: 只要这个数组中存在一个奇数 ...

最新文章

  1. windows令牌学习
  2. python编程100行_自己动手写100行Python代码抢火车票!
  3. DVWA暴力破解(Brute Force)——全等级(Low,Medium,High,lmpossible)精讲
  4. MyBatis 思维导图,让 MyBatis 不再难懂(一)
  5. 网易新闻 时事新闻抓取链接
  6. 动态规划——莱文斯坦距离
  7. c++查询当前文件夹下文件数目_python3自动化小工具--删除某个文件夹xx后缀文件...
  8. 网页html语言怎么看,怎样查看网页的css代码?
  9. docker 实战---使用oracle xe作为开发数据库(六)
  10. python中去除异常值_在Python中获取异常值
  11. this.$router.push相关的vue-router的导航方法
  12. 单例模式之懒汉式(线程安全)
  13. c++用化简命题逻辑公式的方法设计一个5人表决开关电路,要求3人以上(含3人)同意则表决通过
  14. Crackme 21
  15. asterisk的sip.conf配置
  16. Postman安装流程
  17. 机器学习、数据挖掘、神经网络、人工智能和模式识别之间,主要是什么关系
  18. 迪士尼挖角波士顿动力,耗时3年打造漫威英雄机器人,1:1复刻效果堪比CG
  19. 当元宇宙时代来临,才真正让这些新技术跳出了互联网的牵绊
  20. Help Hanzo(区间素数筛)

热门文章

  1. 微信小程序实现数字为四位一组间隔(仿银行卡卡号)
  2. 中超联赛提交函数的c语言,国家体育总局
  3. cygwin swoole_swoole入门--------基础概念
  4. 彼聆智能语音机器人_人工智能2.0时代,创造他们的究竟是谁?
  5. osgCallback的实现方法及原理
  6. osg下物体绕自身轴旋转
  7. Android开发之下载Apk安装的方法兼容Android7.0和8.0及以上
  8. matlab文件目录表示,Matlab - 文件目录路径操作_读取不同路径下的相同文件名表格...
  9. php text留言本,PHP+TEXT留言本(五)
  10. linux怎么衡量负载大小,如何查看linux机器的平均负载