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,问怎样组合问题的处理顺序可以使得耗费达到最 ...
随机推荐
- git_stats安装及使用
git_stats是仓库代码统计工具,今天我们要求用git_stats工具做项目的代码统计,也是一步一坑的找到了一些方法,在这里记录一下 一.安装 git_stats可以在windows和linux使 ...
- [NUnit]No results
Results (nunit3) saved as TestResult.xmlCommitting...No results, this could be for a number of reaso ...
- 定时器任务django-crontab的使用【静态化高频率页面,增加用户体验】【系统的定时器,独立于项目执行】【刘新宇】
页面静态化 思考: 网页的首页访问频繁,而且查询数据量大,其中还有大量的循环处理. 问题: 用户访问首页会耗费服务器大量的资源,并且响应数据的效率会大大降低. 解决: 页面静态化 1. 页面静态化介绍 ...
- vscode c 语言 win10
在看 CSAPP 一些课程,一些c 语言的小程序的例子,想跑起来试试,用一个DEV c++ 简单上手,但这是一个上古的IDE, 前端开发中的代码不全,语法高亮,都不太好,就想着为什么不折腾一下 V ...
- CSS3:pointer-events | a标签禁用
用纯css就能实现取消事件响应的方法,pointer-events,使用起来更加简单,它可以: pointer-events: auto | none | visiblePainted | visib ...
- 11个rsync使用实例
rsync表示 remote sync,其用于在本地或与远程主机间进行文件或目录备份.相比较scp等工具,rsync有以下优点: 速度:除首次全拷贝外,其他时候实现增量拷贝,加快传输速度 安全:传输数 ...
- 通过注解实现通用导出Excel
Javaweb开发中数据的导入导出很常见,每次我们都需要写很多代码,所以我就在想能不能写一些通用的方法,之前已经在网上 整理过一些通用的方法,最近在网上看到一位牛人封装的更加简介,自己拿过来整理了一下 ...
- python匿名函数的介绍及用途
匿名函数 用lambda能够创建一个匿名函数,这中函数得名于省略了用def声明函数的标准步骤. 语法 lambda [arg1 [,arg2,.....argn]]:expression 如何使用 我 ...
- 深度搜索(dfs)+典型例题(八皇后)
深度优先搜索简称深搜,从起点出发,走过的点要做标记,发现有没走过的点,就随意挑一个往前走,走不了就回退,此种路径搜索策略就称为“深度优先搜索”,简称“深搜”. 如上面的图所示:加入我们要找一个从V0到 ...
- 使用PowerShell 测试DNS
运行环境:Windows Server 2012 R2 获取服务器DNS命令,下面的仅获取一个dns (nslookup sql.ciras.com)[1].split(':')[1].trim() ...