[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 ;
}
[codeforces792C][dp]的更多相关文章
- BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]
1911: [Apio2010]特别行动队 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 4142 Solved: 1964[Submit][Statu ...
- 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...
- AEAI DP V3.7.0 发布,开源综合应用开发平台
1 升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...
- AEAI DP V3.6.0 升级说明,开源综合应用开发平台
AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...
- BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4026 Solved: 1473[Submit] ...
- [斜率优化DP]【学习笔记】【更新中】
参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...
- BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 9812 Solved: 3978[Submit][St ...
- px、dp和sp,这些单位有什么区别?
DP 这个是最常用但也最难理解的尺寸单位.它与“像素密度”密切相关,所以 首先我们解释一下什么是像素密度.假设有一部手机,屏幕的物理尺寸为1.5英寸x2英寸,屏幕分辨率为240x320,则我们可以计算 ...
- android px转换为dip/dp
/** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public int dipTopx(Context context, float dpValue) { final floa ...
随机推荐
- 深度学习-LeCun、Bengio和Hinton的联合综述
深度学习其实要入门也很简单,不要被深度学习.卷积神经网络CNN.循环神经网络RNN等某些“高大上”的专有名词所吓到或被忽悠,要相信大道至简,一个高中生只要愿意学也完全可以入门级了解并依赖一些成熟的Te ...
- - RabbitMQ - 0 - 介绍、linux 和windows安装
目录 一. 介绍 二.windows安装erlang和rabbitMQ 三.Linux安装erlang和RabbitMQ 一. 介绍 rabbitMQ 是基于 erlang 语言开发的, 为了使用 r ...
- Linux基础 目录
一,linux入门介绍 二,界面目录介绍 三,vim使用 四,文件管理. 文件夹管理. 五.用户创建流程.用户管理 .组管理 六.权限管理.软连接/硬链接 七.磁盘管理 八.软件包的管理 九.系统服务 ...
- springboot中配置文件使用1
1.表达方式:application.properties或者application.yml,这是已经约定成俗的文件,不用修改文件名,此文件为全局配置文件. 2.语法格式:yml或者yaml. a.基 ...
- CF网络流练习
1. 103E 大意: 给定$n$个集合, 满足对于任意的$k$, 任意$k$个集合的并集都不少于$k$. 要求选出$k$个集合$(k> 0)$, 使得并恰好等于$k$, 输出最少花费. Hal ...
- Disruptor 并发框架
什么是Disruptor Martin Fowler在自己网站上写了一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金融交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JVM平 ...
- dl in image process
deep learning目前为止无论在分类还是检测上,都是整体的处理,而不会出现像sift这样的局部特征,这个问题或许如果能克服掉,能让检测效果更进一大步.
- C++通用框架和库
C++通用框架和库 来源 https://www.cnblogs.com/skyus/articles/8524408.html 关于 C++ 框架.库和资源的一些汇总列表,内容包括:标准库.Web应 ...
- wrbstrom使用
使用webstrom时遇到Firefox浏览器打不开问题,是webstrom未找到你Firefox的安装路径下面为大家提供解决方法: 文件--->设置--->工具--->web浏览器 ...
- 【php设计模式】责任链模式
责任链模式为请求创建了一个接收者对象的链.这种模式给予请求的类型,对请求的发送者和接收者进行解耦.这种类型的设计模式属于行为型模式. 在这种模式中,通常每个接收者都包含对另一个接收者的引用.如果一个对 ...