You are given an integer nn from 11 to 10181018 without leading zeroes.

In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes.

What is the minimum number of moves you have to make to obtain a number that is divisible by 2525? Print -1 if it is impossible to obtain a number that is divisible by 2525.

Input

The first line contains an integer nn (1≤n≤10181≤n≤1018). It is guaranteed that the first (left) digit of the number nn is not a zero.

Output

If it is impossible to obtain a number that is divisible by 2525, print -1. Otherwise print the minimum number of moves required to obtain such number.

Note that you can swap only adjacent digits in the given number.

Examples

Input
5071
Output
4
Input
705
Output
1
Input
1241367
Output
-1

Note

In the first example one of the possible sequences of moves is 5071 →→ 5701 →→ 7501 →→ 7510 →→ 7150.

题意:

给你一个整数,你可以交换这个整数中的相邻的数位,但交换后的数不能有前导0,请你输出最小的交换次数,使之可以被25整除。

思路:

我们应该知道,>1且可以被25整除的数,后两位一定是00,25,50,75,这四个整数。

那么我们先通过记录这个数n的每一位数的数次,来判断是否是输出-1,

如果出现过2次0,或者1次2一次5,或者一次5一次0,一次7一次5都可以确定一定可以交换出想要的数。否则就是-1.

确定一定可以交换出想要的数后,我们就要想法记录出最小的移动次数。

我们可以知道,如果我们想要最后两位是50,那么我们要把一个数字0移动到最后一位,我们想移动次数最小,那么一定是选择移动最靠右边的那个0,5一定是最靠右的5.其他借此类推。

我们怎么记录移动的次数呢?

我们用STL中的string类型中的rfind函数(从右开始查找)

然后如果想找的字符的下标是i,那么需要移动len-i-1次,len是数组的当前长度。

然后把这个字符从string中删除掉,(消除对下一个字符的影响)。

然后另外一个字符同理。

我们要注意移动的结果可能会有前导0的,那么我们只需要记录有多少个前导0,就可以通过移动多少次来把签到0后面的字符移动到第一位。

取多个值中的最小值就是ans。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
string s,t;
int cnt[maxn];
int len;
int check(string s,char a,char b)
{
int res=;
if(s.rfind(a)!=-)
{
int id=s.rfind(a);
res+=len-id-;
s.erase(s.rfind(a),);
if(s.rfind(b)!=-)
{
id=s.rfind(b);
res+=len-id-;
s.erase(s.rfind(b),);
while(s[]=='')
{
res++;
s.erase(,);
}
return res;
}else
{
return inf;
}
}else
{
return inf;
}
}
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
//gbtb;
cin>>s;
t=s;
len=s.length();
rep(i,,len)
{
cnt[s[i]]++;
}
if((cnt['']>=)||(cnt['']>=&&cnt['']>=)||(cnt['']>=&&cnt['']>=)||(cnt['']>=&&cnt['']>=))
{
int ans=inf;
ans=min(ans,check(t,'',''));
ans=min(ans,check(t,'',''));
ans=min(ans,check(t,'',''));
ans=min(ans,check(t,'',''));
cout<<ans<<endl;
}else
{
cout<<-<<endl;
}
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

