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 ;
}

  

最新文章

  1. Spark读写Hbase的二种方式对比
  2. [翻译] Autoac 最佳实践和建议
  3. Java实现选择排序
  4. Beta版本冲刺——day2
  5. 安装SQL SERVER开启SA用户登录的方法
  6. Python之路-python(面向对象一)
  7. three.js 之旅 (二)
  8. C# .ToString() 格式化
  9. Xilium.CefGlue怎么使用Js调用C#方法
  10. Java中的哈希
  11. skiing(搜索+记忆化搜索)
  12. nodeJS实现路由功能
  13. Java compiler level does not match the version of the installed java project facet错误的解决
  14. MyEclipse 报错:Errors running builder &#39;DeploymentBuilder&#39; on project &#39;工程名&#39;
  15. 读《阿里Java开发手册》总结(1)
  16. Easyui datagrid 特殊处理,记录笔记
  17. android 回调函数使用简介
  18. [LeetCode] 476. Number Complement_Easy tag: Bit Manipulation
  19. 使用MyEclipse设置所需配置的iOS应用
  20. [4] 圆锥(Cone)图形的生成算法

热门文章

  1. &amp;quot;转成&quot;
  2. 一个爬取lativ诚衣网站上模特穿搭图片的爬虫
  3. [luoguP3092] [USACO13NOV]没有找零No Change(状压DP + 二分)
  4. HDU 3527 SPY
  5. ES6__数据结构 Map
  6. hdu 2438 Turn the corner [ 三分 ]
  7. JavaScript 将行结构数据转化为树结构数据源(高效转化方案)
  8. Xcode waring: no rule to process file *** 警告提示
  9. 51nod 马拉松30 C(构二分图+状压dp)
  10. 初学总结--------Java修饰符与修饰关键字(且叫修饰关键字)