Codeforces Round #617 (Div. 3)

题目链接:https://codeforces.com/contest/1296/problem/C

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

Copy

4
4
LRUD
4
LURD
5
RRUDU
5
LLDDR

output

Copy

1 2
1 4
3 4
-1

解法:利用哈希表记录每个出现过的状态,从前到后遍历以此枚举,如果出现了之前出现过的状态,则说明这两状态之间的字符串可以删除,利用两个指针记录全局的最优解

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <unordered_map>
 5
 6 using namespace std ;
 7
 8 typedef pair<int,int> PII ;
 9
10 namespace std{//特例化哈希模板
11     template<>
12     struct hash<PII>{
13         size_t operator()(const PII &S)const{
14             return S.first ^ S.second ;
15         }
16     };
17 }
18
19 int main(){
20
21     int T ;
22     cin >> T ;
23
24     while(T--){
25         int n ;
26         string str ;
27         cin >> n >> str ;
28         unordered_map<PII,int> vis ;
29         PII cur = {0,0} ;
30         vis[cur] = 0 ;
31         int l = -1,r = n ;
32         for(int i=0;i<n;i++){
33             if(str[i] == ‘L‘) ++ cur.first ;
34             if(str[i] == ‘R‘) -- cur.first ;
35             if(str[i] == ‘U‘) ++ cur.second ;
36             if(str[i] == ‘D‘) -- cur.second ;
37             if(vis.count(cur)){
38                 if(i-vis[cur]+1<r-l+1){
39                     r = i ;
40                     l = vis[cur] ;
41                 }
42             }
43             vis[cur] = i + 1 ;
44         }
45         if(l == -1){
46             cout << "-1" << endl ;
47         }else{
48             cout << l + 1 << ‘ ‘ << r + 1 << endl ;
49         }
50     }
51     return 0 ;
52 }  

...

原文地址:https://www.cnblogs.com/gulangyuzzz/p/12262795.html

时间: 2024-10-06 00:53:24

Codeforces Round #617 (Div. 3)的相关文章

Codeforces Round #617 (Div. 3) 题解

目录 Codeforces Round #617 (Div. 3) 题解 前言 A. Array with Odd Sum 题意 做法 程序 B. Food Buying 题意 做法 程序 C. Yet Another Walking Robot 题意 做法 程序 D. Fight with Monsters 题意 做法 程序 E1. String Coloring (easy version) 题意 做法 程序 E2. String Coloring (hard version) 题意 做法

[Codeforces Round #617 (Div. 3)] 题解 A,B,C,D,E1,E2,F

[Codeforces Round #617 (Div. 3)] 题解 A,B,C,D,E1,E2,F 1296A - Array with Odd Sum 思路: 如果一开始数组的sum和是奇数,那么直接YES, 否则:如果存在一个奇数和一个偶数,答案为YES,否则为NO 代码: int n; int a[maxn]; int main() { //freopen("D:\\code\\text\\input.txt","r",stdin); //freopen(

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

http://codeforces.com/contest/1296/problem/C 题意:给一段字符串表示移动,然后求删除最短的一段,并且不影响结果 题解: 意思是:建立pair点和map,当遍历到第i个点有一个pair值,把这个加到map里面,如果向后接着遍历时出现与i点相同的pair值时,那么这一段表示可以删除的一段 #include <bits/stdc++.h> using namespace std; int main() { #ifdef _DEBUG freopen(&qu

Codeforces Round #617 (Div. 3) 题解 1296C 1296D 1296E 1296F

C 对每一步结束之后往map里存个位置,看停留在相同位置的最少差多少步就行了. D 由\(h\%(a+b)\leq (x+1)a\)得\(x_{min}=\lceil\frac{h\%(a+b)}{a}\rceil-1\).然后贪心即可. E1 注意到要把原字符串排序,每一对逆序对都要进行一次交换,即每一对逆序对的颜色都必须不同.因此所需的颜色数即为最长的逆序链长. 对E1,可以考虑建图,每个位置看成一个点,把每一对逆序对建边,很显然这就是判断是不是一张二分图. E2 对E2,依次考虑字母a,b

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

1296A - Array with Odd Sum 题意:可以改变数组中的一个数的值成另外一个数组中的数,问能不能使数组的和是个奇数 思路:签到,如果本来数组的和就是个奇数,那就OK 如果不是,就需要把数组中其中一个奇(偶)数改成偶(奇)数,相当于加一减一 所以测一下这个数组如果有个奇数并且还有个偶数就行 #include <cstdio> #include <iostream> #include <map> #include <set> #include

Codeforces Round #617 (Div. 3)F. Berland Beauty

题意: 给一棵树,边权未知,现在给m组约束,每组约束给出从u到v路径中的最小值,现在让你给出一组边权,使得符合之前的约束,不能给出输出-1 思路: 因为n较小,对于每组约束我们可以直接暴力修改路径上的权值,如果边的权值小于当前约束的最小值,则将权值修改,最后再根据每组约束暴力走一遍路径看路径是否满足要求,如果不满足则输出-1,最后还得对那些没有修改过的边随意赋值 #include<iostream> #include<algorithm> #include<vector>

Codeforces Round #617 (Div. 3) D. Fight with Monsters

题意:打怪,拿分,不同就是可以让别人跳过打怪哪一个环节,不过只有k次 题解:算出每个怪消耗的k,排序 ,贪心 原文地址:https://www.cnblogs.com/RE-TLE/p/12288935.html

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