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.
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);
}
}