思路:
算出n与2的每个幂次方的最大匹配数即可,最好把2的0到61的幂全部匹配一次,否则可能会wa4.
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<math.h>
#include<vector>
using namespace std;
#define ll long long
ll a[64];
ll b[100];
ll c[100];
int arr[100][100];
int main()
{
for(int i = 0; i <= 61; i++)
{
a[i] = pow(2,i);
}
int t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
int cnt = 0;
ll pos = n;
while(n)
{
b[++cnt] = n%10;
n/=10;
}
for(int i = 1; i <= cnt/2; i++)
{
swap(b[i], b[cnt-i+1]);
}
ll ans = 0x3f3f3f3f;
for(int i = 0; i <= 61; i++)
{
ll now = a[i];
int num = 0;
while(now)
{
c[++num] = now%10;
now/=10;
}
for(int j = 1; j <= num/2; j++)
{
swap(c[j], c[num-j+1]);
}
ll sum = 0,f = 0;
for(int st = 1; st <= cnt; st++)
{
ll z = 0;
int flag = st;
if(b[st] == c[1])
{
for(int j = 1; j <= num; j++)
{
while(b[flag] != c[j])
{
flag++;
if(flag > cnt)break;
}
if(flag > cnt)
{
break;
}
else
{
flag++;
z++;
}
}
}
sum = max(sum,z);
ans = min(ans, (cnt-sum)+num-sum);
}
// if(ans == 3)
// {
// cout<< a[i]<<"()"<<endl;
// }
}
cout<<ans<<endl;
}
}