北大ACM(POJ1013-Counterfeit Dollar)
Question:http://poj.org/problem?id=1013
问题点:排除+验证。
1 Memory: 244K Time: 16MS 2 Language: C++ Result: Accepted 3 4 #include <iostream> 5 #include <string> 6 using namespace std; 7 int flag[12];//0:未出现 1:even 2:up/down 8 string str[9]; 9 int main() 10 { 11 int k,weight; 12 cin>>k; 13 while(k-->0) 14 { 15 weight=0; 16 memset(flag,0,sizeof(flag)); 17 for(int i=0;i<9;i++){ 18 cin>>str[i]; 19 if(i==2||i==5||i==8) 20 { 21 if(str[i]=="even") 22 { 23 for(int j=0;j<str[i-1].size();j++) 24 { 25 flag[str[i-1].at(j)-'A']=1; 26 flag[str[i-2].at(j)-'A']=1; 27 } 28 }else{//up down 29 for(int j=0;j<str[i-1].size();j++) 30 { 31 if(flag[str[i-1].at(j)-'A']!=1) 32 { 33 flag[str[i-1].at(j)-'A']+=2; 34 weight=weight>flag[str[i-1].at(j)-'A']?weight:flag[str[i-1].at(j)-'A']; 35 } 36 if(flag[str[i-2].at(j)-'A']!=1) 37 { 38 flag[str[i-2].at(j)-'A']+=2; 39 weight=weight>flag[str[i-2].at(j)-'A']?weight:flag[str[i-2].at(j)-'A']; 40 } 41 } 42 } 43 } 44 } 45 for(int i=0;i<12;i++) 46 { 47 char c=i+'A'; 48 string s=""; 49 if(flag[i]==weight) 50 { 51 for(int j=0;j<3;j++) 52 { 53 string t=""; 54 if(str[j*3+2]=="up") 55 { 56 if(str[j*3].find(c)!=string::npos) t="heavy."; 57 else if(str[j*3+1].find(c)!=string::npos) t="light."; 58 }else if(str[j*3+2]=="down") 59 { 60 if(str[j*3].find(c)!=string::npos) t="light."; 61 else if(str[j*3+1].find(c)!=string::npos) t="heavy."; 62 } 63 if(s==""){ 64 s=t; 65 }else if(t!="" && s!=t){ 66 s=""; 67 break; 68 } 69 } 70 if(s!="") cout<<c<<" is the counterfeit coin and it is "<<s<<endl; 71 } 72 } 73 } 74 75 return 0; 76 }