时间限制: 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;
}

最新文章

  1. 如何定位Oracle数据库被锁阻塞会话的根源
  2. C#运算符
  3. Cursors in MySQL Stored Procedures
  4. Buzz words
  5. 开发Portlet第二步:如何将Crystal静态Portlet转变成基于测试数据的动态Portlet?
  6. Java 多线程间的通讯
  7. jQuery学习笔记(5)--表单域获得焦点和失去焦点样式变化
  8. [Prism框架实用分享]如何在主程序中合理的弹出子窗体
  9. BZOJ_3527_[ZJOI2014]_力_(FFT+卷积)
  10. HOG特征
  11. Android ScrollView 嵌套 ListView、 ListView 嵌套ScrollView Scroll事件冲突解决办法
  12. poj2013---二维数组指针使用
  13. Loda Button
  14. 高性能的代理服务-Envoy
  15. Vim 插键及配置
  16. eclipse 汉化详细方法
  17. 微软的开源Sonar工具测试网站的性能和安全性
  18. TF 设置GPU模式训练
  19. Mysql 中日期类型bigint和datetime互转
  20. UVALive 6913 I Want That Cake 博弈+dp

热门文章

  1. [转] babel入门基础
  2. awk介绍
  3. k8s 廖老师的分享
  4. HDU1814 Peaceful Commission 2-sat
  5. 关于用舞蹈链DLX算法求解数独的解析
  6. scrapy下载图片报[scrapy.downloadermiddlewares.robotstxt] DEBUG: Forbidden by robots.txt:错误
  7. day 68 django 之api操作 | jQueryset集合与对象
  8. css 其他
  9. Nowcoder contest 370F Rinne Loves Edges (简单树形DP) || 【最大流】(模板)
  10. 李宏毅机器学习笔记4:Brief Introduction of Deep Learning、Backpropagation(后向传播算法)