思路:
简单的思维题,应该从后往前遍历操作。如果后面的对数i操作过,则前面对数i的操作都可以无视。可以通过栈这种数据结构实现后往前遍历。
AC代码:
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();
int N = (int) in.nval;
int[] order = new int[N+5];
boolean[] visited = new boolean[N+5];
Stack<Integer> nums = new Stack<>();
Stack<Integer> result = new Stack<>();
//0代表左移 1代表右移
Stack<Integer> operate = new Stack<>();
in.nextToken();
int M = (int) in.nval;
while(M--!=0){
in.nextToken();
if(in.sval.equals("L")){
operate.push(0);
}else {
operate.push(1);
}
in.nextToken();
nums.push((int) in.nval);
}
while(!operate.empty()){
if(visited[nums.peek()]){
nums.pop();
operate.pop();
continue;
}
visited[nums.peek()]=true;
if(operate.pop()==0){
System.out.print(nums.pop()+" ");
}else {
result.push(nums.pop());
}
}
for (int i = 1; i <= N; i++) {
if(!visited[i]){
System.out.print(i+" ");
}
}
while(!result.empty()){
System.out.print(result.pop()+" ");
}
}
}