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
数据结构题,考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的更多相关文章

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

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

  3. Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)

    链接: https://codeforces.com/contest/1272/problem/B 题意: Recently you have bought a snow walking robot ...

  4. Codeforces Round #617 (Div. 3) 补题记录

    1296A - Array with Odd Sum 题意:可以改变数组中的一个数的值成另外一个数组中的数,问能不能使数组的和是个奇数 思路:签到,如果本来数组的和就是个奇数,那就OK 如果不是,就需 ...

  5. Codeforces Round #617 (Div. 3) 题解

    又是隔了一年才来补题的我 A.B水题就不用说了 C - Yet Another Walking Robot C题我居然卡了一会,最后决定用map水,结果出来看了看题解,居然真的是map...没想到会出 ...

  6. [CF百场计划]Codeforces Round #617 (Div. 3)

    A. Array with Odd Sum Description You are given an array \(a\) consisting of \(n\) integers. In one ...

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

  8. Codeforces #344 Div.2

    Codeforces #344 Div.2 Interview 题目描述:求两个序列的子序列或操作的和的最大值 solution 签到题 时间复杂度:\(O(n^2)\) Print Check 题目 ...

  9. Codeforces #345 Div.1

    Codeforces #345 Div.1 打CF有助于提高做题的正确率. Watchmen 题目描述:求欧拉距离等于曼哈顿距离的点对个数. solution 签到题,其实就是求有多少对点在同一行或同 ...

随机推荐

  1. VIM - EX 命令 - 文件读写

    VIM - EX 命令 - 文件读写 1. 概述 vim 通过 ex 命令行, 与其他文件的读写操作 2. 场景 场景1 vim 打开文本 将当前文本的内容, 写入到其他文本 场景2 vim 打开文本 ...

  2. 删除文件时提示,你需来自SYSTEM的权限

    1. 提示如下 2. 对要删除的文件操作如下 2.1 为删除的文件添加本地账户 2.2 提示如下,多点几次继续就好 2.3 给本地账户添加完全控制权限

  3. css颜色+透明度的写法

    今天在学习页面的时候,看到视频里用到颜色的十六进制表达式直接设置透明度,但是后来在实践过程中发现是有误的,特此记录一下,也算是学习了一个新知识. RGBA表示式 比如我们设置rgba(0, 0, 0, ...

  4. The Preliminary Contest for ICPC Asia Xuzhou 2019 K. Center

    这题对于能加入最多边缘点的center点,这个点就是最优的center ,对于center点,总共是n^2的,顶多也就1e6,所以直接双重循环就行了, 然后map<pair,set >映射 ...

  5. 【应急响应】Windows 安全加固

    一.补丁管理 运行cmd,输入systeminfo查看目前补丁信息 二.账户管理 gpedit.msc —>Windows设置—>安全设置—>本地设置—>账户设置 密码策略: ...

  6. openresty-component

    1.Array Var Nginx Module ArrayVarNginxModulelocation /foo { array_split ',' $arg_files to=$array; # ...

  7. leetcode929 Unique Email Addresses

    Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...

  8. 深入理解Java虚拟机(1)

    Java内存区域 对于Java程序员来说,在虚拟机的自动内存管理机制下,不再需要为每一个new操作去写配对的delete和free代码,不容易出现内存泄露和内存溢出问题,可以直接交给虚拟机进行管理. ...

  9. 蚁人cp数

    可怜的蚁人进入量子领域后,黄蜂女被灭霸的一个响指带走,导致可怜的蚁人困在了量子领域,为了生存,他们开始建造自己家园. 蚁人为了方便在这里生存,他们建造了自己火车站.某车站有N个人上车,其中M对是情侣, ...

  10. php源码加密--screw plus

    screw plus是一个开源的php扩展,作用是对php文件进行加密,网络上提供php加密的服务很多,但大多都只是混淆级别的加密,被人拿到加密文件问只要有足够耐心就能破解,与之不同的是,screw ...