标签:
Problem Statement |
|||||||||||||
Given two positive integers x and y, their similarity S(x, y) is defined as follows: To compute S(x, y) we count all d between 0 and 9, inclusive, such that both x and y contain the digit d when written in base 10 (without any leading zeros). For example, S(1123, 220181) = 2 since both numbers contain the digit 1 and both contain the digit 2. You are given two ints L and R that define a range. Find two distinct integers in this range that have the largest similarity. Formally, return the maximum of S(a, b) over all a, b such thatL <= a < b <= R. |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
Constraints |
|||||||||||||
- |
R will be between 2 and 100,000, inclusive. |
||||||||||||
- |
L will be between 1 and R - 1, inclusive. |
||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
|||||||||||||
4) | |||||||||||||
|
直接把每个数根据数位状压成一个数,共2^10 统计即可
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cctype> #include<ctime> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;i>=0;i--) #define MEM(a) memset(a,0,sizeof(a)); #define INF (2139062143) #define F (100000007) #define MAXN (100000) typedef long long ll; int f[MAXN]={0},g[MAXN]={0}; int p2[20]={1,2,4,8,16,32,64,128,256,512,1024}; class Similars { public: int maxsim(int L, int R) { int ans=0; p2[0]=1; For(i,10) p2[i]=p2[i-1]*2; MEM(f) Fork(i,L,R) { int p=i,t=0; while (p) { t=t|p2[p%10]; p/=10; } f[t]++; } Rep(i,p2[10]) { int q=0; Rep(j,10) if (i&p2[j]) q++; g[i]=q; } Rep(i,p2[10]) Rep(j,p2[10]) { if ( (i==j && f[i]>=2) || (i!=j && f[i] && f[j] )) { ans=max(ans,g[i&j]); } } return ans; } };
标签:
原文地址:http://blog.csdn.net/nike0good/article/details/45010327