0
点赞
收藏
分享

微信扫一扫

Codeforces Round #617 (Div. 3)

舍予兄 2022-09-26 阅读 52

地址:​​http://codeforces.com/contest/1296/problem/C​​

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

Copy

4
4
LRUD
4
LURD
5
RRUDU
5
LLDDR
output

Copy

1 2
1 4
3 4
-1
    题意:上下左右移动,如果存在可以删除部分行走过程而保持结束点不变,那么删除最短的行程。不存在就输出-1

    解析:初次接触map的这个用法,不得不感慨自己的知识点还是少。好多地方还是不太明白,根据目前的理解,map储存的是x,y坐标,而map[{x,y}]对应的是x,y出现的时间点。

       要想做到这一点:需要以下语句:

               

#include<map>
map<pair<int,int>,int>mm;
if(mm.find({x,y})!=mm.end())  //表示x,y之前出现过了

         如果i............j,j+1。i处与j+1处的坐标一样,那么删掉i+1到j+1这一段,当然前提是符合最短删。

      其他的就看代码吧,目前还存在一些问题,比如map[{0,0}]=0这句加不加的问题,不加会出错,以我目前来看,它应该是表示0,0已被访问过,出现地点为i=0处,但前提是字符串从1开始输入。如果字符串从0开始输入,那么map[{0,0}]=-1,第一次出现应该在i=-1处。

      代码:

      字符串从1开始:

#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
using namespace std;
const int maxn = 2e5+10;
char s[maxn];
const int inf = 0x3f3f3f3f;
map<pair<int,int>,int>mm;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
scanf("%s",s+1);
mm.clear();      //每次清空
int l,r,minn=inf,x=0,y=0;
mm[{0,0}]=0;
for(int i=1;i<=n;i++)
{
if(s[i]=='L')
x--;
else if(s[i]=='R')
x++;
else if(s[i]=='U')
y++;
else
y--;
if(mm.find({x,y})!=mm.end())
{
if(minn>(i-mm[{x,y}]))    //保证删掉的是最短的
{
l=mm[{x,y}]+1;      //从上一次出现的下一个位置开始删除
r=i;            // 删除到当前位置
minn=i-mm[{x,y}];
}
}
mm[{x,y}]=i;  //更新当前坐标点出现的时间
}
if(minn==inf)
cout<<"-1"<<endl;
else
cout<<l<<" "<<r<<endl;
}
}

字符串从0开始的话:

#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
using namespace std;
const int maxn = 2e5+10;
char s[maxn];
const int inf = 0x3f3f3f3f;
map<pair<int,int>,int>mm;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
scanf("%s",s);
mm.clear();
int l,r,minn=inf,x=0,y=0;
mm[{0,0}]=-1; //访问了
for(int i=0;i<n;i++)
{
if(s[i]=='L')
x--;
else if(s[i]=='R')
x++;
else if(s[i]=='U')
y++;
else
y--;
if(mm.find({x,y})!=mm.end())
{
if(minn>(i-mm[{x,y}]))
{

l=mm[{x,y}]+1;
r=i;//cout<<l<<" , "<<r<<endl;
minn=i-mm[{x,y}];
}
}
mm[{x,y}]=i;
}
if(minn==inf)
cout<<"-1"<<endl;
else
cout<<l+1<<" "<<r+1<<endl;
}
}

 

    



举报

相关推荐

0 条评论