[codeforces792C][dp]
https://codeforc.es/contest/792/problem/C
1 second
256 megabytes
standard input
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.
The first line of input contains n — a positive integer number without leading zeroes (1 ≤ n < 10100000).
Print one number — any beautiful number obtained by erasing as few as possible digits. If there is no answer, print - 1.
1033
33
10
0
11
-1
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 ;
}
最新文章
- 分析App应用市场, App应用有哪些类型
- 【001:C# 中 get set 简写存在的陷阱】
- Remoting&;WebService的区别之处
- 【leetcode】Largest Number ★
- 使用FMDB事务批量更新数据库
- get/post方式调用http接口
- HTML5新事物
- Network Link Conditioner模拟不同网络环境
- SVN备份教程(一)
- 未来 USB Type-C 将可靠软体判断线材是否符合规定
- angularjs于directive声明scope说明何时以及如何使用对象修饰符
- Activity的Task详解
- ListView 介绍
- 用感知机(Perceptron)实现逻辑AND功能的Python3代码
- mysql一些使用技巧
- Ubuntu中firefox设置成中文
- MySQL之表相关操作
- SQL学习指南第三篇
- Ubuntu16.04重新安装MySQL数据库
- BFS与DFS算法解析