[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 ...
随机推荐
- There are no packages available
{ "bootstrapped": true, "channels": [ "https://raw.githubusercontent.com/Ja ...
- scrapy-redis数据去重与分布式框架
数据去重 生成指纹:利用hashlib的sha1,对request的请求体.请求url.请求方法进行加密,返回一个40位长度的16进制的字符串,称为指纹 fp = hashlib.sha1() fp. ...
- easyUI datagrid 刷新取消加载信息 自动刷新闪屏问题
<style type="text/css"> /*-- 消除grid屏闪问题 --//*/ .datagrid-mask { opacity: 0; filter: ...
- S03_CH04_AXI_DMA_OV5640摄像头采集系统
S03_CH04_AXI_DMA_OV5640摄像头采集系统 4.1概述 本课程讲解如何搭建基于DMA的图形系统,方案原理和搭建7725的一样,只是OV5640显示的分辨率是1280X720如下,只是 ...
- S03_CH03_AXI_DMA_OV7725摄像头采集系统
S03_CH03_AXI_DMA_OV7725摄像头采集系统 3.1概述 本课程讲解如何搭建基于DMA的图形系统,方案原理如下. 摄像头采样图像数据后通过DMA送入到DDR,在PS部分产生DMA接收中 ...
- Python实现字符的冒泡排序——说实话,两个数兑换的方法震惊了我,一天比一天感受到了Python的强大
import random M= lettList=[] for i in range(M): lettList.append(chr(random.randrange(,))) for lett i ...
- SQLSERVER 20018 R2 T-SQL 创建linkServer
1. SQLSERVER LINK SQLSERVER EXEC sp_addlinkedserver @server = 'LINKTEST',@srvproduct = '',@provider ...
- .net Core 图片验证码 基于SkiaSharp实现
public class ImageCaptcha { /// <summary> /// 干扰线的颜色集合 /// </summary> private List<SK ...
- JS基础_实参可以是任何值
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- gzip: stdin: not in gzip format 解决办法
# sudo tar zxvf ./jdk-7ull-linux-i586.tar.gz -C /usr/lib/jvm gzip: stdin: not in gzip format tar: Ch ...