ZOJ-1405 位置占用

1405:沙龙座位有限。顾客来时如每座会走掉。给出一个顾客出入串,第一次出现为进入,第二次出现为离开。问一共有多少因为没座直接离开。

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.


因为离开是无序的,因此用map来存储,查找便利。
map<char,int> m 当某索引不存在时,如m['A'],会自动插入该项,且值为0.


#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#include<map>

int seat[30];

int main()
{
map<char,int> m;
int n;
char list[82];
int len;
int cur;
int left;

while(1)
{
m.clear();
left=0;
cur=0;

cin>>n;
if(n==0)
break;

cin>>list;
len=strlen(list);
for(int i=0;i<len;i++)
{
if(m[list[i]]==0)
{
if(n==cur)
{
left++;
m[list[i]]=-1; //-1代表无座离开
}
else
{
cur++;
m[list[i]]=1; //1代表位置占用
}
}
else if(m[list[i]]==1)
{
cur--;
m[list[i]]=0; //0代表位置空闲
}

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

}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值