You are given a positive integer 𝑛, written without leading zeroes (for example, the number 04 is incorrect).
In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros.
Determine the minimum number of operations that you need to consistently apply to the given integer 𝑛 to make from it the square of some positive integer or report that it is impossible.
An integer 𝑥 is the square of some positive integer if and only if 𝑥=𝑦2 for some positive integer 𝑦.
Input The first line contains a single integer 𝑛 (1≤𝑛≤2⋅109). The number is given without leading zeroes.
Output If it is impossible to make the square of some positive integer from 𝑛, print -1. In the other case, print the minimal number of operations required to do it.
Examples inputCopy 8314 outputCopy 2 inputCopy 625 outputCopy 0 inputCopy 333 outputCopy -1 Note In the first example we should delete from 8314 the digits 3 and 4. After that 8314 become equals to 81, which is the square of the integer 9.
In the second example the given 625 is the square of the integer 25, so you should not delete anything.
In the third example it is impossible to make the square from 333, so the answer is -1.
题意: 求一个数最少删掉多少位变成完全平方数。
思路: 模拟
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include <set> #include <map> #include <queue> using namespace std; typedef long long ll; const int maxn = 3e5 + 7; vector<int>digit; int cmp(int x,int y) { //用x匹配y vector<int>d1,d2; while(x) { d1.push_back(x % 10); x /= 10; } while(y) { d2.push_back(y % 10); y /= 10; } int len1 = d1.size(),len2 = d2.size(); int pos = 0; for(int i = 0;i < len1;i++) { if(d1[i] == d2[pos]) { pos++; if(pos == len2) break; } } if(pos == len2) return len1 - len2; return -1; } int main() { int n;scanf("%d",&n); for(int i = 1;i <= 100000;i++) { if(1ll * i * i > n) break; digit.push_back(i * i); } int ans = 0; for(int i = digit.size() - 1;i >= 0;i--) { int x = digit[i]; int num = cmp(n,x); if(num != -1) { printf("%d\n",num); return 0; } } printf("-1\n"); return 0; }