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. web即时通信技术

    ajax jsonp polling setInterval定期发送ajax请求.服务器压力大,浪费流量,大部分是无效请求 ajax long polling ajax发起请求,服务器对于这个请求一直 ...

  2. 微信小程序的坑之wx.miniProgram.postMessage

    工作中有个需求是小程序的网页在关闭的时候,需要回传给小程序一个参数 查阅小程序官方文档,有这样一个接口 wx.miniProgram.postMessage ,可以用来从网页向小程序发送消息,然后通过 ...

  3. spring-mvc jackson配置json为空不输出

    使用的spring-mvc版本是4.1.6,jackson版本是2.1.4 在spring-mvc配置文件中添加以下代码就行 <mvc:annotation-driven> <mvc ...

  4. SpringMVC的删除功能

    Dao层 package net.roseindia.dao; import java.util.Date; import java.util.List; import net.roseindia.m ...

  5. Android开发——查询/杀死手机里正在运行的进程

    0. 前言 以前有同学好像做过一个叫"自习君"的App,开启后自动检测用户这一天的自习时间,在学校里宣传广告还打了不少.其实实现原理非常简单,在SQlite数据库(也可以通过文件) ...

  6. js中的constructor 和prototype

    参考 http://www.cnblogs.com/yupeng/archive/2012/04/06/2435386.html function a(c){ this.b = c; this.d = ...

  7. python 监控oracle 数据库

    import cx_Oracle import os db = cx_Oracle.connect('**********') print "Show Oracle Version: &qu ...

  8. HDU-1020-Encoding,题意不清,其实很水~~

    Encoding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) http:// ...

  9. Go map例题

    package main import "fmt" //map例题 //寻找最长不含有重复字符的子串 // abcabcbb -> abc //pwwkew ->wke ...

  10. POJ 2777 Count Color【线段树】

    题目大意:要求完成以下两个操作:1.将一个区间刷上一种颜色2.询问一段区间上有多少种颜色 思路:这两个操作线段树都可以很迅速的完成,具体做法是:线段树上每个节点存这个线段上的颜色数量,由于颜色数很少, ...