时间限制: 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. Windows Azure 搭建网络代理 Proxy

    额 题目起的有点大 其实就是在 Linux 上使用代理 不过是用的 Azure 上的 Liunx 虚拟机而已 如何在 Azure 上搭建 VPN 见上篇:http://www.cnblogs.com/ ...

  2. java 环境安装

    因为现在的java安装需要点击同意许可才能安装,造成了在linux中使用wget命令下载无法正常完整下载,即便下载下来也是不完整的 安装会提示 no such file等一堆提示 我们使用参数如下的命 ...

  3. centos 6.5升级内核到3.1

    1.查看本机内核版本 [root@localhost ~]# uname -r 2.6.32-358.el6.x86_64 2.安装含有内核软件的源 步骤一:导入证书 [root@localhost ...

  4. 分布式一致性算法——paxos

    一.什么是paxos算法 Paxos 算法是分布式一致性算法用来解决一个分布式系统如何就某个值(决议)达成一致的问题. 人们在理解paxos算法是会遇到一些困境,那么接下来,我们带着以下几个问题来学习 ...

  5. Flink的Windows

    在讲解windows的众多操作之前,需要讲解一个概念: 源源不断的数据流是无法进行统计工作的,因为数据流没有边界,就无法统计到底有多少数据经过了这个流.也无法统计数据流中的最大值,最小值,平均值,累加 ...

  6. mysql配置为半同步复制

    mysql 半同步插件是由谷歌提供,具体位置/usr/local/mysql/lib/plugin/下,一个是 master用的 semisync_master.so,一个是 slave 用的 sem ...

  7. net core体系-web应用程序-4asp.net core2.0 项目实战(1)-3项目架构说明

    本文目录1. 摘要2. 框架介绍 3. 权限管理之多一点说明4. 总结 1.  摘要 NCMVC角色权限管理框架是由最近练习Net Core时抽时间整理的系统,后续能不能发展成一个cms还要看朋友们是 ...

  8. 011 SpringSecurity的基本原理

    一:securuty默认情况 1.默认的配置 在引用security依赖以后,会有一个配置 security.basic.enabled=true 2.启动 用户名:user 密码:在控制台上查看 3 ...

  9. 010 异步处理Rest服务

    一:任务 1.任务 使用Runnable异步处理Rest服务 使用DefaultResult异步处理Rest服务 异步处理的配置 2.原理图说明 二:Callable进行异步处理 1.程序 新建一个a ...

  10. 063 SparkStream数据接收方式

    1.两种方式 2.Basic Source 由StreamingContext可以提供的API 上面做的wordcount中的方式就算是第一种方式. 3.Advanced Source 使用数据接收器 ...