Column Addition~DP(脑子抽了,当时没有想到)
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 这题就是给你一个竖式,然后看去除多少列,能使这个竖式正确。
这题其实很好写,唉 ,DP写少了, 其实这个是个很经典的DP
求最长上升子序列的变形,思想一样,只是判断条件不同而已。
这么裸的DP换个样子我就认不出了。菜是原罪啊!!!! 注意
a[i]+b[i]-10==c[i] 只在这个条件下更新ans的值是有原因的,
因为你如果此时存在进位的情况 你并不能判断他到底是不是该去还是留。
求出最长的对的式子,用n-ans答案就出来了
我觉得我要开始我的基础DP训练了 , 这个真的不能再拖了
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
const int maxn =;
int a[maxn],b[maxn],c[maxn],dp[maxn],k[maxn];
int n;
int main() {
//freopen("DATA.txt","r",stdin );
while(scanf("%d",&n),n){
memset(dp,,sizeof(dp));
memset(k,,sizeof(k));
for (int i= ;i<n ;i++ )
scanf("%1d",&a[i]);
for (int i= ;i<n ;i++ )
scanf("%1d",&b[i]);
for (int i= ;i<n ;i++ )
scanf("%1d",&c[i]);
int ans=;
for (int i=n- ;i>= ;i--) {
if (a[i]+b[i]==c[i]) {
dp[i]=;
k[i]=;
if (dp[i]>ans) ans=dp[i];
}
if (a[i]+b[i]-==c[i]) {
dp[i]=;
k[i]=;
}
for (int j=n- ;j>i ;j--) {
if (a[i]+b[i]+k[j]==c[i] && dp[i]<dp[j]+) {
k[i]=;
dp[i]=dp[j]+;
if (dp[i]>ans) ans=dp[i];
}
if (a[i]+b[i]+k[j]-==c[i] && dp[i]<dp[j]+ ) {
k[i]=;
dp[i]=dp[j]+;
}
}
}
printf("%d\n",n-ans);
}
return ;
}
Column Addition~DP(脑子抽了,当时没有想到)的更多相关文章
- 【动态规划】Column Addition @ICPC2017Tehran/upcexam5434
时间限制: 1 Sec 内存限制: 128 MB 题目描述 A multi-digit column addition is a formula on adding two integers writ ...
- CSU-2034 Column Addition
CSU-2034 Column Addition Description A multi-digit column addition is a formula on adding two intege ...
- 2018湖南多校第二场-20180407 Column Addition
Description A multi-digit column addition is a formula on adding two integers written like this:
- leetcode修炼之路——387. First Unique Character in a String
最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...
- CQOI2021 退役记
Day -1 晚上去了酒店然后就睡觉了. Day 1 进考场之前互相奶. 进了考场之后看题,发现T1很水(伏笔1,然后直接开始写 \(\Theta(n\log^2n)\)(二分+动态开点线段树),调了 ...
- Fzu2109 Mountain Number 数位dp
Accept: 189 Submit: 461Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description One ...
- poj 1088 滑雪(区间dp+记忆化搜索)
题目链接:http://poj.org/problem?id=1088 思路分析: 1>状态定义:状态dp[i][j]表示在位置map[i][j]可以滑雪的最长区域长度: 2>状态转移方程 ...
- CH0103 最短Hamilton路径 dp
正解:状压dp 解题报告: 完了吃枣退役:D 我是真的没想到这是个dp...脑子越来越不好了,大概是太久没碰OI了都要生疏了...哭了,感觉自己太傻逼了可能不适合学信息... 知道是个状压dp就eas ...
- bzoj 1630: [Usaco2007 Demo]Ant Counting【dp】
满脑子组合数学,根本没想到dp 设f[i][j]为前i只蚂蚁,选出j只的方案数,初始状态为f[0][0]=1 转移为 \[ f[i][j]=\sum_{k=0}^{a[i]}f[i-1][j-k] \ ...
随机推荐
- 【BZOJ3529】【SDOI2014】数表
Time Limit: 1000 ms Memory Limit: 512 MB Description 有一张n×m的数表,其第i行第j列 (1≤i≤n,1≤j≤m)的数值为能同时整除i和j的所有自 ...
- Problem : (1.2.1) Text Reverse
#include<iostream> using namespace std; void main() { char arr[1000]; int a,n; int s,t; cin> ...
- 负载均衡,会话保持,session同步(转)
转自:http://bbs.linuxtone.org/thread-18212-1-1.html 一,什么负载均衡一个新网站是不要做负载均衡的,因为访问量不大,流量也不大,所以没有必要搞这些东西.但 ...
- WordPress中添加自定义评论表情包的方法
先来看看效果: 现在由于WordPress版本更新,再加上WordPress主题也越来越多,而现在的主题一般都是禁用了WordPress自带的评论表情,其实自带 的评论表情也是很丑的,但是以前我们可以 ...
- 教你如何前后端完全分离(非api、ajax)
我的前后分离,不是api,不是ajax,我这里只讨论html与后端结合 前话 曾经风靡一时的dedecms相信做网站的十有八.九都知道,还有那么一些不是技术出生的人,通过看一下文档,也能访问出网站出来 ...
- python作业03-文件操作&函数
一.文件处理相关 1.编码问题 (1)请说明python2 与python3中的默认编码是什么?答:Python2默认的字符编码是ASCII,默认的文件编码也是ASCII :python3默认的字符编 ...
- webpack安装入门
这两天心血来潮,突然想研究下webpack,花了一天时间,看网上有很多教程,不过发现安装的都是比较老的版本.后来发现还是我太年轻了,找技术播客还不如找官方网站.下面是一个webpack的官方网站,里面 ...
- dataTable插件的使用
var table; table = $("#example").dataTable({ "sDom": "<'top'f<'clear' ...
- Tomact高并发&Servlet线程处理
Servlet/JSP技术和ASP.PHP等相比,由于其多线程运行而具有很高的执行效率.由于Servlet/JSP默认是以多线程模式执行的,所以,在编写代码时需要非常细致地考虑多线程的安全性问题.然而 ...
- 国内可用的Internet时间同步服务器地址(NTP时间服务器)
不知道什么鬼我这系统自带的Internet时间同步服务器地址居然不可用,终端ping系统自带服务器两个居然都不通???难道时间服务器也和谐么? 好在阿里云提供了7个NTP时间服务器也就是Interne ...