2018湖南多校第二场-20180407 Column Addition
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
Hint
Source
ATRC2017
开始是用贪心写的,总感觉自己没错,贪心的判断写了一层又一层最后还是错了。
结束后看了学长的代码,用dp写的,顿时有种恍然大悟的感觉,考试的时候钻进死胡同了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
#define debug(a) cout << #a << ": " << a << endl
int main() {
int n;
while( cin >> n ) {
if( !n ) {
break;
}
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
int a[], b[], c[];
for( int i = ; i < n; i ++ ) {
a[i] = s1[i] - '';
b[i] = s2[i] - '';
c[i] = s3[i] - '';
}
int dp[];
memset( dp, , sizeof(dp) );
for( int i = n - ; i >= ; i -- ) {
if( ( a[i] + b[i] ) % == c[i] ) { //如果当前直接或者进位后间接满足就置为1
dp[i] = ;
}
for( int j = i + ; j < n; j ++ ) {
int jinwei = ( dp[j] ) && ( a[j] + b[j] > c[j] ); //后面是否有可以进位的
if( ( a[i] + b[i] + jinwei ) % == c[i] ) {
dp[i] = max( dp[i], dp[j] + ); //有的话就更新i位置的dp值
}
}
}
int ans = n;
for( int i = ; i < n; i ++ ) {
if( a[i] + b[i] <= c[i] ) {
ans = min( ans, n - dp[i] );
}
}
cout << ans << endl;
}
return ;
}
2018湖南多校第二场-20180407 Column Addition的更多相关文章
- 2018湖南多校第二场-20180407 Barareh on Fire
Description The Barareh village is on fire due to the attack of the virtual enemy. Several places ar ...
- 2019牛客多校第二场 A Eddy Walker(概率推公式)
2019牛客多校第二场 A Eddy Walker(概率推公式) 传送门:https://ac.nowcoder.com/acm/contest/882/A 题意: 给你一个长度为n的环,标号从0~n ...
- 2018 Multi-University Training Contest 2 杭电多校第二场
开始逐渐习惯被多校虐orz 菜是原罪 1004 Game (hdoj 6312) 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6312 虽然披着 ...
- 2019年湖南多校第一场||2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)
第一场多校就打的这么惨,只能说自己太菜了,还需继续努力啊- 题目链接: GYM链接:https://codeforces.com/gym/101933 CSU链接:http://acm.csu.edu ...
- hdu6312 2018杭电多校第二场 1004 D Game 博弈
Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 2018牛客多校第二场a题
一个人可以走一步或者跳x步,但不能连着跳,问到这个区间里有几种走法 考虑两种状态 对于这一点,我可以走过来,前面是怎么样的我不用管,也可以跳过来但是,跳过来必须保证前一步是走的 dp[i][0]表示 ...
- 2018杭电多校第二场1003(DFS,欧拉回路)
#include<bits/stdc++.h>using namespace std;int n,m;int x,y;int num,cnt;int degree[100007],vis[ ...
- 2019 湖南多校第一场(2018~2019NCPC) 题解
解题过程 开场shl过B,C,然后lfw写J,J WA了以后shl写A,但是因为OJ上空间开小WA了,而不是MLE?,J加了特判过了.之后一直在检查A错哪了,直到qt发现问题改了空间,浪费许多时间,但 ...
- 2014多校第二场1011 || HDU 4882 ZCC Loves Codefires (贪心)
题目链接 题意 : 给出n个问题,每个问题有两个参数,一个ei(所要耗费的时间),一个ki(能得到的score).每道问题需要耗费:(当前耗费的时间)*ki,问怎样组合问题的处理顺序可以使得耗费达到最 ...
随机推荐
- apache bench的简单使用
ApacheBench是 Apache 附带的一个小工具,专门用于 HTTP Server 的benchmark testing,可以同时模拟多个并发请求. 需要针对web做压力测试,所以简单学习了一 ...
- middleware中间件
django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在django项目的se ...
- 记录eclipse中文出现空格宽度不一致的bug
起因 不久前更新了 eclipse(2019-03) 版本:突然发现出现了,使用注释使用中出现的空格的间隔大小不一致的问题,具体可以看下图: 遇到这种问题简直逼不能忍,在网上搜一下解决方式: 谷歌 搜 ...
- 【精选】Markdown 语法汇总
博客园也能Markdown?美滋滋,Markdown真的是好用QAQ. 本文档按照Markdown各种常用语法类别,以文字描述+演示的方式来展现markdown语法的使用.Markdown 的目标是实 ...
- JVM解剖乐园
1.JVM锁粗化和循环原文标题:JVM Anatomy Quark #1: Lock Coarsening and Loops 众所周知Hotsport编译器会进行JVM锁粗化和优化,它将相邻的锁区块 ...
- HTML5 Device Access (设备访问)
camera api (含图片预览) 参考地址 主要为利用input type=file, accept="image/*" 进行处理 图片预览方式(两种) const file ...
- JAVA-SpringMVC 概述及组件介绍
一.SpringMVC概述 SpringMVC是一个WEB层.控制层框架,主要用来负责与客户端交互,业务逻辑的调用. SpringMVC是Spring家族中的一大组件,Spring整合SpringMV ...
- Windows Server 2008利用NTFS管理数据
今天我们学习关于NTFS管理数据 以下是学习的内容NTFS分区和FAT32分区的区别,如何将FAT32分区转化成NTFS分区,FAT 32 不支持大于4G ,NTFS权限设置 ,EFS加密 ,文件夹的 ...
- ubuntu 输出 log 基础
自定义日志文件 nohup your_command > my_nohup.log 2>&1 & #(将日志输出在my_nohup.log文件中,并将stderr重定向至s ...
- 一、Ansible入门篇
一.Ansible简介 Ansible是一个自动化运维的工具 基于python语言编写,因此机器需要具备python环境. 通过ssh的连接方式进行自动化部署,ansible优先使用OpenSSH,在 ...