https://codeforc.es/contest/792/problem/C

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

Copy
1033
output

Copy
33
input

Copy
10
output

Copy
0
input

Copy
11
output

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

给一个数字,你可以删除字符串某一个位置的字符,使其满足下列条件:

  • 数字没有前导0
  • 数字能够被3整除

求经过最少操作次数之后得到的结果。

题解:dp过程记录操作即可

 #include<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<"["<<#x<<"]"<<" is "<<x<<endl;
typedef long long ll;
int dp[][][],pre[][][][],xx[][][];
char ch[],q[];
int main(){
scanf("%s",ch+);
int len=strlen(ch+);
memset(dp,-,sizeof(dp));
dp[][][]=;
int f=-;
for(int i=;i<=len;i++){
int x=ch[i]-'';
if(x){
if((dp[i-][][]!=-)&&(dp[i][][]==-||(dp[i-][][]+<dp[i][][]))){
dp[i][][]=dp[i-][][]+;
pre[i][][][]=;
pre[i][][][]=;
xx[i][][]=;
}
if((dp[i-][(-x+)%][]!=-)&&(dp[i][][]==-||(dp[i-][(-x+)%][]<dp[i][][]))){
dp[i][][]=dp[i-][(-x+)%][];
pre[i][][][]=(-x+)%;
pre[i][][][]=;
xx[i][][]=;
}
if((dp[i-][(-x+)%][]!=-)&&(dp[i][][]==-||(dp[i-][(-x+)%][]<dp[i][][]))){
dp[i][][]=dp[i-][(-x+)%][];
pre[i][][][]=(-x+)%;
pre[i][][][]=;
xx[i][][]=;
}
if((dp[i-][][]!=-)&&(dp[i][][]==-||(dp[i-][][]+<dp[i][][]))){
dp[i][][]=dp[i-][][]+;
pre[i][][][]=;
pre[i][][][]=;
xx[i][][]=;
}
if((dp[i-][(-x+)%][]!=-)&&(dp[i][][]==-||(dp[i-][(-x+)%][]<dp[i][][]))){
dp[i][][]=dp[i-][(-x+)%][];
pre[i][][][]=(-x+)%;
pre[i][][][]=;
xx[i][][]=;
}
if((dp[i-][(-x+)%][]!=-)&&(dp[i][][]==-||(dp[i-][(-x+)%][]<dp[i][][]))){
dp[i][][]=dp[i-][(-x+)%][];
pre[i][][][]=(-x+)%;
pre[i][][][]=;
xx[i][][]=;
}
if((dp[i-][][]!=-)&&(dp[i][][]==-||(dp[i-][][]+<dp[i][][]))){
dp[i][][]=dp[i-][][]+;
pre[i][][][]=;
pre[i][][][]=;
xx[i][][]=;
}
if((dp[i-][][]!=-)&&(dp[i][][]==-||(dp[i-][][]+<dp[i][][]))){
dp[i][][]=dp[i-][][]+;
pre[i][][][]=;
pre[i][][][]=;
xx[i][][]=;
}
if((dp[i-][(-x+)%][]!=-)&&(dp[i][][]==-||(dp[i-][(-x+)%][]<dp[i][][]))){
dp[i][][]=dp[i-][(-x+)%][];
pre[i][][][]=(-x+)%;
pre[i][][][]=;
xx[i][][]=;
}
if((dp[i-][(-x+)%][]!=-)&&(dp[i][][]==-||(dp[i-][(-x+)%][]<dp[i][][]))){
dp[i][][]=dp[i-][(-x+)%][];
pre[i][][][]=(-x+)%;
pre[i][][][]=;
xx[i][][]=;
}
}
else{
f=;
if((dp[i-][][]!=-)&&(dp[i][][]==-||(dp[i-][][]+<dp[i][][]))){
dp[i][][]=dp[i-][][]+;
pre[i][][][]=;
pre[i][][][]=;
xx[i][][]=;
}
if((dp[i-][][]!=-)&&(dp[i][][]==-||(dp[i-][][]<dp[i][][]))){
dp[i][][]=dp[i-][][];
pre[i][][][]=;
pre[i][][][]=;
xx[i][][]=;
}
if((dp[i-][][]!=-)&&(dp[i][][]==-||(dp[i-][][]<dp[i][][]))){
dp[i][][]=dp[i-][][];
pre[i][][][]=;
pre[i][][][]=;
xx[i][][]=;
}
if((dp[i-][][]!=-)&&(dp[i][][]==-||(dp[i-][][]<dp[i][][]))){
dp[i][][]=dp[i-][][];
pre[i][][][]=;
pre[i][][][]=;
xx[i][][]=;
}
}
}
int tot=;
if(dp[len][][]!=-&&dp[len][][]<dp[len][][]){
int t1=;
int t2=;
for(int i=len;i>=;i--){
if(!xx[i][t1][t2]){
q[++tot]=ch[i];
}
int tt1=t1;
int tt2=t2;
t1=pre[i][tt1][tt2][];
t2=pre[i][tt1][tt2][];
}
for(int i=tot;i>=;i--){
printf("%c",q[i]);
}
printf("\n");
}
else{
printf("%d\n",f);
}
return ;
}

最新文章

  1. 分析App应用市场, App应用有哪些类型
  2. 【001:C# 中 get set 简写存在的陷阱】
  3. Remoting&amp;WebService的区别之处
  4. 【leetcode】Largest Number ★
  5. 使用FMDB事务批量更新数据库
  6. get/post方式调用http接口
  7. HTML5新事物
  8. Network Link Conditioner模拟不同网络环境
  9. SVN备份教程(一)
  10. 未来 USB Type-C 将可靠软体判断线材是否符合规定
  11. angularjs于directive声明scope说明何时以及如何使用对象修饰符
  12. Activity的Task详解
  13. ListView 介绍
  14. 用感知机(Perceptron)实现逻辑AND功能的Python3代码
  15. mysql一些使用技巧
  16. Ubuntu中firefox设置成中文
  17. MySQL之表相关操作
  18. SQL学习指南第三篇
  19. Ubuntu16.04重新安装MySQL数据库
  20. BFS与DFS算法解析

热门文章

  1. 输出重定向之python2和python3的区别
  2. 【Python】【demo实验33】【练习实例】【列表的反转】
  3. quartz报错 org.quartz.impl.StdSchedulerFactory.
  4. A&lt;=B的前提下全排列A使答案尽量大
  5. vue的基本语法
  6. python flask 如何读取数据库数据并返回到html
  7. 服务器上office不能正常使用?
  8. MVC授权不通过之后不执行任何自定义ActionFilter
  9. SSE指令集加速之 I420转BGR24
  10. 使用高德地图JS获取当前位置和经纬度