好久没练习了,如今…也已经大三了,算法的路,基本也完蛋了。。。
但还是要练习的,毕竟还有CCF,还有机考,等到下学期还有个蓝桥杯,也许还有其他比赛什么的,好像有点功利…但继续吧,毕竟就像攀岩一样,山就在那。emm,上次蓝桥杯也突击几天混了个省二,从现在开始准备CCF!
首先是不久前的CF739 div3的D题Make a Power of Two
咋看真的无从下手,其实就是个字符串匹配问题,增和删操作其实就操作步骤次数来说本质上是一样的,寻找是否和2的倍数相等,情况很多,我们可以先初始化一个数组,里面全放的都是2的指数。用long long吧,毕竟数很大,long long的最大值是9223372036854775807,有19位数,这里用个<1e18作为限制条件,用to_string把这个数转为字符串,数组其实还是不怎么方便因为我们不知道有多少个数,(有60个),就用过vector就行,然后就是比较相同的字符(比在书上学过的找出最长子串还简单点),u1s1 for(auto)还挺好用的,下面是代码:
#include<iostream>
#include<string.h>
#include<cmath>
#include<sstream>
#include<algorithm>
#include<vector>
using namespace std;
vector<string> ts;
int main()
{
ios::sync_with_stdio(false);
for (long long i = 1; i < 1e18; i*=2)
{
ts.push_back(to_string(i));
//cout << i << endl;
}
cout << ts.size() << endl;
int T;
cin >> T;
string s;
while (T--)
{
cin >> s;
int p1 = 0, p2 = 0,r=2e6;
for (auto p : ts)
{
p2 = 0;
for (int i = 0; i < s.size(); i++)
if (s[i] == p[p2])
p2++;
r = min(r, (int)(p.size() - p2 + s.size() - p2));
}
cout << r << endl;
}
}