Divisibility by 25 CodeForces - 988E (技巧的暴力)的更多相关文章

  1. Divisibility by 25 CodeForces - 988E

    You are given an integer nn from 11 to 10181018 without leading zeroes. In one move you can swap any ...

  2. Divisibility by 25 CodeForces - 988E(模拟)

    遇见模拟题 有两种做法 例如这题: 1.直接去算次数(统计哪个数在第几位,然后去运算) 2.模拟操作 贴一个别人的代码...https://blog.csdn.net/weixin_39453270/ ...

  3. Codeforces Round #486 (Div. 3) E. Divisibility by 25

    Codeforces Round #486 (Div. 3) E. Divisibility by 25 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  4. CF 988E Divisibility by 25 思维 第十二

    Divisibility by 25 time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. Codeforces A. Playlist(暴力剪枝)

    题目描述: Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  6. 你应该了解的25个JS技巧

    目录 1. 类型检查小工具 2. 检查是否为空 3. 获取列表最后一项 4. 带有范围的随机数生成器 5. 随机 ID 生成器 6. 创建一个范围内的数字 7. 格式化 JSON 字符串,string ...

  7. Codeforces Round #486 (Div. 3)988E. Divisibility by 25技巧暴力||更暴力的分类

    传送门 题意:给定一个数,可以对其做交换相邻两个数字的操作.问最少要操作几步,使得可以被25整除. 思路:问题可以转化为,要做几次交换,使得末尾两个数为00或25,50,75: 自己一开始就是先for ...

  8. Codeforces 988E. Divisibility by 25

    解题思路: 只有尾数为25,50,75,00的数才可能是25的倍数. 对字符串做4次处理,以25为例. a. 将字符串中的最后一个5移到最后一位.计算交换次数.(如果没有找到5,则不可能凑出25,考虑 ...

  9. [codeforces 200 A Cinema]暴力,优化

    题意大致是这样的:有一个有n行.每行m个格子的矩形,每次往指定格子里填石子,如果指定格子里已经填过了,则找到与其曼哈顿距离最小的格子,然后填进去,有多个的时候依次按x.y从小到大排序然后取最小的.输出 ...

随机推荐

  1. 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 十四 ║ VUE 计划书 & 我的前后端开发简史

    ---新内容开始--- 番外 大家周一好呀,又是元气满满的一个周一呀!感谢大家在周一这个着急改Bug的黄金时期,抽出时间来看我的博文哈哈哈,时间真快,已经到第十四篇博文了,也很顺顺(跌跌)利利 (撞撞 ...

  2. springboot~lombok使用总结

    @Getter & @Setter 生成getter和setter块 @Data注解 @Data相当于@Getter @Setter @RequiredArgsConstructor @ToS ...

  3. LindDotNetCore~Ocelot实现微服务网关

    回到目录 网关在硬件里有自己的定义,而在软件架构里也有自己的解释,它就是所有请求的入口,请求打到网关上,经过处理和加工,再返回给客户端,这个处理过程中当然就是网关的核心,也是Ocelot的核心,我们可 ...

  4. Raptor井字棋游戏

    作为大学第一个小作品,记录一下,也给那些想接触到Raptor游戏的人一个小小的参考QAQ至于Raptor的语法和使用,可以参考一下他的帮助手册,看不懂英文的话可以复制放到翻译上看. 以上是主函数 以下 ...

  5. java 深克隆(深拷贝)与浅克隆(拷贝)详解

    java深克隆和浅克隆 基本概念 浅复制(浅克隆) 被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象.换言之,浅复制仅仅复制所拷贝的对象,而不复制它所引用的对 ...

  6. Spring中关于AOP的实践之AspectJ方式实现通知

    (本文中如有不当之处,恳请批评指正) AspectJ方式的简化了通知的出现复杂度.但是对配置文件的操作复杂度有了一定的提升 一. 配置通知 package com.xkx.adviceDemo; im ...

  7. 多线程之Thread

    Thread类可以创建和控制线程,Thread类的构造函数重载为接受ThreadStart和ParameterizedThreadStart类型的委托参数. Thread类默认创建的是前台线程,所以我 ...

  8. python的学习笔记__初识函数

    函数定义与调用 #函数定义 def mylen(): """计算s1的长度""" s1 = "hello world" ...

  9. C# 利用位运算传递多个参数方法

    前言 在工作中用sendMessage的方法向另外一个进程中传递窗体的位置,长度,宽度四个值,但是sendMessage的方法签名中只有两个参数.于是在网上找到了一些代码,找到了这个利用位运算来合并参 ...

  10. jsp:set/getProperty底层实现的探究

    关于jsp:set/getProperty底层实现的探究 今天上课讲到<jsp:useBean>时涉及到了<jsp:setProperty>和<jsp:getProper ...