POJ-1250(模拟)
Tanning Salon
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 8213 | Accepted: 4384 |
Description
Tan Your Hide, Inc., owns several coin-operated tanning salons. Research has shown that if a customer arrives and there are no beds available, the customer will turn around and leave, thus costing the company a sale. Your task is to write a program that tells the company how many customers left without tanning.
Input
The input consists of data for one or more salons, followed by a line containing the number 0 that signals the end of the input. Data for each salon is a single line containing a positive integer, representing the number of tanning beds in the salon, followed by a space, followed by a sequence of uppercase letters. Letters in the sequence occur in pairs. The first occurrence indicates the arrival of a customer, the second indicates the departure of that same customer. No letter will occur in more than one pair. Customers who leave without tanning always depart before customers who are currently tanning. There are at most 20 beds per salon.
Output
For each salon, output a sentence telling how many customers, if any, walked away. Use the exact format shown below.
Sample Input
2 ABBAJJKZKZ 3 GACCBDDBAGEE 3 GACCBGDDBAEE 1 ABCBCA 0
Sample Output
All customers tanned successfully. 1 customer(s) walked away. All customers tanned successfully. 2 customer(s) walked away.
题意:有一个洗澡间,招待人洗澡。有n个位置。输入一个序列,字符第一次出现代表进来一个客人,如果没有位置了,客人就会走。第二次碰到相同的字符代表客人离开。问走了多少客人。
思路:模拟。注意,如果客人在等待,有人洗完澡了。等待的客人不会进去洗。就比如:1 ABCCAB,输出2.
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 using namespace std; 7 8 int n; 9 char s[100000]; 10 //记录对应人的状态。0代表没洗,1代表正在洗,2代表等待中 11 int cnt[30]; 12 13 int main() 14 { 15 while(scanf("%d", &n), n){ 16 scanf("%s", s); 17 int count = 0;//记录目前有几个人洗澡 18 int result = 0;//记录走了几个人 19 int len = strlen(s); 20 memset(cnt, 0,sizeof(cnt)); 21 22 for(int i = 0; i < len; i++){ 23 int index = s[i] - 'A'; 24 25 if(cnt[index] == 0){//如果人没洗 26 if(count >= n){//没有空位了 27 cnt[index] = 2;//记录等待序列 28 } 29 else{//如果有空位 30 cnt[index] = 1;//记录正在洗澡 31 count++; 32 } 33 } 34 35 else if(cnt[index] == 1){//如果正在洗澡 36 cnt[index] = 0;//洗完了 37 count--; 38 } 39 40 else if(cnt[index] == 2){//如果在等待 41 result++; 42 cnt[index] = 0;//走了 43 } 44 } 45 46 if(result == 0) 47 printf("All customers tanned successfully.\n"); 48 else 49 printf("%d customer(s) walked away.\n", result); 50 } 51 return 0; 52 }

浙公网安备 33010602011771号