POJ1250 ZOJ1405 UVALive2538 Tanning Salon【数据结构】

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8693   Accepted: 4650

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


Regionals 2002 >> North America - Mid-Central USA


问题链接POJ1250 ZOJ1405 UVALive2538 Tanning Salon

问题简述

  旅馆房间为n,用大写字母表示人的入住和离开,第一次出现的字母为入住,再次出现的字母为离开。房间有限,计算流失的客人数量。

问题分析

  这个问题的关键书采用什么样的数据结构表示。

  程序中的46行,结果需要除以2。

程序说明:(略)

参考链接:(略)

题记(略)


AC的C++语言程序如下:

/* POJ1250 ZOJ1405 UVALive2538 Tanning Salon */

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

const int N = 20;
const int N2 = 26;
char flag[N];
char s[N2 * 2];

int main()
{
    int n;
    while(~scanf("%d", &n) && n) {
        memset(flag, '$', sizeof(flag));

        scanf("%s", s);

        int ans = 0;
        for(int i = 0; s[i]; i++) {
            // 检查是否是离开
            int j;
            for(j = 0; j < n; j++)
                if(s[i] == flag[j]) {
                    flag[j] = '$';
                    break;
                }
            // 进入
            if(j == n) {
                for(j = 0; j < n; j++)
                    if(flag[j] == '$') {
                        flag[j] = s[i];
                        break;
                    }
                if(j == n)
                    ans++;
            }
        }

        if(ans == 0)
            printf("All customers tanned successfully.\n");
        else
            printf("%d customer(s) walked away.\n", ans / 2);
    }

    return 0;
}



猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/80872619