POJ 1250 Tanning Salon

传送门:http://poj.org/problem?id=1250

 题意:有n个位置,每个字母第一次出现代表客人的进来,第二次出现代表离开 , 统计流失了几个客户

我一直纠结的是Tanning是什么意思?题目为简单题,不要想到队列和栈,完全模拟即能过。只是有一句话不好理解,“Customers who leave without tanning always depart before customers who are currently tanning.”,意思是第二次扫描到相同的人的时候,是离开的人,没有床位离开的人总是depart处理的,即算上一个计数,其实就是如果顾客i来的时候没有空位了,无论他什么时候离开,都是算做没有住旅馆。


但是问题就来了,但数据为 1 ABCACB 1ABCBAC的时候应该输出2吧,但我AC的代码是输出1。。。。。这就尴尬了,数据弱么。。。。还是说给的数据都是占到床铺的人一定会在没有床睡的人之后离开?


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char weizhi[30];

int main()
{
   //freopen("in.txt","r",stdin);
   int n,flag,ans;
   string s;
   while(cin>>n&&n!=0){
    cin>>s;
    int i,j;
    ans = 0;
    memset(weizhi,'*',sizeof(weizhi));
    for(i=0;i<s.length();i++){
        flag = 0; //为0表示进入
        for(j=0;j<n;j++){ //离开后清空位置
            if(weizhi[j]==s[i]){
                weizhi[j]='*';
                flag = 1;
                break; //清空扫描下一个人
            }
    }
    //位置满了后,即超过n个,来的人直接走
    if(flag==0)
    for(j=0; j<n; j++){
        if(weizhi[j]=='*')
        {
            weizhi[j]=s[i];
            break;
        }
    }
    if(j==n)
        ans++;

    }
    if(ans==0)
        cout<<"All customers tanned successfully."<<endl;
    if(ans!=0)
        cout<<ans/2<<" customer(s) walked away."<<endl;

}
return 0;
}

感觉我AC的代码还是有点问题的,按照题目上那么说,看了下网上其他人的代码,对数据1 ABCACB 1 ABCBAC输出为2的有:

#include <cstdio>
#include<iostream>
#include<string>
#include <cstring>
#include<algorithm>
#define max(a,b) (a>b?a:b)
#define abs(a) ((a)>0?(a):-(a))
#define min(a,b) (a<b?a:b)
using namespace std;
int n;
bool vis[30];
string s;
int main()
{
	while(scanf("%d",&n),n)
	{
		memset(vis,0,sizeof(vis));
		cin>>s;
		int ans=0;
		for(int i=0;i<s.length();i++)
		{
			int t=s[i]-'A';
			if(!vis[t])
			{
				if(n<=0)
				{
					n--;
					ans++;
				}
				else
				{
					n--;
				}
			}
			else
			{
				n++;
			}
			vis[t]=!vis[t];
		}
		if(!ans)
			printf("All customers tanned successfully.
");
		else
			printf("%d customer(s) walked away.
",ans);
	}
	return 0;
}

网上还有好多都是2的,感觉人家的才是真正能AC的。。。。(ノಠ益ಠ)ノ彡┻━┻

原文地址:https://www.cnblogs.com/mingrigongchang/p/6246206.html