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. 7-3 python操作excel

    1.写excel 写入特定单元格数据 # .导入xlwt模块 # .新建一个excel # .添加一个sheet页 # .往指定的单元格中写入数据 # .保存excel import xlwt boo ...

  2. Centos7上搭建activemq集群和zookeeper集群

    Zookeeper集群的搭建 1.环境准备 Zookeeper版本:3.4.10. 三台服务器: IP 端口 通信端口 10.233.17.6 2181 2888,3888 10.233.17.7 2 ...

  3. 【Effective C++ 读书笔记】条款03: 尽量使用 const

    关键字const多才多艺,变化多端却不高深莫测. const 修饰指针 面对指针, 你可以指出 指针自身.指针所指物.或者两者都不是 const. 如果关键字 const 出现在星号左边,表示被指物是 ...

  4. SpringCloud框架搭建+实际例子+讲解+系列五

    (4)服务消费者,面向前端或者用户的服务 本模块涉及到很多知识点:比如Swagger的应用,SpringCloud断路器的使用,服务API的检查.token的校验,feign消费者的使用.大致代码框架 ...

  5. 笔记-python-standard library-16.3 time

    笔记-python-standard library-16.3 time 1.      time 1.1.    开始 time模块中时间表现的格式主要有三种: timestamp时间戳,时间戳表示 ...

  6. python, 面向对象编程Object Oriented Programming(OOP)

    把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机程序视为一系列的命令集合,即一组函数的顺序执行.为了简化程序设计,面向过程把函数继续切分为子函数,即把大块函数 ...

  7. cocos2d-x 3.0的入门程序:helloworld

    看过了这么多不同方向的应用,发现很多程序入门都是helloworldhelloworld是所有程序员的绝对初恋 先看一下程序的运行结果吧 然后就是他的工程代码 工程的目录有两个 Classes:程序中 ...

  8. Ensure that you have installed a JDK (not just a JRE) and configured your JAVA_HOME system variable

    编译的时候出现这个,我从svn download下来的代码,运行就报这个错. 当时我还无知的大吼,怎么可能没有配置java_home, 运行了java -version 都显示出来1.8了. 后来,让 ...

  9. luogu2221 [HAOI2012]高速公路

    和sdoi的相关分析很像qwq,推柿子然后线段树搞搞 #include <iostream> #include <cstdio> using namespace std; ty ...

  10. Jenkins拾遗--第四篇-适当的让构建失败

    问题的引出: 有一段我们的前端构建总会现git上分支名称中的版本号和工程里的版本号不一致的问题:这样会导致构一个问题:构建后的产品名称叫做1.1,但是进入app的关于页面,看到的版本还是1.0.这会让 ...