时间限制: 1 Sec 内存限制: 128 MB
题目描述

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.
输入
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.
输出
For each test case, print a single line containing the minimum number of columns needed to be erased.
样例输入
3
123
456
579
5
12127
45618
51825
2
24
32
32
5
12299
12299
25598
0
样例输出
0
2
2
1

给你一个只有两个数相加的,长度为n列的加法竖式,问最少删去几列使得竖式成立。
设两个相加的数为a和b,和为c
如果要使第i列的等式成立,应该满足下面四种情况的一种:
1. a[i]+b[i] == c[i]; 刚好
2. a[i]+b[i]-10 == c[i]; 产生进位
3. a[i]+b[i]+1 == c[i]; 接受进位后成立
4. a[i]+b[i]+1-10 == c[i]; 接受进位成立且产生进位
我是从左往右推,令dp[i][0] (i from 1)表示第i位不接受进位时,最少删去的列数;
令dp[i][1] 表示第i位接受进位时,最少删去的列数;
因为第n位一定不接受进位,所以输出dp[n][0]表示答案;
转移方程详见代码
读者也可尝试从右往左推

#define IN_LB() freopen("F:\\in.txt","r",stdin)
#define IN_PC() freopen("C:\\Users\\hz\\Desktop\\in.txt","r",stdin)
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const int INF = 0x3f3f3f3f;
int a[maxn],b[maxn],c[maxn],dp[maxn][2];
int main() {
// IN_LB();
int n;
while(scanf("%d",&n)&&n) {
for(int i=1; i<=n; i++)scanf("%1d",a+i);
for(int i=1; i<=n; i++)scanf("%1d",b+i);
for(int i=1; i<=n; i++)scanf("%1d",c+i);
dp[0][1] = INF;
for(int i=1; i<=n; i++) {
if(a[i]+b[i]==c[i]) {
dp[i][0] = dp[i-1][0];
} else if(a[i]+b[i]-10==c[i]) {
dp[i][0] = min(dp[i-1][0]+1,dp[i-1][1]);
} else dp[i][0] = dp[i-1][0]+1;
if(a[i]+b[i]+1 ==c[i]) {
dp[i][1] = min(dp[i-1][1]+1,dp[i-1][0]);
} else if(a[i]+b[i]+1-10==c[i]) {
dp[i][1] = dp[i-1][1];
} else dp[i][1] = dp[i-1][1]+1;
}
printf("%d\n",dp[n][0]);
}
return 0;
}

【动态规划】Column Addition @ICPC2017Tehran/upcexam5434的更多相关文章

  1. Column Addition~DP(脑子抽了,当时没有想到)

    Description A multi-digit column addition is a formula on adding two integers written like this:

  2. CSU-2034 Column Addition

    CSU-2034 Column Addition Description A multi-digit column addition is a formula on adding two intege ...

  3. 2018湖南多校第二场-20180407 Column Addition

    Description A multi-digit column addition is a formula on adding two integers written like this:

  4. TokuDB存储引擎

    TokuDB是Tokutek公司开发的基于ft-index(Fractal Tree Index)键值对的存储引擎. 它使用索引加快查询速度,具有高扩展性,并支持hot scheme modifica ...

  5. Servlet3.0学习总结(二)——使用注解标注过滤器(Filter)

    Servlet3.0提供@WebFilter注解将一个实现了javax.servlet.Filter接口的类定义为过滤器,这样我们在web应用中使用过滤器时,也不再需要在web.xml文件中配置过滤器 ...

  6. MariaDB glare cluster简介

    MariaDB MariaDB 是由原来 MySQL 的作者Michael Widenius创办的公司所开发的免费开源的数据库服务器,MariaDB是同一MySQL版本的二进制替代品, 当前最新版本1 ...

  7. MySQL 高性能存储引擎:TokuDB初探

    在安装MariaDB的时候了解到代替InnoDB的TokuDB,看简介非常的棒,这里对ToduDB做一个初步的整理,使用后再做更多的分享. 什么是TokuDB? 在MySQL最流行的支持全事务的引擎为 ...

  8. System.Windows.Forms

    File: winforms\Managed\System\WinForms\DataGridView.cs Project: ndp\fx\src\System.Windows.Forms.cspr ...

  9. 浅谈MariaDB Galera Cluster架构

    MariaDB          MariaDB 是由原来 MySQL 的作者Michael Widenius创办的公司所开发的免费开源的数据库服务器,MariaDB是同一MySQL版本的二进制替代品 ...

随机推荐

  1. es6 let和const

    一.let 1.let块作用域 if(true){ var a=1; let b=2; } console.log("a:"+a);//a:1 console.log(" ...

  2. JSP基础知识➣客户端请求与服务端响应(三)

    JSP客户端请求 浏览器请求服务器端,信息头的一些重要内容,在以后的网络编程中将会经常见到这些信息: Accept:指定浏览器或其他客户端可以处理的MIME类型.它的值通常为 image/png 或 ...

  3. 【bzoj4811】[Ynoi2017]由乃的OJ 树链剖分+线段树区间合并

    题解: 好像和noi那题并没有什么区别 只是加上了修改和变成树上 比较显然我们可以用树链剖分来维护

  4. nginx 限制并发访问及请求频率

    0. 1.参考 [工作]Nginx限制IP并发连接数和请求数的研究 Module ngx_http_limit_conn_module Module ngx_http_limit_req_module ...

  5. 1. ELK 之elasticsearch 简介、获取、安装

    简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Ap ...

  6. 学号:20165239 预备作业3 Linux安装及学习

    实验三 用户及文件权限管理 之前从未接触过虚拟机,借着老师布置的任务,这次寒假初次接触了虚拟机,既紧张又兴奋,在学习了老师的一部分教程以及查阅网上的资料之后,有了以下的学习笔记和心得. 一.Linux ...

  7. String str = new String("xyz") 会创建几个对象

    转载:https://blog.csdn.net/andychen314/article/details/50857313 答案是 两个对象,要理解这个,就要知道string类的工作原理.下面来慢慢分 ...

  8. Python 小程序之 恋爱表情包爬取

    虽然恋爱跟我一毛钱关系没有,,但是我还是想爬它 实验爬取网址:http://qq.yh31.com/zjbq/1491124.html # -*- coding: utf-8 -*- # @Time ...

  9. day41 mycql 函数

    一些经典的练习题,以及函数的简单用法,內建函数 -- 函数 python函数 def fun1(a1,a2,a3): sum = a1+a2+a3 return sum fun1(1,2,3) jav ...

  10. 直接引用vee-validate校验插件

    直接在页面引用vee-validate 源文件下载地址:http://www.bootcdn.cn/vee-validate/ 官方api https://baianat.github.io/vee- ...