C. Make a Square
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a positive integer nn, 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 nn to make from it the square of some positive integer or report that it is impossible.

An integer xx is the square of some positive integer if and only if x=y2x=y2 for some positive integer yy.

Input

The first line contains a single integer nn (1≤n≤2⋅1091≤n≤2⋅109). The number is given without leading zeroes.

Output

If it is impossible to make the square of some positive integer from nn, print -1. In the other case, print the minimal number of operations required to do it.

Examples
input

Copy
8314
output

Copy
2
input

Copy
625
output

Copy
0
input

Copy
333
output

Copy
-1
Note

In the first example we should delete from 83148314 the digits 33 and 44. After that 83148314 become equals to 8181, which is the square of the integer 99.

In the second example the given 625625 is the square of the integer 2525, so you should not delete anything.

In the third example it is impossible to make the square from 333333, so the answer is -1.

题意:判断x是否是一个完全平方数,如果不是,最少能删去几个数让他是完全平方数,如果有,输出删去的最少操作次数,若没有,输出-1;

思路:暴力枚举,从1开始,到sqrt(x)位置,看是否满足这个数是x的子串,如果是,存下它的操作次数与min进行比较,输出 min;

小知识点:to_string 函数,可以将一串数字转化成字符串

#include<iostream>
#include<string> using namespace std; int main()
{
string s1 = "2018.11";
int a1 = stoi(s1);
double c = stod(s1);
float d = stof(s1);
int a2 = a1 + ;
string b = to_string(a1);
b.append(" is a string"); cout << a1 <<"/"<<c<<"/"<<d<<"/"<<a2<<"/"<<b<< endl;
}

AC代码

#include <bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define mp make_pair
#define low_b lower_bound
#define up_b upper_bound
#define all(v) v.begin(), v.end() using namespace std; typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll; inline void Kazakhstan(){
ios_base :: sync_with_stdio();
cin.tie(), cout.tie();
} const int N = 2e9; int main(){
Kazakhstan();
string s; cin >> s;
int mn = INT_MAX;
for(int i = ; i * i <= N; i++){
string t = to_string(i * i);
int k = ;
bool w = ;
for(int j = ; j < s.size(); j++){
if(t[k] == s[j])k++;
if(k == t.size()){
w = ;
break;
}
}
if(w){
mn = min(mn, int(s.size() - t.size()));
}
}
if(mn == INT_MAX)cout << "-1";
else cout << mn;
}

Educational Codeforces Round 42 (Rated for Div. 2) C的更多相关文章

  1. Educational Codeforces Round 42 (Rated for Div. 2) E. Byteland, Berland and Disputed Cities

    http://codeforces.com/contest/962/problem/E E. Byteland, Berland and Disputed Cities time limit per ...

  2. Educational Codeforces Round 42 (Rated for Div. 2) D. Merge Equals

    http://codeforces.com/contest/962/problem/D D. Merge Equals time limit per test 2 seconds memory lim ...

  3. Educational Codeforces Round 42 (Rated for Div. 2)F - Simple Cycles Edges

    http://codeforces.com/contest/962/problem/F 求没有被两个及以上的简单环包含的边 解法:双联通求割顶,在bcc中看这是不是一个简单环,是的话把整个bcc的环加 ...

  4. Educational Codeforces Round 42 (Rated for Div. 2) B

    B. Students in Railway Carriage time limit per test 2 seconds memory limit per test 256 megabytes in ...

  5. Educational Codeforces Round 42 (Rated for Div. 2) A

    A. Equator time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  6. D. Merge Equals(from Educational Codeforces Round 42 (Rated for Div. 2))

    模拟题,运用强大的stl. #include <iostream> #include <map> #include <algorithm> #include < ...

  7. Educational Codeforces Round 42 (Rated for Div. 2)

    A. Equator(模拟) 找权值的中位数,直接模拟.. 代码写的好丑qwq.. #include<cstdio> #include<cstring> #include< ...

  8. C Make a Square Educational Codeforces Round 42 (Rated for Div. 2) (暴力枚举,字符串匹配)

    C. Make a Square time limit per test2 seconds memory limit per test256 megabytes inputstandard input ...

  9. D Merge Equals Educational Codeforces Round 42 (Rated for Div. 2) (STL )

    D. Merge Equals time limit per test2 seconds memory limit per test256 megabytes inputstandard input ...

随机推荐

  1. ELK+kafka日志处理

    此次使用kafka代替redis,elk集群搭建过程请参考:https://www.cnblogs.com/dmjx/p/9120474.html kafka名词解释: 1.话题(Topic):是特定 ...

  2. H5拍照、选择图片上传组件核心

    背景 前段时间项目重构,改成SSR的项目,但之前用的图片选择上传组件不支持SSR(server-side-render).遂进行了调研,发现很多的工具.但有的太大,有的使用麻烦,有的不满足使用需求.决 ...

  3. 类的特殊方法"__call__"详解

    1. __call__ 当执行对象名+括号时, 会自动执行类中的"__call__"方法, 怎么用? class A: def __init__(self, name): self ...

  4. 环形缓冲区实现类(Delphi)

    环形缓冲区的用途及原理可以去百度资料狠多的,这里就不介绍了.直接贴代码.代码分别用D7,XE2编译测试 源码下载 http://files.cnblogs.com/lwm8246/uCircleBuf ...

  5. python中的文件操作小结2

    ''' #-----------文件修改---------- f=open("test_1",'r',encoding="utf-8") f2=open(&qu ...

  6. C语言结构体篇 结构体

    在描述一个物体的属性的时候,单一的变量类型是无法完全描述完全的.所以有了灵活的结构体变量. 结构体变量从意义上来讲是不同数据类型的组合,从内存上来讲是在一个空间内进行不同的大小划分. 1.1 结构体类 ...

  7. C++ 基础 引用

    1.引用的本质 int b = 10; int &a = b; 等效于: int * const a = &b; 由于 a 是 const声明,所以一旦定义无法修改,所以要在定义时就要 ...

  8. DbVisualizer 解决中文乱码问题

    在SQL Commander中,sql语句中如果有中文,显示是‘口口口’. 解决办法如下: 在Tools->tool Properties->General->Appearance- ...

  9. SLB 7层负载均衡“HUNG”问题追查

    最近接到博客园的反馈,SLB 7层负载均衡的实例会不定期出现流量突跌的情况,突跌持续10s左右:同时,SLB自身监控也观察到了相同的现象: 针对该问题,我们进行了持续追查,最终定位到是nginx配置的 ...

  10. 以+scheduledTimerWithTimeInterval...的方式触发的timer,在滑动页面上的列表时,timer会暂定回调,为什么?如何解决?

    这里强调一点:在主线程中以+scheduledTimerWithTimeInterval...的方式触发的timer默认是运行在NSDefaultRunLoopMode模式下的,当滑动页面上的列表时, ...