Codeforces--1200A--Hotelier

題目描述:
Amugae has a hotel consisting of 10 rooms. The rooms are numbered from 0 to 9 from left to right.

The hotel has two entrances — one from the left end, and another from the right end. When a customer arrives to the hotel through the left entrance, they are assigned to an empty room closest to the left entrance. Similarly, when a customer arrives at the hotel through the right entrance, they are assigned to an empty room closest to the right entrance.

One day, Amugae lost the room assignment list. Thankfully Amugae’s memory is perfect, and he remembers all of the customers: when a customer arrived, from which entrance, and when they left the hotel. Initially the hotel was empty. Write a program that recovers the room assignment list from Amugae’s memory.
輸入描述:
The first line consists of an integer n (1≤n≤105), the number of events in Amugae’s memory.
The second line consists of a string of length n describing the events in chronological order. Each character represents:
‘L’: A customer arrives from the left entrance.
‘R’: A customer arrives from the right entrance.
‘0’, ‘1’, …, ‘9’: The customer in room x (0, 1, …, 9 respectively) leaves.
It is guaranteed that there is at least one empty room when a customer arrives, and there is a customer in the room x when x (0, 1, …, 9) is given. Also, all the rooms are initially empty.
輸出描述:
In the only line, output the hotel room’s assignment status, from room 0 to room 9. Represent an empty room as ‘0’, and an occupied room as ‘1’, without spaces.
輸入:
8
LLRL1RL1
9
L0L0LLRR9
輸出:
1010000011
1100000010
題意:
Amugae的酒店由10人組成10客房。房間從0開始編號0到9 從左到右。 酒店有兩個入口 - 一個來自左端,另一個來自右端。當顧客通過左入口到達酒店時,他們被分配到最靠近左入口的空房間。類似地,當顧客通過右入口到達酒店時,他們被分配到最靠近右入口的空房間。 有一天,Amugae失去了房間分配清單。值得慶幸的是,Amugae的記憶非常完美,他記得所有顧客:當顧客到達時,從哪個入口到他們離開酒店。最初酒店是空的。編寫一個程序,從Amugae的記憶中恢復房間分配列表。
題解
直接暴力
代碼:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;

const int maxn = 100000 + 5;
char s[maxn],op[15];

int main(){
    int n;
    while(scanf("%d%s",&n,s)!=EOF){
        for(int i = 0; i <= 9; i ++) op[i] = '0';
        int l = strlen(s);
        for(int i = 0; i < l; i ++){
            if(s[i] == 'L'){
                for(int i = 0; i <= 9; i ++){
                    if(op[i] == '0'){
                        op[i] = '1';
                        break;
                    }
                }
            }
            else if(s[i] == 'R'){
                for(int i = 9; i >= 0; i --){
                    if(op[i] == '0'){
                        op[i] = '1';
                        break;
                    }
                }
            }
            else{
                int t = s[i] - '0';
                op[t] = '0';
            }
        }
        for(int i = 0; i <= 9; i ++) printf("%c",op[i]);
        printf("\n");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章