Kattis - leftandright 字符串的处理
With modern technology advancement, it is now possible to deliver mail with a robot! There is a neighborhood on a long horizontal road, on which there are nn houses numbered 11 to nn from left to right. Every day a mail delivery robot receives a pile of letters with exactly one letter for each house. Due to mechanical restrictions, the robot cannot sort the letters. It always checks the letter on top of the pile, visits the house that should receive that letter and delivers it. The robot repeats this procedure until all the letters are delivered. As a result, each of the nn houses is visited by the robot exactly once during the mail delivery of a single day.
The mail delivery robot has a tracking device that records its delivery route. One day the device was broken, and the exact route was lost. However, the technical team managed to recover the moving directions of the robot from the broken device, which are represented as a string consisting of n−1n−1 letters. The ii-th letter of the string is ‘L’ (or ‘R’) if the (i+1)(i+1)-th house visited by the robot is on the left (or right) of the ii-th house visited. For example, if n=4n=4 and the robot visited the houses in the order of 2,4,3,12,4,3,1, its moving directions would be “RLL”.
With the moving directions, it may be possible to determine the order in which the robot visited the houses. The technical team has asked you to write a program to do that. There can be multiple orders producing the same moving directions, among which you should find the lexicographically earliest order.
Input
The input has a single integer nn (2≤n≤2⋅1052≤n≤2⋅105) on the first line. The second line has a string of length n−1n−1 consisting of letters ‘L’ and ‘R’ giving the moving directions of the robot.
Output
Output the lexicographically earliest order in which the robot may have visited the houses and delivered the letters according to the moving directions. Consider two different integer sequences of equal length A=(a1,a2,…,ak)A=(a1,a2,…,ak) and B=(b1,b2,…,bk)B=(b1,b2,…,bk), and let 1≤i≤k1≤i≤k be the lowest-numbered index where ai≠biai≠bi. Then AA is lexicographically earlier than BB if ai<biai<bi; otherwise BB is lexicographically earlier than AA.
Sample Input 1 | Sample Output 1 |
---|---|
3 LR |
2 1 3 |
Sample Input 2 | Sample Output 2 |
---|---|
6 RLLRL |
1 4 3 2 6 5 |
Sample Input 3 | Sample Output 3 |
---|---|
6 RRRLL |
1 2 3 6 5 4 |
一道字符串的处理加点思维
题意是一个邮差要送信,他一开始可以先送任意门号的信
之后要按照给出的操作送信,L代表向左走(可以走无限远),R代表向右走
输出送信顺序的门号(要求字典序最小)
思路大概是,既然一开始可以随意门号,那么不妨假设我们一开始在0号,向右走(就是在字符串之前加一个R)
剩下的我写在代码里吧。。逻辑不太好说
1 /* 2 加油,别忘记你写这段话时候的心情 3 如临深渊,如履薄冰 4 */ 5 //#include<bits/stdc++.h> 6 #include<cstdio> 7 #include<cstring> 8 #include<vector> 9 #include<algorithm> 10 #include<cmath> 11 #include<iostream> 12 #include<queue> 13 #include<set> 14 #include<map> 15 16 using namespace std; 17 typedef long long ll; 18 typedef unsigned long long ull; 19 const int maxn = 2e5+50; 20 const ll INF = 2*1e18; 21 22 int mark[maxn]; 23 24 int main() 25 { 26 int n; 27 cin >> n; 28 string s = "0R"; //因为我是从1开始计数,所以加了0R 29 string t; 30 cin >> t; 31 s = s+t; 32 int num = 1; //用来记住当前门号后的一个门号 33 for(int i = 1; i <= n; i++) 34 { 35 //cout << "*******"<<endl; 36 if(s[i] == 'R' && s[i+1] == 'L') //如果R后面是L,那就把L先送完 37 { 38 int cnt = 0; 39 for(int j = i+1; j <= n; j++) 40 { 41 if(s[j] == 'R') break; 42 cnt++; 43 } 44 num = i + cnt; 45 cout << num-- <<endl; 46 while(num >= i) 47 { 48 cout << num-- <<endl; 49 } 50 i += cnt; 51 num = i+1; 52 } 53 else if(i == n) //如果只剩一个,输出就行 54 { 55 cout << num << endl; 56 } 57 else if(s[i]== 'R' && s[i+1] == 'R') //如果都是R,那就输出num 58 { 59 cout << num++ << endl; 60 } 61 } 62 return 0; 63 }
- ASP.NET进程优化
- 多说 提速:js内页页脚加载、静态文件CDN
- 微信小程序的王者时代
- [程序设计语言]-[核心概念]-02:名字、作用域和约束(Bindings)
- NUMA架构
- 如何处理 Python 入门难以进步的现象?
- 编写前置和后置条件的连贯接口库:CuttingEdge.Conditions
- [程序设计语言]-[核心概念]-03:控制流
- 简单代码实现“网站维护中”“coming soon” 效果
- 使用Topshelf创建Windows 服务
- 自定义AuthorizeAttribute
- 系统进程管理工具Process Explorer
- jquery 操作DOM元素(1)
- 开源的读取Excel文件组件-ExcelDataReader
- 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 数组属性和方法
- 本地机器如何访问服务器上的docker容器内的tensorboard?
- MySQL是如何实现可重复读的?
- Java源码系列4——HashMap扩容时究竟对链表和红黑树做了什么?
- 如何理解被 protected 修饰的成员变量?
- 重学数据结构(五、串)
- python boto和boto3操作bucket
- 对比 Redis 中 RDB 和 AOF 持久化
- Nginx负载均衡健康检查功能
- 深入浅出hiredis -- C++操作redis
- redis学习(十五)
- nginx反向代理’/’的问题记录
- 简说中台
- JVM进阶
- MapperReduce常见错误及解决方案
- MapReduce案例之寻找共同好友