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,问怎样组合问题的处理顺序可以使得耗费达到最 ...
随机推荐
- C++ protobuffer 前后端通信 简单应用
后端发送多个protobuffer消息到前端,前端用socket监听,如何区分消息类型呢? //定义心跳包 DseHeartbeat _DseHeartbeat; DseHeartbeat _DseH ...
- golang文档、中文、学习文档
Golang中文文档地址 http://zh-golang.appspot.com/doc/ Golang非英文文档地址: https://github.com/golang/go/wiki/NonE ...
- 请使用switch语句和if...else语句,计算2008年8月8日这一天,是该年中的第几天。
请使用switch语句和if...else语句,计算2008年8月8日这一天,是该年中的第几天. #include <stdio.h> int main() { /* 定义需要计算的日期 ...
- drf之序列化
在django视图中使用serializer 只是使用serializer类编写API视图,没有用到REST框架 app01下的models.py from django.db import mode ...
- Linux - 查看端口的占用情况、找出并杀死占用进程的方法
目录 1 lsof查看端口的占用情况 1.1 命令使用示例 1.2 查看某一端口的占用情况 1.3 杀死某个端口的所有进程 2 netstat查看端口占用情况 2.1 命令使用示例 2.2 查看占用某 ...
- ASP.NET Web项目发布选项:“允许更新此预编译站点” 详解
目录 #使用visual studio 发布web项目 #"允许更新此预编译站点" 选项的意义 1.选中 "允许更新此预编译站点" 2.不选中 "允许 ...
- IE浏览器主页被篡改为2345,针对一般解决办法无法解决的情况
1.注册表修改 按微软键+R 输入regedit 弹出注册表.HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN 将右侧的Sta ...
- eclipse解决properties文件中文乱码(两种方试)
第一种:大多数网上搜到的情况(不靠谱) 第一步:windows-->properties-->General-->Content Types-->text(如下图) 第二步:p ...
- 算法与数据结构基础 - 二叉树(Binary Tree)
二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如Leet ...
- Struts1.x 跨站脚本(XSS)漏洞的解决
一. 演示XSS 当访问一个不存在的网址时,例如[url]http://localhost:8080/demo/noAction.do[/url],那么Struts处理后都会跳到提示“Invali ...