Codeforces——C. Yet Another Walking Robot

There is a robot on a coordinate plane. Initially, the robot is located at the point (0,0). Its path is described as a string s of length n 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) to the point (x−1,y);
‘R’ (right): means that the robot moves from the point (x,y) to the point (x+1,y);
‘U’ (up): means that the robot moves from the point (x,y) to the point (x,y+1);
‘D’ (down): means that the robot moves from the point (x,y) to the point (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), then after optimization (i.e. removing some single substring from s) the robot also ends its path at the point (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 s).

Recall that the substring of s is such string that can be obtained from s 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 t independent test cases.

Input

The first line of the input contains one integer t (1≤t≤1000) — the number of test cases.

The next 2t lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer n (1≤n≤2⋅1e5) — the length of the robot’s path. The second line of the test case contains one string s consisting of n characters ‘L’, ‘R’, ‘U’, ‘D’ — the robot’s path.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅1e5 (∑n≤2⋅1e5).

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 l and r such that 1≤l≤r≤n — endpoints of the substring you remove. The value r−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

题目大意

给你一串字符串,删除一个子串,使最后的结果不变,并且子串的长度要最小。

分析

先赋初值,然后对字符串从前往后遍历,记录下操作后的点对应的位置,若在之后的操作中出现了相同的点,则只需要比较当前点的位置减去前一次点出现的位置与最小值的大小,并将小的那个赋给最小值,最后判断是否出现过相同的点,若没有,则输出-1,反之,输出最小值。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
map<pair<int,int>,int> mp;
int main()
{int t,n;string s;cin>>t;while(t--){cin>>n>>s;int x=0,y=0,l=0,r=n;mp.clear();mp[{0,0}]=1;for(int i=0;i<s.size();i++){if(s[i]=='L'){x--;}else if(s[i]=='R'){x++;}else if(s[i]=='U'){y++;}else{y--;}if(mp[{x,y}]!=0&&r-l>i-mp[{x,y}]+1){l=mp[{x,y}];r=i+1;}mp[{x,y}]=i+2;}if(l==0){cout<<-1<<endl;}else{cout<<l<<" "<<r<<endl;}}return 0;
}

Codeforces——C. Yet Another Walking Robot相关推荐

  1. Yet Another Walking Robot CodeForces - 1296C

    There is a robot on a coordinate plane. Initially, the robot is located at the point (0,0)(0,0). Its ...

  2. Walking Robot

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

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

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

  4. Leetcode 874. Walking Robot Simulation

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

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

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

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

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

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

  8. Codeforces Round #617 (Div. 3)

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

  9. codeforces每日5题(均1500)-第二十一天

    Walking Robot 题面翻译 Description 在一个数轴上,有一个机器人要从 x=0x = 0x=0 处移动到 x=nx = nx=n 处.机器人身上有两种电池,第一种是普通电池,第二 ...

  10. android xlintdeprecation 重新编译,天博体育官网-官网首页

    PC Flutter 裁剪布局之 ClipRect.ClipRRect.ClipOval.ClipPath.CustomClipper. 斯坦福大学公开课机器学习:Neural network-mod ...

最新文章

  1. 数据库设计的三大范式
  2. 自然语言处理中N-Gram模型介绍
  3. MLPclassifier,MLP 多层感知器的的缩写(Multi-layer Perceptron)
  4. leetcode-Symmetric Tree 对称树
  5. python logging模块的作用_【python】【logging】python日志模块logging常用功能
  6. C++ 返回类型协变
  7. Git生成SSH共钥
  8. 常用网络协议的端口号
  9. JavaScript函数及其作用
  10. 让终端窗口“下雪”的有趣指令
  11. simics虚拟机+solaris 9 sparc系统运行memory compiler(非常详细)
  12. 做好PMC管理三大工作,轻松搞定生产计划与物料控制
  13. 图形编辑器:对齐功能的实现
  14. android把后台应用放最上层,Android 判断app是否在最上层展示
  15. zk - zookeeper主节点、从节点、客户端三者之间的交互
  16. 生活中的收支明细该如何记录
  17. 前端上班第一天-开发环境配置
  18. 嵌入式开发者技能大全
  19. 标题 穿越雷区 java_标题:穿越雷区
  20. Maya中的场景与Unity3D中的场景匹配

热门文章

  1. 笔记本电脑外接显示屏的分辨率设置,外接显示屏分辨率总是低一点的解决方法
  2. Ubuntu20.04 搭建repo + gitlab的代码管理系统
  3. Xcode debug时如何看crash的call stack
  4. 短信通知接口json报文开发设计总结
  5. 【SIGIR2017满分论文】IRGAN:大一统信息检索模型的博弈竞争
  6. Eclipse 更换皮肤
  7. Life feelings--13--青春不毕业,那些心里念念叨叨难以忘怀的记忆
  8. 【开源】.net微服务开发引擎Anno 让复杂的事简单点- 日志、链路追踪一目了然 (上)
  9. 丧心病狂的前端冷知识
  10. 【转载】征途单机版详细架设图文教程