POJ1250解题报告

 
Tanning Salon
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5469 Accepted: 2991

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.
用JAVA写的话,算是偷了个巧,利用现成的容器:
import java.util.*;
public class Main {

	private ArrayList<String> bed=new ArrayList<String>();
	private ArrayList<String> leave=new ArrayList<String>();
	
	public Main()
	{
		Scanner scan=new Scanner(System.in);
		int num;
		while((num=Integer.parseInt(scan.next()))!=0)
		{
			String str=scan.next();
			int people=0;
			leave.clear();
			bed.clear();
			
			for(int i=0;i<str.length();i++)
			{
				//System.out.println(bed.size());
				
				if(bed.contains(String.valueOf(str.charAt(i))))
				{
					bed.remove(String.valueOf(str.charAt(i)));
				}
				else
				{
					if(bed.size()>=num)
					{
						if(leave.contains(String.valueOf(str.charAt(i))))
						{
							leave.remove(String.valueOf(str.charAt(i)));
						}
						else
						{
							leave.add(String.valueOf(str.charAt(i)));
							people++;
					    }
					}
					else
					{
						bed.add(String.valueOf(str.charAt(i)));
					}
				}
			}
			
			if(people==0)
			{
				System.out.println("All customers tanned successfully.");
			}
			else
			{
				System.out.println(people+" customer(s) walked away.");
			}
		}
	}
	public static void main(String[] args)
	{
		Main mainf=new Main();
	}
}
用C++写,自己实现容器。实现判断大小、判断是否含有以及加入和移除。运行Sample是对的,但是在POJ上却是RE。看来确实与C无缘啊!
#include<stdio.h>
#include<string.h>
//判断数组中是否含有本字符
int contains(char *str,int num,char ch)
{
    int result=0;
    int i;
    for(i=0;i<num;i++)
    {
            if(str[i]==ch)
            {
                          result=1;
                          break;
            }  
    } 
    return result;
} 
//算含有的数目
int getNum(char *str,int num)
{
    int result=0;
    int i;
    for(i=0;i<num;i++)
    {
         if(str[i]!='#')
         {
                        result++;
         }
    }
    return result;
} 
//重新设置数组
char* reset(char *str,int num)
{
    int len=getNum(str,num);
    int i;
    char *temp=(char*)malloc(len*sizeof(char));
    int index=-1;
    for(i=0;i<num;i++)
    {
            if(str[i]!='#')
            {
                  temp[++index]=str[i];              
            }
    }   
    //重新设置
    for(i=0;i<=index;i++)
    {
            str[i]=temp[i];
    } 
    for(i=index+1;i<num;i++)
    {
            str[i]='#';
    }
    free(temp);
    return str;
} 
//加入字符
char* addChar(char *str,int num,char ch)
{
     int index=getNum(str,num);
     if(index<num)
     {
          str[index]=ch;
     }
     return str;
} 
//移除字符
char* removeChar(char *str,int num,char ch)
{
     int i;
     for(i=0;i<num;i++)
     {
          if(str[i]==ch)
          {
              str[i]='#';
              break;
          }
     }
     str=reset(str,num);
     return str;
} 
int main()
{
 //床的数目 
 int num;
 scanf("%d",&num);
 while(num!=0)
 {
  char* s;
  scanf("%s",s);
  int len=strlen(s);
  //代表床的数组 
  char *bed=(char*)malloc(sizeof(char)*num);
  char *leaveC=(char*)malloc(sizeof(char)*len);
  
  //初始化 
  int i;
  for(i=0;i<num;i++)
  {
      bed[i]='#';        
  } 
  for(i=0;i<len;i++)
  {
      leaveC[i]='#';
  }
  int index=-1;
  int leave=0;
  
  for(i=0;i<len;i++)
  {
          int m;
          
          if(contains(bed,num,s[i])==1)
          {
              //如果已经含有,那么就是离开 
              bed=removeChar(bed,num,s[i]);
          }
          else
          {
              //否则则是加入
              if(getNum(bed,num)>=num)
              {
                  //判断是否是没有位置的人
                  if(contains(leaveC,len,s[i])==1)
                  {
                      leaveC=removeChar(leaveC,len,s[i]);
                  }
                  else
                  {
                      leaveC=addChar(leaveC,len,s[i]);
                      leave++;
                  }
              } 
              else
              {
                  bed=addChar(bed,num,s[i]);
              }
          }
  } 
  if(leave==0)
  {
          printf("All customers tanned successfully.");
  }
  else
  {
          printf("%d customer(s) walked away.",leave);
  }
  free(bed);
  free(leaveC);
  scanf("%d",&num);            
 }
 system("pause");
 return 0;    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值