POJ-1013-Counterfeit Dollar -暴力水题


http://poj.org/problem?id=1013

这代码写得太挫太长了。。。。


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;  
struct node
{
	int len;
	char ip1[105];
	char ip2[105];
	char ip3[105]; 
	
};
node input[5];
bool cmp(node a,node b)
{
	return a.len<b.len;
}
char tm[105];
char nm[105];
char result[105]; 
int value[15];
int vis[15];
int vis2[15];
set<int>::iterator it;
set<int> ans;
set<int> ans2;
int main()
{  
	int i,j;
	int t;
	cin>>t;
	while(t--)
	{
		memset(value,0,sizeof(value));
		memset(vis2,0,sizeof(vis2));
		memset(vis,0,sizeof(vis));
		ans.clear();
		ans2.clear();
		
		for (i=1;i<=3;i++)
		{
			scanf("%s %s %s",input[i].ip1,input[i].ip2,input[i].ip3);
			input[i].len=strlen(input[i].ip1);
		}
	 	sort(input+1,input+1+3,cmp);  //从长度最短的开始选可保证尽可能得到答案(set里面元素尽可能少),但是本题说明了必然有答案,所以排序不排序都不影响结果
		
		for (i=1;i<=3;i++)
		{
			int n=input[i].len;
			strcpy(tm,input[i].ip1);
			strcpy(nm,input[i].ip2);
			strcpy(result,input[i].ip3);
			
			if (strcmp(result,"even")==0)
			{
				for (j=0;j<n;j++) 
					vis2[nm[j]-'A'+1]=vis2[tm[j]-'A'+1]=vis[nm[j]-'A'+1]=vis[tm[j]-'A'+1]=1;
			}
			else
				if (strcmp(result,"up")==0) 
				{
					//if ans is light
					if (ans.size()==0)
					{	
						for (j=0;j<n;j++) 
							ans.insert(nm[j]-'A'+1); //insert all possible value; (must contain the ans)
					}
					else
					{
						for (it=ans.begin();it!=ans.end();it++)
						{
							if (std::find(nm,nm+n,(*it+'A'-1))==tm+n ) //cannot find
								vis[*it]=1;  // mark to delete 
							else
								value[*it]++;
						}  
					}
					for (j=0;j<n;j++) 
						vis[tm[j]-'A'+1]=1;  //those impossible ans
					
					//if ans is heavy
					if (ans2.size()==0)
					{	
						for (j=0;j<n;j++) 
							ans2.insert(tm[j]-'A'+1); //insert all possible value; (must contain the ans)
					}
					else
					{
						for (it=ans2.begin();it!=ans2.end();it++)
						{
							if (std::find(tm,tm+n,(*it+'A'-1))==tm+n )   //cannot find
								vis2[*it]=1;  // mark to delete 
							else
								value[*it]++;   
						}  
					}
					for (j=0;j<n;j++) 
						vis2[nm[j]-'A'+1]=1;     //those impossible ans
					
					
				} 
				else
					if (strcmp(result,"down")==0) 
					{
						//if ans is light
						if (ans.size()==0)
						{	
							for (j=0;j<n;j++) 
								ans.insert(tm[j]-'A'+1); 
						}
						else
						{
							for (it=ans.begin();it!=ans.end();it++)
							{ 
								if (std::find(tm,tm+n,(*it+'A'-1))==tm+n )  
									vis[*it]=1;    
								else
									value[*it]++;
							}  
						}
						for (j=0;j<n;j++) 
							vis[nm[j]-'A'+1]=1;
						//if ans is heavy
						if (ans2.size()==0)
						{	
							for (j=0;j<n;j++) 
								ans2.insert(nm[j]-'A'+1);  
						}
						else
						{
							for (it=ans2.begin();it!=ans2.end();it++)
							{
								if (std::find(nm,nm+n,(*it+'A'-1))==tm+n )  
									vis2[*it]=1;  
								else
									value[*it]++;
							}  
						}  
						for (j=0;j<n;j++) 
							vis2[tm[j]-'A'+1]=1;
					}
		}
		
		
		for (j=1;j<=12;j++)
		{
			if (vis[j])
				ans.erase(j);
			if (vis2[j])
				ans2.erase(j);
		}   
		int op=-1;
		if (ans.size()==1&&ans2.size()==1)     // special condition
		{
			int t1=*ans.begin();
			int t2=*ans2.begin();
			if (value[t1]>value[t2])
				op=1;
			else
				op=2;
			
			if (op!=1)
				printf("%c is the counterfeit coin and it is heavy.\n",(*ans2.begin()+'A'-1));
			else 
				printf("%c is the counterfeit coin and it is light.\n",(*ans.begin()+'A'-1)); 
			
			continue;
		}
		
		if (ans.size()!=1)
			printf("%c is the counterfeit coin and it is heavy.\n",(*ans2.begin()+'A'-1));
		else 
			printf("%c is the counterfeit coin and it is light.\n",(*ans.begin()+'A'-1));
		
		
}
return 0;

} 


你可能感兴趣的:(POJ-1013-Counterfeit Dollar -暴力水题)