C. Divide by Three

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.

The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not.

Write a program which for the given n will find a beautiful number such that n can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number n.

If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.

Input

The first line of input contains n — a positive integer number without leading zeroes (1 ≤ n < 10100000).

Output

Print one number — any beautiful number obtained by erasing as few as possible digits. If there is no answer, print  - 1.

Examples

input
1033
output
33
input
10
output
0
input
11
output
-1

Note

In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two.

思路:咕咕咕。

代码:

#include<bits/stdc++.h>
using namespace std; char num[100010];
int dp[100010][3],f[100010][3]; void dfs(int x,int temp){
if (x==0){
return;
}
else{
if (f[x][temp]==3){
dfs(x-1,temp);
}
else{
dfs(x-1,f[x][temp]);
printf("%c",num[x]);
}
}
} int main(){
scanf("%s",num+1);
int ll=strlen(num+1); for (int i=0; i<=ll; i++)
for (int j=0; j<3; j++)
dp[i][j]=1e6;
dp[0][0]=0; int find0=0;
for (int i=1; i<=ll; i++){
int t=num[i]-'0';
if (t==0) find0=1; for (int j=0; j<3; j++){
if ( dp[i-1][j]+1 < dp[i][j] ){
dp[i][j]=dp[i-1][j]+1;
f[i][j]=3;
} if (dp[i-1][j]!=i-1 || t!=0)
if ( dp[i-1][j] < dp[i][(j+t)%3] ){
dp[i][(j+t)%3]=dp[i-1][j];
f[i][(j+t)%3]=j;
}
}
} if (dp[ll][0]==ll)
if (find0) printf("0");
else
printf("-1");
else
dfs(ll,0); return 0;
}

CodeForces - 792C Divide by Three (DP做法)的更多相关文章

  1. codeforces 792C. Divide by Three

    题目链接:codeforces 792C. Divide by Three 今天队友翻了个大神的代码来问,我又想了遍这题,感觉很好,这代码除了有点长,思路还是清晰易懂,我就加点注释存一下...分类吧. ...

  2. CodeForces 792C - Divide by Three [ 分类讨论 ]

    删除最少的数位和前缀0,使得剩下的数能被3整除 等价于各数位数字之和能被3整除. 当前数位和可能是 0, 1, 2(mod 3) 0: 直接处理 1: 删除一个a[i]%3 == 1 或者 两个a[i ...

  3. codeforces 1288C. Two Arrays(dp)

    链接:https://codeforces.com/contest/1288/problem/C C. Two Arrays 题意:给定一个数n和一个数m,让构建两个数组a和b满足条件,1.数组中所有 ...

  4. [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)

    [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...

  5. Educational Codeforces Round 18 C. Divide by Three DP

    C. Divide by Three   A positive integer number n is written on a blackboard. It consists of not more ...

  6. 【codeforces 792C】Divide by Three

    [题目链接]:http://codeforces.com/contest/792/problem/C [题意] 让你删掉最少的数字使得剩下的数字%3==0 [题解] 看代码..内置题解了现在. [完整 ...

  7. Divide by Three CodeForces - 792C

    A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You ...

  8. codeforces 429 On the Bench dp+排列组合 限制相邻元素,求合法序列数。

    限制相邻元素,求合法序列数. /** 题目:On the Bench 链接:http://codeforces.com/problemset/problem/840/C 题意:求相邻的元素相乘不为平方 ...

  9. [CodeForces - 712D]Memory and Scores (DP 或者 生成函数)

    题目大意: 两个人玩取数游戏,第一个人分数一开始是a,第二个分数一开始是b,接下来t轮,每轮两人都选择一个[-k,k]范围内的整数,加到自己的分数里,求有多少种情况使得t轮结束后a的分数比b高.  ( ...

随机推荐

  1. ON DUPLICATE KEY UPDATE 插入or更新

    mean:若数据表中存在以相同主键的记录,就更新该条记录.否则就插入一条新的记录. 单条:INSERT INTO tablename (`field1`,`field2`) VALUES(value1 ...

  2. yii学习笔记(3),自定义全局工具函数

    在平时开发是经常需要打印数据来调试 常见的打印方式有print_r和var_dump,但是这样打印出来格式很乱不好浏览 在打印函数前后加上<pre></pre>就可以将内容原样 ...

  3. day30 进程

    推荐两本书:现代操作系统和操作系统原来,学习好python以后再去研究.   并发:任务的切换,保存状态,存在io的是实现空间和时间的 重复利用 操作系统的发展历史: 第一代(1940-1955)手工 ...

  4. python中函数参数的引用方式

    值传递和引用传递时C++中的概念,在python中函数参数的传递是变量指向的对象的物理内存地址!!! python不允许程序员选择采用传值还是传引用.Python参数传递采用的肯定是“传对象引用”的方 ...

  5. python学习笔记:第4天 列表和元组

    目录 基本数据类型:列表 基本数据类型:元组 补充知识 基本数据类型:列表 1. 列表的介绍 列表也是python的基础的数据类型之一,类似于Java中的数组一样,可以存放很多元素.列表是用括号括起来 ...

  6. C语言中结构体的访问方法解读

    在C语言中,对结构体的访问一般有两种常规方式:"."访问和"->"访问.那么两者有什么区别呢?对C语言有一定了解的同学应该知道,我们新建一个结构体的时候, ...

  7. 北京Uber优步司机奖励政策(4月6日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  8. 手写ORM第一版

    ORM第一版: #Author = __rianley cheng__ #ORM 简易版 from mysql_ import Mysql class Fileld: def __init__(sel ...

  9. LiteOS创建任务的一个BUG

    在任务创建的时候,参数无法传递,第二个参数本来是用来做参数传递的,但是却没用到,很尴尬啊,缺少了这个功能,很多无法写了? osThreadId_t osThreadNew (osThreadFunc_ ...

  10. 编译chromium时下载gn.exe时出错的解决方案

    天朝人写个代码真难,想要编译一下chromium,但是获取代码时各种坑,不是网速慢,就是网络联不通,真难玩. 本文针对下载gn.exe等工具时失败的解决方案. 原因1:gclient没有走代理,针对使 ...