Column Addition~DP(脑子抽了,当时没有想到)
Description
A multi-digit column addition is a formula on adding two integers written like this:
A multi-digit column addition is written on the blackboard, but the sum is not necessarily correct. We can erase any number of the columns so that the addition becomes correct. For example, in the following addition, we can obtain a correct addition by erasing the second and the forth columns.
Your task is to find the minimum number of columns needed to be erased such that the remaining formula becomes a correct addition.
Input
There are multiple test cases in the input. Each test case starts with a line containing the single integer n, the number of digit columns in the addition (1 ⩽ n ⩽ 1000). Each of the next 3 lines contain a string of n digits. The number on the third line is presenting the (not necessarily correct) sum of the numbers in the first and the second line. The input terminates with a line containing “0” which should not be processed.
Output
For each test case, print a single line containing the minimum number of columns needed to be erased.
Sample Input
3
123
456
579
5
12127
45618
51825
2
24
32
32
5
12299
12299
25598
0
Sample Output
0
2
2
1 这题就是给你一个竖式,然后看去除多少列,能使这个竖式正确。
这题其实很好写,唉 ,DP写少了, 其实这个是个很经典的DP
求最长上升子序列的变形,思想一样,只是判断条件不同而已。
这么裸的DP换个样子我就认不出了。菜是原罪啊!!!! 注意
a[i]+b[i]-10==c[i] 只在这个条件下更新ans的值是有原因的,
因为你如果此时存在进位的情况 你并不能判断他到底是不是该去还是留。
求出最长的对的式子,用n-ans答案就出来了
我觉得我要开始我的基础DP训练了 , 这个真的不能再拖了
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
const int maxn =;
int a[maxn],b[maxn],c[maxn],dp[maxn],k[maxn];
int n;
int main() {
//freopen("DATA.txt","r",stdin );
while(scanf("%d",&n),n){
memset(dp,,sizeof(dp));
memset(k,,sizeof(k));
for (int i= ;i<n ;i++ )
scanf("%1d",&a[i]);
for (int i= ;i<n ;i++ )
scanf("%1d",&b[i]);
for (int i= ;i<n ;i++ )
scanf("%1d",&c[i]);
int ans=;
for (int i=n- ;i>= ;i--) {
if (a[i]+b[i]==c[i]) {
dp[i]=;
k[i]=;
if (dp[i]>ans) ans=dp[i];
}
if (a[i]+b[i]-==c[i]) {
dp[i]=;
k[i]=;
}
for (int j=n- ;j>i ;j--) {
if (a[i]+b[i]+k[j]==c[i] && dp[i]<dp[j]+) {
k[i]=;
dp[i]=dp[j]+;
if (dp[i]>ans) ans=dp[i];
}
if (a[i]+b[i]+k[j]-==c[i] && dp[i]<dp[j]+ ) {
k[i]=;
dp[i]=dp[j]+;
}
}
}
printf("%d\n",n-ans);
}
return ;
}
Column Addition~DP(脑子抽了,当时没有想到)的更多相关文章
- 【动态规划】Column Addition @ICPC2017Tehran/upcexam5434
时间限制: 1 Sec 内存限制: 128 MB 题目描述 A multi-digit column addition is a formula on adding two integers writ ...
- CSU-2034 Column Addition
CSU-2034 Column Addition Description A multi-digit column addition is a formula on adding two intege ...
- 2018湖南多校第二场-20180407 Column Addition
Description A multi-digit column addition is a formula on adding two integers written like this:
- leetcode修炼之路——387. First Unique Character in a String
最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...
- CQOI2021 退役记
Day -1 晚上去了酒店然后就睡觉了. Day 1 进考场之前互相奶. 进了考场之后看题,发现T1很水(伏笔1,然后直接开始写 \(\Theta(n\log^2n)\)(二分+动态开点线段树),调了 ...
- Fzu2109 Mountain Number 数位dp
Accept: 189 Submit: 461Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description One ...
- poj 1088 滑雪(区间dp+记忆化搜索)
题目链接:http://poj.org/problem?id=1088 思路分析: 1>状态定义:状态dp[i][j]表示在位置map[i][j]可以滑雪的最长区域长度: 2>状态转移方程 ...
- CH0103 最短Hamilton路径 dp
正解:状压dp 解题报告: 完了吃枣退役:D 我是真的没想到这是个dp...脑子越来越不好了,大概是太久没碰OI了都要生疏了...哭了,感觉自己太傻逼了可能不适合学信息... 知道是个状压dp就eas ...
- bzoj 1630: [Usaco2007 Demo]Ant Counting【dp】
满脑子组合数学,根本没想到dp 设f[i][j]为前i只蚂蚁,选出j只的方案数,初始状态为f[0][0]=1 转移为 \[ f[i][j]=\sum_{k=0}^{a[i]}f[i-1][j-k] \ ...
随机推荐
- 常用Oracle进程资源查询语句(运维必看)
(一)根据程序名称查找相关信息select A.process,B.spid,A.sid,A.serial#,A.sql_address,A.username,A.program,A.status,A ...
- NancyFX 附录: Nuget程序包
Nancy.Authentication.Forms 该程序包向Nancy提供标准的基于ASP.NET/IIS的表单身份验证服务. 采用这个模块启用身份验证后,可以获得标准ASP.NET表单验证方式. ...
- python爬微信公众号前10篇历史文章(1)-思路概览
作为程序员,要时刻保持一颗好奇心和想要学习的姿态. 练习怎样利用搜狗微信爬取某指定微信公众号的历史文章.爬取微信公众号本身难度非常大,感谢搜狗提供了一个可以爬取数据的平台. 代码部分参考于: http ...
- Filecoin官方更新: Q4工作进展和2018年工作计划
ICO过后,Filecoin团队一直没有对外更新过工作计划(很多投资人都等待的不耐烦了).经过漫长的等待,在新年的第一个工作日,我们终于等来了来自于filecoin团队的声音, 这次更新真是出乎小编的 ...
- 如何在IPFS里面上传一张图片
之前有好几人问过小编,想在IPFS里面上传一张图片.如何做? 今天小编就讲一下如何在IPFS里面上传.下载文件? 1 下载IPFS软件 下载地址:https://dist.ipfs.io/#go-ip ...
- three.js 实现全景以及优化(2)
继昨天全景实现后,再做了一个全景图切换实验; code:https://github.com/Thinkia/threejs_/blob/master/test/test1-panorama/inde ...
- Mycat 分片规则详解--范围分片
实现方式:切分规则根据文件(autopartition-long.txt)配置的范围来进行切片,制定基准列的取值范围,然后把这一范围的所有数据都放到一个DN上面 优点:适用于整体数量可知或总数量为固定 ...
- JAVA基本数据类型和引用数据类型的区别
[基本数据类型] 基本数据类型:声明时直接在栈内存中开辟空间,并直接在当前内存中存放数据,赋值时传递的是变量中的值,总的来说,基本数据类型是传值的. [引用数据类型] 声明引用数据类型(数组或对象), ...
- MyBatis-plus 代码生成器
1.添加pom文件依赖 <!-- Mybatis-Plus 自动生成实体类--> <dependency> <groupId>com.baomidou</gr ...
- 韩天峰博客 php基础知识学习记录
http://rango.swoole.com 写好PHP代码真的不容易,给大家几个建议: 慎用全局变量,全局变量不好管理的,会导致你的代码依赖于全局变量,而耦合度太高. 一定不要复制粘贴代码,可重用 ...