http://bailian.openjudge.cn/practice/4152?lang=en_US

题解 :dp[i][j]代表前i个字符加j个加号可以得到的最小值,于是dp[i+k[j+1]可以由dp[i][j]得到。具体转移方程看代码。

   然后数字是50位所以要用高精度类。自己写了一个

坑:高精度的<和+有bug。一开始的更新方法也在乱写

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
const int N = + ; typedef long long ll;
struct BigInterger {
static const int BASE = 1e8;
static const int WIDTH = ;
vector<int> s;
BigInterger(long long num = ) { *this = num; }
BigInterger operator =(long long num) {
s.clear();
do {
s.push_back(num%BASE);
num /= BASE;
} while (num > );//num==0
return *this;
}
BigInterger operator =(const string& str) {
s.clear();
int x, len = (str.length() - ) / WIDTH + ;//len==width
for (int i = ; i < len; i++) {
int end = str.length() - i*WIDTH;
int start = max(, end - WIDTH);
sscanf(str.substr(start, end - start).c_str(), "%d", &x);
s.push_back(x);
}
return *this;
} BigInterger operator +(const BigInterger& b)const {
BigInterger c;
c.s.clear();
for (int i = , g = ;; i++) {
if (g == && i >= max(s.size(), b.s.size()))break;
int x = g;
if (i < s.size())x += s[i];
if (i < b.s.size()) x += b.s[i];
c.s.push_back(x%BASE);
g = x / BASE;
}
return c;
}
BigInterger operator +=(const BigInterger& b) {
*this = *this + b; return *this;
} bool operator<(const BigInterger& b)const {
if (s.size() != b.s.size()) return s.size() < b.s.size();
for (int i = s.size() - ; i >= ; i--) {
if (s[i] != b.s[i]) return s[i] < b.s[i];//Width 个数字一起比
}
return false;//==
}
bool operator>(const BigInterger &b)const { return b < *this; }
bool operator<=(const BigInterger &b)const { return !(b < *this); }
bool operator>=(const BigInterger &b)const { return !(*this < b); }
bool operator!=(const BigInterger &b)const { return b < *this || *this<b; }
bool operator==(const BigInterger &b)const { return!(b < *this) && !(*this<b); }
};
ostream& operator <<(ostream &out, const BigInterger& x) {
out << x.s.back();
for (int i = x.s.size() - ; i >= ; i--) {
char buf[];
sprintf(buf, "%08d", x.s[i]);
for (int j = ; j < strlen(buf); j++)cout << buf[j];
}
return out;
}
istream&operator>>(istream &in, BigInterger&x) {
string s;
if (!(in >> s))return in;
x = s;
return in;
}
BigInterger dp[N][N];//前i个数加j个加号的最小值。
int main() {
int n; BigInterger s; string s1;
string INF;
for (int i = ; i < ; i++)INF += "";
while (cin >> n) {
cin >> s1;
int len = s1.length();
s = s1;
//if (n == 0) {cout << s<<endl; continue;}
for (int i = ; i <= len; i++)
for (int j = ; j <= n; j++) dp[i][j] = INF; for (int i = ; i <= len; i++)
for (int j = ; j <= n; j++) {
if (j == ) { dp[i][j] = s1.substr(, i); }
else if (i < j + ) dp[i][j] = INF;
else {
for (int k = j; k <= i - ; k++) {
BigInterger x;
x = s1.substr(k, i - k);
dp[i][j] = min(dp[i][j], dp[k][j - ] + x);
}
}
//cout <<i<<j<<' '<< dp[i][j] << endl;
}
cout << dp[len][n] << endl; }
}

OpenJ_Bailian - 4152 最佳加法表达式 dp的更多相关文章

  1. 百练4152:最佳加法表达式(dp+高精度)

    描述 给定n个1到9的数字,要求在数字之间摆放m个加号(加号两边必须有数字),使得所得到的加法表达式的值最小,并输出该值.例如,在1234中摆放1个加号,最好的摆法就是12+34,和为36 输入有不超 ...

  2. OpenJudge 4152 最佳加法表达式

    总时间限制: 1000ms 内存限制: 65536kB 描述 给定n个1到9的数字,要求在数字之间摆放m个加号(加号两边必须有数字),使得所得到的加法表达式的值最小,并输出该值.例如,在1234中摆放 ...

  3. 最佳加法表达式(dp)

    题目描述: 有一个由1..9组成的数字串.问如果将m个加 号插入到这个数字串中,在各种可能形成的 表达式中,值最小的那个表达式的值是多少 (本题只能用于整数) 解题思路: 假定数字串长度是n,添完加号 ...

  4. 【OpenJ_Bailian - 4152 】最佳加法表达式(动态规划)

    最佳加法表达式 Descriptions: 给定n个1到9的数字,要求在数字之间摆放m个加号(加号两边必须有数字),使得所得到的加法表达式的值最小,并输出该值.例如,在1234中摆放1个加号,最好的摆 ...

  5. dp 动规 最佳加法表达式

    最佳加法表达式 有一个由1..9组成的数字串.问如果将m个加号插入到这个数字串中,在各种可能形成的表达式中,值最小的那个表达式的值是多少 解题思路 假定数字串长度是n,添完加号后,表达式的最后一个加号 ...

  6. 【动态规划】最佳加法表达式(百练oj4152)

    总时间限制: 1000ms 内存限制: 65536kB 描述 给定n个1到9的数字,要求在数字之间摆放m个加号(加号两边必须有数字),使得所得到的加法表达式的值最小,并输出该值.例如,在1234中摆放 ...

  7. 递推,动态规划(DP),字符串处理,最佳加法表达式

    看了一些资料,竟然发现连百度文库也有错误的地方,在这里吐槽一下题目大意:http://wenku.baidu.com/link?url=DrUNNm19IqpPNZjKPX4Jg6shJiK_Nho6 ...

  8. java源码——0~9十个数字不重复地使用使加法表达式成立

    这个问题是在我写个的几个博客里较为复杂的一个.首先,先看看整个问题的表述. 星号表示0~9的一个数字,而且不允许重复,使得下面的加法表达式成立.输出所有结果. ※ ※ ※ ※ ※    +  2   ...

  9. [JSOI2016] 最佳团队 (树形DP+01分数规划)

    Description JSOI信息学代表队一共有N名候选人,这些候选人从1到N编号.方便起见,JYY的编号是0号. 每个候选人都由一位编号比他小的候选人Ri推荐.如果Ri=0则说明这个候选人是JYY ...

随机推荐

  1. Nginx Session Sticky

    nginx的粘性session主要通过nginx-sticky-module实现 1 下载 nginx-sticky-module 下载地址:https://code.google.com/p/ngi ...

  2. 基本select 语句总结

    --------------基本select语句总结 8.6---------------------------------------------------------------------- ...

  3. DropDownListFor的种种纠结(禁止转载)

    严重禁止转载,好多爬虫软件为了浏览到处抓东西,真缺德 具有键“CorpType”的 ViewData 项属于类型“System.Int64”,但它必须属于类型“IEnumerable<Selec ...

  4. MongoDB中的聚合操作

    根据MongoDB的文档描述,在MongoDB的聚合操作中,有以下五个聚合命令. 其中,count.distinct和group会提供很基本的功能,至于其他的高级聚合功能(sum.average.ma ...

  5. grid网格的流动grid-auto-flow属性

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Win10 虚拟桌面

    我们可以建立多个桌面,各个桌面上运行的窗口任务互不干扰,这就是虚拟桌面 创建虚拟桌面:Win + Ctrl + D查看虚拟桌面:Win + Tab删除当前虚拟桌面:Win + Ctrl + F4切换到 ...

  7. linux实现开机自启动脚本

    Linux下(以RedHat为范本)添加开机自启动脚本有两种方法,先来简单的; 一.在/etc/rc.local中添加如果不想将脚本粘来粘去,或创建链接什么的,则:step1. 先修改好脚本,使其所有 ...

  8. PyQt4显示提示信息

    我们可以为任何窗口部件设置一个气球提示. #!/usr/bin/python # -*- coding:utf-8 -*- import sys from PyQt4 import QtGui fro ...

  9. eclipse闪退解决

    log: !SESSION 2014-03-12 14:02:45.207 -----------------------------------------------eclipse.buildId ...

  10. SQL Server 索引结构及其使用(二)

    作者:freedk 一.深入浅出理解索引结构 改善SQL语句 很多人不知道SQL语句在SQL SERVER中是如何执行的,他们担心自己所写的SQL语句会被SQL SERVER误解.比如: select ...