Codeforces #617 (Div. 3) C. Yet Another Walking Robot
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.
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 ).
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.
4 4 LRUD 4 LURD 5 RRUDU 5 LLDDR
1 2 1 4 3 4 -1
数据结构题,考stl的map怎么用。用map<pair<int,int>,int>存储走过的地方,是坐标点到字符串下标的映射。只要发现下一个走到的点已经出现在字典里了,说明这一段走的路就是在兜圈子,可以直接删掉,这时候更新答案的值同时把这个点的坐标对应的值更新成最新的(因为以后要是再一次经过的话到当前位置的字符串长度肯定小于到更新前位置的字符串的长度)。最后输出答案即可。
#include <bits/stdc++.h> using namespace std; int main() { int t; cin>>t; map<pair<int,int>,int>m; while(t--) { int n; scanf("%d",&n); int i; m.clear();//不要忘记清空 m[{0,0}]=0; int x=0,y=0; int ans=100000000; int beg=-1,end=-1; char s[200005]; scanf("%s",s); for(i=1;i<=n;i++) { char c=s[i-1]; if(c=='L') { x-=1; } else if(c=='R') { x+=1; } else if(c=='U') { y+=1; } else if(c=='D') { y-=1; } if(m.find({x,y})!=m.end()) { if(ans>i-m[{x,y}]+1) { ans=i-m[{x,y}]+1;//更新答案 beg=m[{x,y}]+1; end=i; m[{x,y}]=i; } else { m[{x,y}]=i;//ans不必更新的话只更新这个点对应的值即可 } } else { m[{x,y}]=i;//之前没发现的话直接添加进字典 ,经过第i次操作到达(x,y) } } if(ans!=99999999&&beg!=-1)cout<<beg<<' '<<end<<endl; else cout<<-1<<endl; } return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战