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. Jackson Gson Json.simple part 2

    这篇blog介绍 Jackson 的特点和使用方法 Jackson支持三种使用方法 流API(streaming api Incremental parsing/generation) JsonPar ...

  2. GreenPlum数据加载

    1. copy命令 对于数据加载,GreenPlum数据库提供copy工具,copy工具源于PostgreSQL数据库,copy命令支持文件与表之间的数据加载和表对文件的数据卸载.使用copy命令进行 ...

  3. java的代理和动态代理简单测试

    什么叫代理与动态代理? 1.以买火车票多的生活实例说明. 因为天天调bug所以我没有时间去火车票,然后就给火车票代理商打电话订票,然后代理商就去火车站给我买票.就这么理解,需要我做的事情,代理商帮我办 ...

  4. redis资料

    http://snowolf.iteye.com/blog/1630697  征服redis配置 http://redis.readthedocs.org/en/latest/  redis命令参考 ...

  5. Qt打包部署程序自动查找依赖DLL工具windeployqt

    qt编译好一个exe程序之后,部署到一台没有开发环境的机器上,需要一起拷贝其依赖的dll文件.这时需要一个windeployqt工具来帮忙,因为手动拷贝的话容易遗漏. https://blog.csd ...

  6. 在 Core Data 中存取 transformable 类型的数据

    本文转载至 http://imenjoe.com/2015/04/10/CoreData-transformable-20150410/ 在开发过程中有一个需要在 Core Data 中存取 NSDi ...

  7. 《转载》为什么我要在2018年学习Python?

    本文转载自36kr 从网页编程到时髦的人工智能,机器学习,这个享有"瑞士军刀(万能工具)"盛誉的Python语言, 你学会了吗? 编者注: 根据维基百科的解释,"Pyth ...

  8. apache使某目录下的文件能够列表显示出来

    想要使web目录下,某目录下的文件列表显示而不是显示"You don't have permission to access / on this server" 需要在httpd. ...

  9. canvas - drawImage()方法绘制图片不显示的问题

    canvas有个很强大的api是drawImage()(w3c): 他的主要功能就是绘制图片.视频,甚至其他画布等.   问题: 慕名赶来,却一脚踩空,低头一看,地上一个大坑. 事情是这样的,在我看完 ...

  10. iframe内点击a标签禁止滚动到顶部

    在iframe内加载的表中,操作下的按钮用a标签布局,但是会出现一个非常不好的体验,就是当页面有滚动条的时候,点击a标签,列表会自动滚动到顶部. 首先看我的a标签: <a href=" ...