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;
}
来源:CSDN
作者:做一只大熊猫
链接:https://blog.csdn.net/qq_34261446/article/details/104187845