Topcoder open 2015 Round 1A 250 Similars 枚举 + 状压

题意:问你A-B之间 问你选两个数字 使得 这两个数字 相同 数字 个数最多 的 相同数字个数。

解题思路:枚举A-B之间所有的数,把数分解成10进制hash状态压缩,然后再把所有压缩后的值 N^2枚举找出最大值。

解题代码:

  1 // BEGIN CUT HERE
  2 /*
  3 
  4 */
  5 // END CUT HERE
  6 #line 7 "Similars.cpp"
  7 #include <cstdlib>
  8 #include <cctype>
  9 #include <cstring>
 10 #include <cstdio>
 11 #include <cmath>
 12 #include <algorithm>
 13 #include <vector>
 14 #include <string>
 15 #include <iostream>
 16 #include <sstream>
 17 #include <map>
 18 #include <set>
 19 #include <queue>
 20 #include <stack>
 21 #include <fstream>
 22 #include <numeric>
 23 #include <iomanip>
 24 #include <bitset>
 25 #include <list>
 26 #include <stdexcept>
 27 #include <functional>
 28 #include <utility>
 29 #include <ctime>
 30 using namespace std;
 31 
 32 #define PB push_back
 33 #define MP make_pair
 34 
 35 #define REP(i,n) for(i=0;i<(n);++i)
 36 #define FOR(i,l,h) for(i=(l);i<=(h);++i)
 37 #define FORD(i,h,l) for(i=(h);i>=(l);--i)
 38 
 39 typedef vector<int> VI;
 40 typedef vector<string> VS;
 41 typedef vector<double> VD;
 42 typedef long long LL;
 43 typedef pair<int,int> PII;
 44 
 45 map <LL,int > mp;
 46 map <LL,int >::iterator ti,tj;
 47 int hs[11];
 48 
 49 int pp(LL a,LL b)
 50 {
 51    int ans = 0 ; 
 52    while(a && b)
 53    {
 54        if(a&b&1 == 1 )
 55        {
 56           ans ++ ; 
 57        }
 58        a = a >> 1; 
 59        b = b >> 1; 
 60    }
 61    return ans;
 62 }
 63 void change(int L)
 64 {
 65   memset(hs,0,sizeof(hs));
 66   int t = 0 ; 
 67   while(L)
 68   {
 69     hs[L % 10] = 1; 
 70     L/= 10 ; 
 71   }
 72   LL tmp = 0 ; 
 73   for(int i = 0 ;i <= 9;i ++)
 74   {
 75      if(hs[i])
 76          tmp += (1<<i);
 77   }
 78   //printf("%lld
",tmp);
 79   mp[tmp] ++;
 80 }
 81 class Similars
 82 {
 83         public:
 84         int maxsim(int L, int R)
 85         {
 86             mp.clear();
 87             int ans = 0 ;
 88              for(int i = L ;i <= R ;i ++)
 89              {
 90                 change(i);
 91              }
 92              for(ti = mp.begin();ti != mp.end();ti++)
 93                  for(tj = ti ;tj != mp.end();tj++)
 94                  {
 95                      if(ti == tj)
 96                      {
 97                         if(ti->second >= 2)
 98                         {
 99                            ans = max(ans,pp(ti->first,tj->first));
100                         }
101                      }else{
102                            ans = max(ans,pp(ti->first,tj->first));
103                      }
104                  }
105              return ans ;
106         }
107         
108 // BEGIN CUT HERE
109     public:
110     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
111     private:
112     template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '"' << *iter << "","; os << " }"; return os.str(); }
113     void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "	Expected: "" << Expected << '"' << endl; cerr << "	Received: "" << Received << '"' << endl; } }
114     void test_case_0() { int Arg0 = 1; int Arg1 = 10; int Arg2 = 1; verify_case(0, Arg2, maxsim(Arg0, Arg1)); }
115     void test_case_1() { int Arg0 = 1; int Arg1 = 99; int Arg2 = 2; verify_case(1, Arg2, maxsim(Arg0, Arg1)); }
116     void test_case_2() { int Arg0 = 99; int Arg1 = 100; int Arg2 = 0; verify_case(2, Arg2, maxsim(Arg0, Arg1)); }
117     void test_case_3() { int Arg0 = 1000; int Arg1 = 1010; int Arg2 = 2; verify_case(3, Arg2, maxsim(Arg0, Arg1)); }
118     void test_case_4() { int Arg0 = 444; int Arg1 = 454; int Arg2 = 2; verify_case(4, Arg2, maxsim(Arg0, Arg1)); }
119 
120 // END CUT HERE
121 
122 };
123 
124 // BEGIN CUT HERE
125 int main()
126 {
127         Similars ___test;
128         ___test.run_test(-1);
129         return 0;
130 }
131 // END CUT HERE
View Code
原文地址:https://www.cnblogs.com/zyue/p/4422719.html