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

standard input

output

standard output

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 𝑠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 (𝑥−1,𝑦)(x−1,y); ‘R’ (right): means that the robot moves
from the point (𝑥,𝑦)(x,y) to the point (𝑥+1,𝑦)(x+1,y); ‘U’ (up):
means that the robot moves from the point (𝑥,𝑦)(x,y) to the point
(𝑥,𝑦+1)(x,y+1); ‘D’ (down): means that the robot moves from the
point (𝑥,𝑦)(x,y) to the point (𝑥,𝑦−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), 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≤𝑡≤10001≤t≤1000) — the number of test cases.

The next 2𝑡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≤𝑛≤2⋅1051≤n≤2⋅105) — 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⋅1052⋅105 (∑𝑛≤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 𝑙l and 𝑟r such that
1≤𝑙≤𝑟≤𝑛1≤l≤r≤n — endpoints of the substring you remove. The value
𝑟−𝑙+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

Copy

1 2
1 4
3 4
-1
Solution

思路如下

  • 题意:删除一个最小的 移动周期(‘U’ 与’D’、 ‘L’ 与’R’ 个数相同)(必须是周期,这样才不改变结束位置,必须是连续的字符串),在不改变 最终结束时的位置
  • 思路:我们考虑:当我们删除某个周期之后,那么在这个周期之前面那一个位置的状态(‘U’ 、‘D’、 ‘L’ 、'R’的数量) 与 这个周期结束位置的后面的第一个位置的状态(‘U’ 、‘D’、 ‘L’ 、'R’的数量)应该是相同的,而这题 我们可以同map 的特性 去存储我们所需要的 “某个位置的状态(作为map的 key)”,若是在以后的遍历中 出现了 与这个状态相同的状态,那么我们就 可以利用两个位置(作为map的值val) 去求 区间的长度和 ,区间的段点。

题解如下

#include<iostream>
#include<map>
 
using namespace std;
 
int main() {
    //freopen("T.txt", "r", stdin);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        string s;
        cin >> n >> s;
        map<pair<int , int> , int> vis;	//key 为状态、val 存储的为 位置 + 1
        pair<int , int> cur(0,0); 		//最开始的状态
        vis[cur] = 0;					//把 最开始的状态cur 和 出现这个状态的位置(实际上 0 是 +1 的得到的) 0 
        int l = -1, r = n;
        for(int i = 0; i < n; i ++)
        {
            if(s[i] == 'L') cur.first --;
            if(s[i] == 'R') cur.first ++;
            if(s[i] == 'U') cur.second ++;
            if(s[i] == 'D') cur.second --;
            if(vis.count(cur))
            {
            	// i - vis[cur] 当前状态 与 上一次出现该状态 之间的 距离
            	// r - l + 1 当前最优距离
                if(i - vis[cur] + 1 < r - l + 1)
                {
                    l = vis[cur];
                    r = i;
                }
            }
 
            vis[cur] = i + 1;
        }
 
        if(l == -1)
            cout << -1 << endl;
        else
            cout << l + 1 << " " << r + 1 <<endl;
    }
 
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值