C. Divide by Three
 

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
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.

 题意:

  给你一个01串,问你最少删除多少个字符,使得余下的串10进制下%3=0,不得有前导0

题解:

  设定dp[i][j][0/1/2]表示前i个字符中,组成%3=j的串需要的最少删除次数;

  同时0表示还未填数,

1表示有一个前导0,

   2表示开头填了一个非0数

  需要记录路径pre

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 1e5+, M = 1e3+, mod = 1e9+, inf = 2e9; int dp[N][][],pre[N][][];//前i个数mod3 = j最少需要删除的字母个数 是否有前导0
char s[N];
int n,a[N],ans[N];
int main() {
scanf("%s",s+);
int n = strlen(s+);
for(int i = ; i <= n; ++i) a[i] = s[i] - '';
for(int i = ; i <= n; ++i) {
for(int j = ; j < ; ++j) dp[i][j][] = inf,dp[i][j][] = inf, dp[i][j][] = inf;
}
dp[][][] = ;
for(int i = ; i < n; ++i) {
for(int j = ; j < ; ++j) {
if(dp[i][j][] < dp[i+][(j+a[i+])%][(a[i+])==?:]) {
dp[i+][(j+a[i+])%][(a[i+])==?:] = dp[i][j][];
pre[i+][(j+a[i+])%][(a[i+])==?:] = ;
}
if(dp[i][j][]+ < dp[i+][(j+a[i+])%][(a[i+])==?:]) {
dp[i+][(j+a[i+])%][(a[i+])==?:] = dp[i][j][]+;
pre[i+][(j+a[i+])%][(a[i+])==?:] = ;
}
if(dp[i][j][] < dp[i+][(j+a[i+])%][]) {
dp[i+][(j+a[i+])%][] = dp[i][j][];
pre[i+][(j+a[i+])%][] = ;
} if(dp[i][j][]+ < dp[i+][j][]) {
dp[i+][j][] = dp[i][j][]+;
pre[i+][j][] = -;
}
if(dp[i][j][]+ < dp[i+][j][]) {
dp[i+][j][] = dp[i][j][]+;
pre[i+][j][] = -;
}
if(dp[i][j][]+ < dp[i+][j][]) {
dp[i+][j][] = dp[i][j][]+;
pre[i+][j][] = -;
}
}
}
if(dp[n][][] >= inf && dp[n][][] >= inf) {
puts("-1");
return ;
}
if(dp[n][][] < dp[n][][]) {
puts("");
return ;
}
int j = ,num = n - dp[n][][];
for(int i = n; i >= ; --i) {
if(pre[i][j][] == ) {
ans[num--] = a[i];
j = ((j - a[i])% + ) % ;
}
if(num == ) break;
}
for(int i = ; i <= n - dp[n][][]; ++i) cout<<ans[i];
return ;
}

  

Educational Codeforces Round 18 C. Divide by Three DP的更多相关文章

  1. Educational Codeforces Round 53 E. Segment Sum(数位DP)

    Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...

  2. Educational Codeforces Round 18

    A. New Bus Route 题目大意:给出n个不同的数,问差值最小的数有几对.(n<=200,000) 思路:排序一下,差值最小的一定是相邻的,直接统计即可. #include<cs ...

  3. Educational Codeforces Round 18 D

    Description T is a complete binary tree consisting of n vertices. It means that exactly one vertex i ...

  4. Educational Codeforces Round 18 B

    Description n children are standing in a circle and playing the counting-out game. Children are numb ...

  5. Educational Codeforces Round 18 A

    Description There are n cities situated along the main road of Berland. Cities are represented by th ...

  6. Educational Codeforces Round 16 E. Generate a String dp

    题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...

  7. Educational Codeforces Round 8 D. Magic Numbers 数位DP

    D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...

  8. Educational Codeforces Round 19 E. Array Queries(暴力)(DP)

    传送门 题意 给出n个数,q个询问,每个询问有两个数p,k,询问p+k+a[p]操作几次后超过n 分析 分块处理,在k<sqrt(n)时,用dp,大于sqrt(n)用暴力 trick 代码 #i ...

  9. Educational Codeforces Round 67 E.Tree Painting (树形dp)

    题目链接 题意:给你一棵无根树,每次你可以选择一个点从白点变成黑点(除第一个点外别的点都要和黑点相邻),变成黑点后可以获得一个权值(白点组成连通块的大小) 问怎么使权值最大 思路:首先,一但根确定了, ...

随机推荐

  1. 性能优化 java 24 次阅读 · 读完需要 15 分钟 0

    摘要: 技术传播的价值,不仅仅体现在通过商业化产品和开源项目来缩短我们构建应用的路径,加速业务的上线速率,也会体现在优秀程序员在工作效率提升.产品性能优化和用户体验改善等小技巧方面的分享,以提高我们的 ...

  2. 条款16:成对使用new和delete时要采取相同形式

    NOTE: 1.如果你在new表达式中使用[],必须在相应的delete表达式中也使用[].如果你在new表达式中不使用[],一定不要在相应的delete表达式中使用[].

  3. 【HIHOCODER 1325】 平衡树·Treap

    描述 小Ho:小Hi,我发现我们以前讲过的两个数据结构特别相似. 小Hi:你说的是哪两个啊? 小Ho:就是二叉排序树和堆啊,你看这两种数据结构都是构造了一个二叉树,一个节点有一个父亲和两个儿子. 如果 ...

  4. HDU 3932 模拟退火

    HDU3932 题目大意:给定一堆点,找到一个点的位置使这个点到所有点中的最大距离最小 简单的模拟退火即可 #include <iostream> #include <cstdio& ...

  5. 后缀排序(codevs 1500)

    题目描述 Description 天凯是MIT的新生.Prof. HandsomeG给了他一个长度为n的由小写字母构成的字符串,要求他把该字符串的n个后缀(suffix)从小到大排序. 何谓后缀?假设 ...

  6. Free Web Application Firewall相关资料

    http://www.freewaf.org/solution/#1 http://baike.soso.com/v60659982.htm

  7. poj -1185 炮兵阵地 (经典状压dp)

    http://poj.org/problem?id=1185 参考博客:http://poj.org/problem?id=1185 大神博客已经讲的很清楚了,注意存状态的时候是从1开始的,所以初始化 ...

  8. markdown八条基础语法

    1.空行 答:使用全角打出空格,之后再换行就可以打出空行了 2.标题 答:#表示标题,#表示一级标题,字号最大,一共有六级标题 3.列表 答:- 无序列表,1. 有序列表,注意和文本之间有空格 4.链 ...

  9. LCA 求 树中两个点的距离

    PS:在树中:dis(u,v)=dis(root,v)+dis(root,u)-2*dis(root,lca(u,v)); 这个性质可以写很多题. vector<int>mp[N];int ...

  10. hp 88a加粉

    http://v.youku.com/v_show/id_XNzEzODEwNzMy.html