OpenJ_Bailian - 4152 最佳加法表达式 dp
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的更多相关文章
- 百练4152:最佳加法表达式(dp+高精度)
描述 给定n个1到9的数字,要求在数字之间摆放m个加号(加号两边必须有数字),使得所得到的加法表达式的值最小,并输出该值.例如,在1234中摆放1个加号,最好的摆法就是12+34,和为36 输入有不超 ...
- OpenJudge 4152 最佳加法表达式
总时间限制: 1000ms 内存限制: 65536kB 描述 给定n个1到9的数字,要求在数字之间摆放m个加号(加号两边必须有数字),使得所得到的加法表达式的值最小,并输出该值.例如,在1234中摆放 ...
- 最佳加法表达式(dp)
题目描述: 有一个由1..9组成的数字串.问如果将m个加 号插入到这个数字串中,在各种可能形成的 表达式中,值最小的那个表达式的值是多少 (本题只能用于整数) 解题思路: 假定数字串长度是n,添完加号 ...
- 【OpenJ_Bailian - 4152 】最佳加法表达式(动态规划)
最佳加法表达式 Descriptions: 给定n个1到9的数字,要求在数字之间摆放m个加号(加号两边必须有数字),使得所得到的加法表达式的值最小,并输出该值.例如,在1234中摆放1个加号,最好的摆 ...
- dp 动规 最佳加法表达式
最佳加法表达式 有一个由1..9组成的数字串.问如果将m个加号插入到这个数字串中,在各种可能形成的表达式中,值最小的那个表达式的值是多少 解题思路 假定数字串长度是n,添完加号后,表达式的最后一个加号 ...
- 【动态规划】最佳加法表达式(百练oj4152)
总时间限制: 1000ms 内存限制: 65536kB 描述 给定n个1到9的数字,要求在数字之间摆放m个加号(加号两边必须有数字),使得所得到的加法表达式的值最小,并输出该值.例如,在1234中摆放 ...
- 递推,动态规划(DP),字符串处理,最佳加法表达式
看了一些资料,竟然发现连百度文库也有错误的地方,在这里吐槽一下题目大意:http://wenku.baidu.com/link?url=DrUNNm19IqpPNZjKPX4Jg6shJiK_Nho6 ...
- java源码——0~9十个数字不重复地使用使加法表达式成立
这个问题是在我写个的几个博客里较为复杂的一个.首先,先看看整个问题的表述. 星号表示0~9的一个数字,而且不允许重复,使得下面的加法表达式成立.输出所有结果. ※ ※ ※ ※ ※ + 2 ...
- [JSOI2016] 最佳团队 (树形DP+01分数规划)
Description JSOI信息学代表队一共有N名候选人,这些候选人从1到N编号.方便起见,JYY的编号是0号. 每个候选人都由一位编号比他小的候选人Ri推荐.如果Ri=0则说明这个候选人是JYY ...
随机推荐
- How to solve the problem : "You have been logged on with a temporary profile"
/*By Jiangong SUN*/ I've encountered a problem in one server, which is : Every time I login into the ...
- Java API方式调用Kafka各种协议
众所周知,Kafka自己实现了一套二进制协议(binary protocol)用于各种功能的实现,比如发送消息,获取消息,提交位移以及创建topic等.具体协议规范参见:Kafka协议 这套协议的具 ...
- linux下getsockopt和setsockopt详解及测试
linux下getsockopt和setsockopt详解及测试 NAME 名字 getsockopt, setsockopt - get and set options on sockets 获取或 ...
- Windows下POSIX线程编程(pThread)环境搭建
系统: Windows 编辑器:codeblocks13.12 1. 简介: Windows有一个叫 POSIX Threads for Win32 的开源项目给出了一个功能比较完善的Windows下 ...
- vc下项目的头文件包含目录以及库导入预计库目录设置
1.包含目录:include 头文件包含目录设置: project->setting->C/C++->常规: Additional include directories(附加包含目 ...
- Kindeditor问题
1.初始的时候,拿到切图,kindeditor不知道为什么就是显示不出来,只出来个文本框一样的东西 原因:样式未加载,这个是因为美工那边样式重调了,而且新项目并没有将整个插件拷贝过来,而只是拿了kin ...
- c# MVC Take的使用
Take的使用 myPicture = dbContext.MyPictures.Where(u => u.Width == request.Width && u.Height ...
- SNAT DNAT MASQUERADE 区别
SNAT,DNAT,MASQUERADE都是NATMASQUERADE是SNAT的一个特例SNAT是指在数据包从网卡发送出去的时候,把数据包中的源地址部分替换为指定的IP,这样,接收方就认为数据包的来 ...
- chrome 安装页面编码选择插件
https://chrome.google.com/webstore/detail/set-character-encoding/bpojelgakakmcfmjfilgdlmhefphglae se ...
- 【大数据系列】节点的退役和服役[datanode,yarn]
一.datanode添加新节点 1 在dfs.include文件中包含新节点名称,该文件在名称节点的本地目录下 [白名单] [s201:/soft/hadoop/etc/hadoop/dfs.incl ...