POJ 1250 Tanning Salon(我的水题之路——字母表数组标记)

Tanning Salon
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5475   Accepted: 2994

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.

Source

Mid-Central USA 2002

题意是说,有一个沙龙,里面有N个床,会有很多客人过来住,如果当一个客人过来,但是沙龙的床已经满了,那么这个客人就要离开。题目将给出床的数量N,和一个字符队列,表示客人们来的顺序。每个客人的均用一个字符表示,每个客人的字符会在队列了出现了两次,第一次表示客人到来,占用了一个床,第二次出现表示客人离开,将床位让开。

解法是用一个字符数组对每个字符的状态进行标记,如果这个人出现了,且床位有空,则让其字符标记为1,请床位书fullnum加1,如果床位已满,则让字符数组标记为2,且离开人数量加一。如果这个人离开了,就让fullnum减一,且该字母标记为0.将整个队列标记一次之后就可以得到结果。

这个方法的适用于以下情况:
1)所有的字母仅出现一次
2)每个字母有多个状态

代码(1AC):
#include <cstdio>
#include <cstdlib>
#include <cstring>

char array[50];
int words[50];
int N;

int main(void){
    int i, j;
    int len, leftnum;
    int fullnum;
    while (scanf("%d", &N) , N != 0){
        scanf(" %s", array);
        memset(words, 0, sizeof(words));
        len = strlen(array);
        leftnum = fullnum = 0;
        for (i = 0; i < len; i ++){
            if (words[array[i] - '0'] == 0){
                if (fullnum < N){
                    words[array[i] - '0'] = 1;
                    fullnum++;
                }
                else{
                    words[array[i] - '0'] = 2;
                    leftnum++;
                }
            }
            else if (words[array[i] - '0'] == 1){
                fullnum --;
                words[array[i] - '0'] = 0;
            }
            else if(words[array[i] - '0'] == 2){
                    words[array[i] - '0'] = 0;
            }
        }
        if (leftnum == 0){
            printf("All customers tanned successfully.\n");
        }
        else{
            printf("%d customer(s) walked away.\n", leftnum);
        }
    }
    return 0;
}


你可能感兴趣的:(POJ 1250 Tanning Salon(我的水题之路——字母表数组标记))