Codeforces Round #617 (Div. 3)——C. Yet Another Walking Robot

Codeforces Round #617 (Div. 3)——C. Yet Another Walking Robot

/ 0评 / 990次 / 1

Description

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 \((x_e,y_e)\), then after optimization (i.e. removing some single substring from \(s\)) the robot also ends its path at the point \((x_e,y_e)\).

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 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 \(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 \(2×t\) 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⋅10^5)\) — 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 \(n\) over all test cases does not exceed \(2⋅10^5\) \((∑n≤2⋅10^5)\).

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

题文翻译

一个机器人(0,0)点可以进行上、下、左、右的操作,给你系列操作符(UDLR)让你简化(删除)他的操作指令,达到的终点都是同一位置。只能简化一个区间的。如果不能删除,输出-1,否则输出删除的左端点\(l\)和右端点\(r\),使\(r-l+1\)应该尽可能小,若有多个相同的答案,输出一个就行。

Analysis

起初我看到这个题目把他当做一种字母串匹配的变式来做的,也就是从字符串处理的角度去出发,并不能得解,这个题目有趣的地方在于他其实可以从模拟的角度去出发,直接模拟整个运动过程,从而截取一个没有产生实际移动的命令串。

在大神的点拨之下,我想到了使用map去记录状态并加以处理

将状态以结构体的形式存储,状态的键值则记录着当前的时间位置,一旦经历到相同的位置,比较时间差距(也就是删去的字符串长度),最后将最小者输出即可

Accepted Code

/*
 * @Author: Gehrychiang
 * @Date: 2020-02-05 11:28:14
 * @Website: www.yilantingfeng.site
 * @E-mail: gehrychiang@aliyun.com
 */
#include <bits/stdc++.h>
using namespace std;
struct $
{
    int x;
    int y;
    bool operator<(const $ &a) const//重载运算符因为结构体直接没有办法直接在红黑树中比较
    {
        if (x == a.x)
            return y < a.y;
        return x < a.x;
    }
};

map<$, int> m;
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        m.clear();
        int n;
        int minm = 300005, ansl = 0, ansr = 0;
        cin >> n;
        char a[200005];
        cin >> a;
        $ cur;
        cur.x = 0;
        cur.y = 0;
        m[cur] = -1; //额外标记。初始位置为-1。其实本来初始位置应该是0,但是这就没有办法去与未走过的部分加以分辨了,故给初始位点一个独特的值
        for (int i = 0; i < n; i++)
        {
            if (a[i] == 'L')
            {
                cur.x--;
            }
            else if (a[i] == 'R')
            {
                cur.x++;
            }
            else if (a[i] == 'U')
            {
                cur.y++;
            }
            else if (a[i] == 'D')
            {
                cur.y--;
            }

            if (m[cur] == 0) //未经过
            {
                m[cur] = i + 1;
            }
            else
            {
                if (m[cur] == -1) //初始位置(其实应该是0不过被挤占了)
                {
                    if (i + 1 - m[cur] - 1 < minm)
                    {
                        ansl = m[cur] + 1 + 1;
                        ansr = i + 1;
                        minm = i + 1 - m[cur] - 1;
                    }
                    m[cur] = i + 1;
                    if (minm == 2)
                        break;
                }
                else
                {
                    if (i + 1 - m[cur] < minm)
                    {
                        ansl = m[cur] + 1;
                        ansr = i + 1;
                        minm = i + 1 - m[cur];
                    }
                    m[cur] = i + 1;
                    if (minm == 2)//最小距离就是2个一组,找到就可以直接跳出了
                        break;
                }
            }
        }
        if (ansl == 0 && ansr == 0)
        {
            cout << "-1" << endl;
        }
        else
        {
            cout << ansl << " " << ansr << endl;
        }
    }
    return 0;
}

本题中使用到了在map中使用没有办法直接比较大小的键的时候的操作,就需要额外的自己去重载运算符,更为简易的做法还可以参考使用pair的做法,不需要去重载运算符,但只能是两个元素构成的结构体,其实pair的底层实现也就是结构体,大家可以相互学习。

发表回复

您的电子邮箱地址不会被公开。

你好 No.63967