A. Hotelier
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
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.
Input
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 nn 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 xx (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 (00, 11, …, 99) is given. Also, all the rooms are initially empty.
Output
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.
example
input
8
LLRL1RL1
output
1010000011
input
9
L0L0LLRR9
output
1100000010
AC代码
#include<cstdio>
#include <iostream>
#include<cstdlib>
#include <cstring>
const int maxn = 1e5+1;
int main()
{
int a[10]={0};
char c[maxn];
int n,l,r,ch;
scanf("%d",&n);
for(int i=0;i<=n;i++)//看下面那张图,第一个他会把scanf自带的\n录进去,所以是<=n
{
scanf("%c",&c[i]);
//std::cout<<c[i]<<std::endl;
if(c[i]=='L') {
//std::cout<<1;
for (l = 0; l < 10; l++)
if (a[l] == 0) {
a[l] = 1;
break;
}
}
if(c[i]=='R') {
//std::cout<<2;
for (r = 9; r > -1; r--)
if (a[r] == 0) {
a[r] = 1;
break;
}
}
if(c[i]>='0'&&c[i]<='9')
{
//std::cout<<3;
ch=(int)c[i]-'0';
a[ch]=0;
}
// std::cout<<"---- "<<i<<std::endl;
//for(int i=0;i<10;i++)
//printf("%d",a[i]);
//std::cout<<std::endl;
}
for(int i=0;i<10;i++)
printf("%d",a[i]);
return 0;
}