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. 如何学好C++语言(转)

    不久前发现陈皓的blog -- 酷壳,闲暇之余看看,学到不少东西,感觉作为一个程序员自己才刚开始也还有很长的路需要去走.真后悔本科到研究生的6年间看书不够,那只有从现在起不断的学习.最近有李开复老师得 ...

  2. python 学习总结1

    计算机与程序设计 一.计算机的概念 1.计算机是根据指令操作数据的设备. 2.计算机主要包括两个功能性一个是功能性另一个是计算性 功能性是对数据的操作,表现为数据计算,输入输出处理和结果存储 可编程性 ...

  3. uwsgs loading shared libraries: libicui18n.so.58 异常处理

    背景 想使用 ningx + uwsgi + flask 搭建 python 应用环境 Python使用的是anaconda3(pyhton 3.6) 依赖包安装完毕,但是执行 uwsgi 的时候出现 ...

  4. Java集合之Queue

    queue 定义 C++:queue Java:Queue 创建与其基本操作 创建: Queue<Integer> q=new LinkedList<Integer>(); 数 ...

  5. .NET Core 文件的上传与下载

    1.前言 文件导入导出是简单且常用的功能,以下示例实现了文件上传与下载的几种方法,如有不妥的地方,欢迎指正.小白一枚,在往目标前进. 2.1.文件上传    --    纯文件 <form ac ...

  6. github私有库购买信息

    github私有库购买信息      一年84美元. 换算成人民币是:532元.   话说其他开发者都买了么?    

  7. cf839c Journey

    大水题 #include <iostream> #include <cstdio> using namespace std; int n, du[100005], hea[10 ...

  8. github新建本地仓库,再同步远程仓库基本用法

    github新建本地仓库,再同步远程仓库基本用法 1 mkdir gitRepo 2 cd gitRepo 3 git init  #初始化本地仓库 4 git add xxx  #添加要push到远 ...

  9. Sencha Touch 2 实现跨域访问

    最近要做手机移动App,最后采用HTMML5来做框架用Sencha Touch,在数据交互时遇到了Ajax跨域访问,在Sencha Touch 2中,实现跨域访问可以使用Ext类库提供给我们的类Ext ...

  10. java HashMap的使用

    java HashMap的使用 import java.util.HashMap; import java.util.Iterator; public class WpsklHashMap { pub ...