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] \ ...
随机推荐
- 由html,body{height:100%}引发的对html和body的思考
html,body{height:100%} 今天看到一个CSS样式:html,body{height:100%},第一次看到,感觉挺奇怪,为什么html还需要设置height:100%呢,html不 ...
- iframe结构的项目,登录页面嵌套
参考:http://www.cnblogs.com/qixin622/p/6548076.html 在网页编程时,我们经常需要处理,当session过期时,我们要跳到登陆页面让用户登陆,由于我们可能用 ...
- PHP+MySQL分页原理实现
功能简介:包含上一页.下一页.首页.尾页.跳转页面等功能. 编码寄语:化繁为简,则豁然开朗. 运行截图: 关键步骤: 创建数据库 CREATE TABLE `page` ( `ID` ) NOT NU ...
- Ext概述
Ext是一个具有丰富组件的javascript集合类库,除了自身提供的一套选择器.效果.ajax等功能,还提供了大量的javascript创建页面元素的类.方法.这个意味着:只要客户端支持javasc ...
- python中的lambda函数用法
例1:传入多个参数的lambda函数def sum(x,y): return x+y用lambda来实现:p = lambda x,y:x+yprint(4,6) 例2:传入一个参数的lambda函数 ...
- PHP中的ArrayAccess用法详解
在Laravel的源码当中,作者多次使用到了PHP SPL中的ArrayAccess接口,那么这个ArrayAccess接口到底有什么作用呢?我会用一个简单的例子跟大家说明. 请看下面的这段代码,Fo ...
- SqlSever数据库实践周
资源下载 进行了为期5天的数据库设计,虽然以前用过数据库,但是这一次是使用书上规范的设计流程设计的数据库,感觉有必要记录一下,希望对其他人有帮助. 我的收获:在这个博客中会体现到我的收获,对于将要进行 ...
- Lucene-02:搜索初步
承接上一篇文章. package com.amazing; import java.io.File; import java.io.IOException; import org.apache.luc ...
- C++标准库string类型的使用和操作总结
string是C++标准库最重要的类型之一,string支持长度可变的字符串,其包含在string头文件中.本文摘自<C++PRIMER 第四版·特别版>和个人的一些总结. 一.声明和初始 ...
- 把文件每隔三行合并成一行(awk之RS、ORS与FS、OFS)
比如文本如下:123abc合并后的结果是:1 2 3a b c #.txt a b c awk之RS.ORS与FS.OFS 转自http://www.cnblogs.com/fhefh/archive ...