2021-8-20 Make a Power of Two

Posted 洛水天iriya

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021-8-20 Make a Power of Two相关的知识,希望对你有一定的参考价值。

难度 1300

题目 Leetcode:

D. Make a Power of Two
time limit per test
1 second
memory limit per test
256 megabytes

You are given an integer nn. In 11 move, you can do one of the following actions:

  • erase any digit of the number (it\'s acceptable that the number before the operation has exactly one digit and after the operation, it is "empty");
  • add one digit to the right.

The actions may be performed in any order any number of times.

Note that if, after deleting some digit from a number, it will contain leading zeroes, they will not be deleted. E.g. if you delete from the number 301301 the digit 33, the result is the number 0101 (not 11).

You need to perform the minimum number of actions to make the number any power of 22 (i.e. there\'s an integer kk (k0k≥0) such that the resulting number is equal to 2k2k). The resulting number must not have leading zeroes.

E.g. consider n=1052n=1052. The answer is equal to 22. First, let\'s add to the right one digit 44 (the result will be 1052410524). Then let\'s erase the digit 55, so the result will be 10241024 which is a power of 22.

E.g. consider n=8888n=8888. The answer is equal to 33. Let\'s erase any of the digits 88 three times. The result will be 88 which is a power of 22.

Input

The first line contains one integer tt (1t1041≤t≤104) — the number of test cases. Then tt test cases follow.

Each test case consists of one line containing one integer nn (1n1091≤n≤109).

Output

For each test case, output in a separate line one integer mm — the minimum number of moves to transform the number into any power of 22.

 

题目解析

本题大意就是对给出的数字n进行变换,可以去掉任意一位,或者在n的最右侧加一个数字,操作若干次后,使得当前数字n变成2的次幂

根据题目给出的n的范围,这一题我们可以考虑先打表,然后用输入的数字和表里的数字逐一比较,对字符串n的子序列去匹配那些幂次方字符串。

解析完毕,以下是参考代码

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<sstream>
 4 #include<vector>
 5 #include<string>
 6 using namespace std;
 7 typedef long long ll;
 8 vector<string>m;
 9 int judge(string a, string b)
10 {
11     int ans = 0, i = 0, j = 0;
12     int alen = a.size();
13     int blen = b.size();
14     for (; i < alen && j < blen;)
15     {
16         if (a[i] == b[j])
17         {
18             ans++;
19             i++;
20             j++;
21         }
22         else j++;
23     }
24     return alen - i + blen - ans;
25 }
26 int main()
27 {
28     ll x = 1;
29     string temp;
30     for (int i = 0; i < 60; i++)
31     {
32         stringstream sstream;
33         sstream << x;
34         sstream >> temp;
35         m.push_back(temp);
36         x <<= 1;
37     }
38     int t; cin >> t;
39     while (t--)
40     {
41         string s; cin >> s;
42         int ans = 100, len = m.size();
43         for (int i = 0; i < len; i++)ans = min(ans, judge(m[i], s));
44         cout << ans << "\\n";
45     }
46     return 0;
47 }

 

以上是关于2021-8-20 Make a Power of Two的主要内容,如果未能解决你的问题,请参考以下文章

Make a Power of Two(D)

Codeforces1560 D. Make a Power of Two(思维+暴力)

D. Make a Power of Two(DIV3)(预处理+模拟)

D. Make a Power of Two(DIV3)(预处理+模拟)

D. Make a Power of Two(DIV3)(预处理+模拟)

231. Power of Two