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] \ ...
随机推荐
- highcharts使用-拼接数据
在写后台统计时,使用highcharts 作为数据视图化的工具 PHP端 拼接数据 1 首先分组查询出来数据 2 然后拼接出来想要的数据格式 $c_x ='';foreach ($courierGro ...
- Java 容器之Hashset 详解
Java 容器之Hashset 详解.http://blog.csdn.net/nvd11/article/details/27716511
- NancyFX 第十一章 Bootstrapping
本章我们将深入Nancy的内部,对Nancy的内部组件进行修改和调整. 那什么是bootstrap哪?字典里是这么介绍的: 一般而言,处于引导中(bootstrapping)是在终端用户可以使用之前开 ...
- c# MongoDB Driver 官方教程翻译
先贴官方文档地址:http://mongodb.github.io/mongo-csharp-driver/2.5/getting_started/quick_tour/ 安装部分很简单,nuget搜 ...
- thoughtworks面试题分析与解答
题目描述 A squad of robotic rovers are to be landed by NASA on a plateau on Mars. This plateau, which is ...
- shell常用脚本
shell常用脚本 author:headsen chen 2017-10-17 15:36:17 个人原创,转载请注明,否则依法追究法律责任 1,vim name.grep.sh 2,cat ...
- selenium 学习之路开始了,一遍搬一遍理解学习,加油!!!
selenium 学习之路开始了,一遍搬一遍理解学习,加油!!!
- 【django之分页器】
一.什么是分页功能 二.Django的分页器(paginator) 语法: paginator = Paginator(book_list, 8) #8条一页print("count:&qu ...
- springMVC的异常处理
1. 异常 什么是异常: 在程序中预期会出现,但是却无法处理的问题,叫做异常 异常处理原则: 延迟处理 先记着...,后续补充
- Java-Integer源码分析
除了两种浮点型,剩下的几种基本数据类型的包装类几乎都实现了常量池,有好处用数据的时候直接去拿,没有再去创建,坏处是在程序编译的时候就存入大量数据不管用不用到.下面是一篇很好的文章,很详细,转自:htt ...