Yet Another Walking Robot (Map)
题意描述
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 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 (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 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≤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 n (1≤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⋅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≤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.
Example input 4 4 LRUD 4 LURD 5 RRUDU 5 LLDDR output 1 2 1 4 3 4 -1
思路
这道题想优化想了很长时间,于是就去看了题解。原来的思路是寻找给定字符串的子串,字串内的操作能够抵消。而题解的做法是记录坐标,如果该坐标之前已经存在,则找到了答案。自己看问题还是不够全面,看到字符串就向字符串的角度去想,而忽视了其他重要的因素。
AC代码
#include<bits/stdc++.h>
#define x first
#define y second
#pragma GCC optimize(2)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize(3)
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target("sse3","sse2","sse")
#pragma GCC target("avx","sse4","sse4.1","sse4.2","ssse3")
#pragma GCC target("f16c")
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
#pragma GCC diagnostic error "-std=c++14"
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef long long LL;
const int N=2*1e5+10;
const int M=150;
const int INF=0x3f3f3f3f;
const int MOD=998244353;
map<PII,int> m;
int main(){
IOS;
int T;cin>>T;
while(T--){
int n;cin>>n;
string s;cin>>s;
m.clear();
PII ans={0,0};
m[{0,0}]=0;
int x=0,y=0;
int cnt=INF;
for(int i=1;i<=n;i++){
if(s[i-1]=='L') x--;
if(s[i-1]=='R') x++;
if(s[i-1]=='U') y++;
if(s[i-1]=='D') y--;
if(m.count({x,y})){
int a=m[{x,y}];
if(i-a<cnt){
cnt=i-a;
ans={a+1,i};
}
}
m[{x,y}]=i;
}
if(ans.first==0) cout<<"-1"<<endl;
else cout<<ans.first<<' '<<ans.second<<endl;
}
return 0;
}
- LINQ to SQL集成到应用程序中需考虑的一些问题
- WCF的追踪分析工具——SvcPerf
- 解决七牛云存储缓存加速Gravatar 头像图片路径url 参数失效的问题
- 命令行解析的规则以及Command Line Parser Library
- 简单代码让WordPress 支持电子邮箱(Email)作为登录名
- .NET Migration工具
- 如何有效监控.NET 应用程序
- 写入Ring Buffer
- Enterprise Library 4 缓存快速入门
- Enterprise Library 4 缓存应用程序块的设计
- 让WordPress 在RSS 中Feed 输出支持“More”标签
- WordPress文章版权保护:复制文字自动添加版权信息
- 替换WordPress 自带默认的 jQuery库, jQuery库页脚加载
- Enterprise Library 4 数据访问应用程序块
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- php如何计算两坐标点之间的距离
- Python调用C语言程序方法解析
- php workerman定时任务的实现代码
- Yii2.0 RESTful API 基础配置教程详解
- opencv 形态学变换(开运算,闭运算,梯度运算)
- 使用darknet框架的imagenet数据分类预训练操作
- php两点地理坐标距离的计算方法
- tp5(thinkPHP5)框架连接数据库的方法示例
- CI框架附属类用法分析
- ThinkPHP5.0框架结合Swoole开发实现WebSocket在线聊天案例详解
- PHP封装的page分页类定义与用法完整示例
- Swoole实现异步投递task任务案例详解
- Keras预训练的ImageNet模型实现分类操作
- 通过实例解析Python RPC实现原理及方法
- PHP实现的支付宝支付功能示例