小组练习第一周------L

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 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) 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 ss) 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 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≤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 nn (1≤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).
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 lland 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.

input
4
LRUD
4
LURD
5
RRUDU
5
LLDDR
outputCopy1 2
1 4
3 4

题意:该题给出字符串是由四种字母组成,分别代表着向四个方向运动,要求删除最小的字符串,且不改变物体运动的终点
想法:感觉这题的思路不是特别的难但是却不能用代码表示出来,感觉还是比较麻烦,所以就找到CF上的原题看了别人的代码,看了好几个都感觉代码特别麻烦,主要还是我看不懂,不过还是找了个比较好懂得,感觉他用得都比较好,而且我刚好可以理解
思路:给定一个坐标,然后用坐标当map得键,遍历字符串,如果出现重复得键则这段可以删除,判断与ans得大小比较,如果较小则换掉原来的值,且给出可取字符串得新位置,最后字接判断有误有输出位置,没有输出-1

代码:

import java.math.BigInteger;
import java.util.*;
public class Main{
 static Map<String, Integer> map;//记录走过的位置,键来记录坐标,如果出现重复则比较步数的多少,留下最小的
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        while(n-- > 0){
            int len = Integer.parseInt(sc.nextLine());
            char[] chars = sc.nextLine().toCharArray();
            map = new HashMap<>();
            map.clear();
            map.put(0 + " " + 0, 0);
            solve(chars, len, 0, 0);
        }
    }
    public static void solve(char[] chars, int len, int x, int y){
        int ans = Integer.MAX_VALUE;
        int l = -1;
        int r = -1;
        for(int i = 1; i <= len; i++){
            switch (chars[i - 1]){//记录走到的位置
                case 'L':
                    x = x - 1;
                    break;
                case 'R':
                    x = x + 1;
                    break;
                case 'U':
                    y = y + 1;
                    break;
                case 'D':
                    y = y - 1;
                    break;
            }
            if(map.containsKey(x + " " + y)){//如果出现重复则留下较小的步数
                int t = map.get(x + " " + y);
                int d = i - t;
                if(d < ans){//ans代表当前最小的情况
                    ans = d;
                    l = t + 1;
                    r = i;
                }
                map.replace(x + " " + y, i);//替换
            }else map.put(x + " " + y, i);
        }
        if(ans == Integer.MAX_VALUE){
            System.out.println(-1);
            return;
        }
        System.out.println((l)+ " " + (r));
 
    }
}

感悟:他的这个代码用到了我不是太熟得HashMap,看了感觉多少加深了对Map得理解,还有就是它用了我很少用得switch语句,代码更加简洁了,还有其他的一些方面,感觉可以学习一下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值