[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 ...
随机推荐
- sysbench的简单安装
1. 下载 可以到网站上面找 我用到的这个是201908最新的 wget https://src.fedoraproject.org/repo/pkgs/sysbench/sysbench-1.0.1 ...
- postgres csv日志和查看用户权限
最近在使用postgres 时遇到的2个问题,顺便记录一下查到的比较好的资料. 怀疑postgres在执行SQL时报错,程序日志中有无明确异常信息.通过查看csv日志来确定是否SQL真的是执行时报错. ...
- HanLP-命名实体识别总结
人名识别 在HanLP中,基于角色标注识别了中国人名.首先系统利用隐马尔可夫模型标注每个词语的角色,之后利用最大模式匹配法对角色序列进行匹配,匹配上模式的即为人名.理论指导文章为:<基于角色标注 ...
- SQLite进阶-17.视图
目录 视图(View) 操作视图 更新视图 删除视图 查看所有的视图 视图(View) 视图是一个预定义的SQLite查询的形式存在的表的组合. 可以包含一个表的所有行或从一个或多个表选定行.可以从一 ...
- AC自动机fail树上dfs序建线段树+动态memset清空
题意:http://acm.hdu.edu.cn/showproblem.php?pid=4117 思路:https://blog.csdn.net/u013306830/article/detail ...
- Windows一键设置环境变量(以设置java环境变量为例)
右击以管理员方式运行 JDKSetting.bat @echo off color 0a echo.------------------------------------ echo.TODO:设 ...
- skywalking-agent 与docker组合使用
docker部署 公司有使用docker部署的微服务 可以直接使用 仓库/java:8-jdk-alpine-asla-shanghai-1-skyagent-2作为基础镜像 这个镜像包是java8 ...
- Jmeter4.0---- 测试数据说明(17)
1.说明 jmeter工具对于请求的测试结果,有多种形式展现,但是数据比较难懂,现在针对不同的展现做具体的说明. 2.监听器 (一)图形结果 (1)样本数目:总共发到服务器的请求数 (2)最新样本:服 ...
- Eclipse MyEclipse 反编译.class文件 myeclipse source not found
首先,需要下载两个必须的插件包. 一个是:准备反编译需要的jad.exe 下载地址:http://varaneckas.com/jad/ 二个是:反编译编辑器net.sf.jadclipse_3.3. ...
- Qt调用VS生成的dll
预备知识: 1.如果在没有导入库文件(.lib),而只有头文件(.h)与动态链接库(.dll)时,我们才需要显示调用,如果这三个文件都全的话,我们就可以使用简单方便的隐式调用. 2.通常Windo ...