Make a Square
传送门
题意:给你一个数字(没有前导零),要求你删除一些数字,使得这个数字(不能有前导零)的平方根为整数。没有则输出-1.
思路:数字不大,二进制枚举每一种组合情况。
二进制枚举实现传送门
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <functional>
#define inf 1000000000
using namespace std ;
typedef long long ll;
const int MAXN=1e9 +10 ;
const int MAX=1e2 +10 ;
const double eps=1e-6 ;
string s;
int vis[MAX];
ll getnum(string t){
int cnt=0 ,len=t.size()-1 ;
ll res=0 ;
while (len+1 ){
double temp=1 ;
for (int i=1 ;i<=cnt;i++) temp*=10 ;
res+=temp*(t[len]-'0' );
len--;cnt++;
}
return res;
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("in.txt" ,"r" ,stdin);
#endif
cin >>s;
int n=s.size();
string ans;
for (int i=0 ;i<(1 <<n);i++){
memset (vis,0 ,sizeof (vis));
for (int j=0 ;j<n;j++){
if (i&(1 <<j))
vis[j]=1 ;
}
string temp;
temp.clear();
for (int j=0 ;j<n;j++){
if (vis[j]) continue ;
temp+=s[j];
}
if (temp[0 ]=='0' ) continue ;
int t=getnum(temp);
if (!t) continue ;
if ((ll)sqrt (t)*(ll)sqrt (t)==t){
if (ans.size()==0 ) ans=temp;
else if (temp.size()>ans.size())
ans=temp;
}
}
if (ans.size())
cout <<s.size()-ans.size()<<endl;
else
cout <<-1 <<endl;
return 0 ;
}