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 }