UVA 10317 - Equating Equations (背包)
Problem F
Equating Equations
Input: standard input
Output: standard output
Time Limit: 6 seconds
Memory Limit: 32 MB
Read equations with up to 16 terms and + and – operators (not unary) and reorder the terms (but not the operators) so that the equations hold. For example
1 + 2 = 4 - 5 + 6
is not a correct equation but the terms can be rearranged thus so it is:
6 + 2 = 4 - 1 + 5
Input
Standard input consists of several pseudo-equations, one per line. Each pseudo-equation has up to 16 integer terms and each term is less than 100. Adjacent terms are separated by one operator, and spaces always appear surrounding the terms. There is exactly one = operator in the equation
Output
Your output will consist of one line per input line, with the same operators and the same terms. The order of the operators must be preserved, but the terms can be reordered so the equation holds. Any ordering such that the equation holds is correct. If there is more than one ordering any one of the orderings will do. If no ordering exists, a line containing
no solution
should be printed. One space should appear on either side of each operator and there should be no other spaces.
Sample Input
1 + 2 = 4 - 5 + 6
1 + 5 = 6 + 7
Sample Output
6 + 2 = 4 - 1 + 5
no solution
(The Decider Contest, Source: Waterloo ACM Programming Contest)
题意:给一个式子,符号位置不能变,数字位置可以变,求转换成正确等式。
思路:由于只有加减,所以左边减号相当于右边加号,右边加号相当于左边减号,如此一来左右两边相等,为sum / 2, 然后去背包求sum / 2 的组成方法,然后按加减输出即可。
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; const int N = 20; int num[N], numn, opern, Min, sum, jian, jia, jianz, jiaz, dp[N][N * 100], path[N][N * 100][N], pathn[N][N * 100], path2[N], pathn2, vis[105];;
char oper[N], c, str[105]; void init() {
int Num = 0, flag = 0;
numn = opern = Min = sum = jian = jia = jianz = jiaz = 0;
for (int i = 0; i <= strlen(str); i ++) {
if (str[i] == '+' || str[i] == '=' || str[i] == '-') {
if (str[i] == '+') {
jiaz ++;
jia ++;
}
if (str[i] == '-') {
jian ++;
jianz ++;
}
if (str[i] == '=') {
jia = 0;
jian = 0;
}
oper[opern++] = str[i];
}
else if (str[i] >= '0' && str[i] <= '9') {
Num = Num * 10 + (str[i] - '0');
flag = 0;
}
else {
if (flag == 0) {
num[numn++] = Num;
sum += Num;
Num = 0;
flag = 1;
}
}
}
Min = jiaz - jia + jian + 1;
} void DP() {
memset(dp, 0, sizeof(dp));
memset(pathn, 0, sizeof(pathn));
dp[0][0] = 1;
for (int k = 0; k < numn; k ++) {
for (int i = Min; i >= 1; i --) {
for (int j = sum; j >= num[k]; j --) {
if (dp[i - 1][j - num[k]] == 1) {
dp[i][j] = 1;
for (int l = 0; l < i - 1; l ++)
path[i][j][l] = path[i - 1][j - num[k]][l];
path[i][j][i - 1] = num[k]; pathn[i][j] = i;
}
}
}
}
} void print() {
pathn2 = 0;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < numn; i ++)
vis[num[i]] ++;
for (int i = 0; i < pathn[Min][sum]; i ++) {
vis[path[Min][sum][i]] --;
}
for (int i = 0; i < numn; i ++) {
if (vis[num[i]]) {
path2[pathn2++] = num[i];
vis[num[i]] --;
}
}
printf("%d", path[Min][sum][0]);
int flag = 0; int s1 = 1, s2 = 0;
for (int i = 0; i < opern; i ++) {
char c = oper[i];
if (flag == 0) {
if (c == '+')
printf(" %c %d", c, path[Min][sum][s1++]);
if (c == '-')
printf(" %c %d", c, path2[s2++]);
if (c == '=') {
flag = 1;
printf(" %c %d", c, path2[s2++]);
}
}
else {
if (c == '+')
printf(" %c %d", c, path2[s2++]);
if (c == '-')
printf(" %c %d", c, path[Min][sum][s1++]);
}
}
printf("\n");
} int main() {
while (gets(str) != NULL) {
init();
if (sum % 2) printf("no solution\n");
else {
sum /= 2;
DP();
if (dp[Min][sum])
print();
else printf("no solution\n");
}
}
return 0;
}
UVA 10317 - Equating Equations (背包)的更多相关文章
- UVA 10306 e-Coins(全然背包: 二维限制条件)
UVA 10306 e-Coins(全然背包: 二维限制条件) option=com_onlinejudge&Itemid=8&page=show_problem&proble ...
- UVA 624 - CD (01背包 + 打印物品)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA - 11137 Ingenuous Cubrency[背包DP]
People in Cubeland use cubic coins. Not only the unit of currency iscalled a cube but also the coins ...
- uva 147 Dollars(完全背包)
题目连接:147 - Dollars 题目大意:有11种硬币, 现在输入一个金额, 输出有多少种组成方案. 解题思路:uva 674 的升级版,思路完全一样, 只要处理一下数值就可以了. #inclu ...
- UVA 624 CD (01背包)
//路径记录方法:若是dp[j-value[i]]+value[i]>dp[j]说明拿了这个东西,标志为1, //for循环标志,发现是1,就打印出来,并把背包的容量减少,再在次容量中寻找标志: ...
- UVA 624 ---CD 01背包路径输出
DescriptionCD You have a long drive by car ahead. You have a tape recorder, but unfortunately your b ...
- 紫书 习题 10-5 UVa 1213(01背包变形)
这里就是01背包多了一维物品个数罢了 记得不能重复所以有一层循环顺序要倒着来 边界f[0][0] = 1 #include<cstdio> #include<vector> # ...
- UVA 624 (0 1背包 + 打印路径)
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #i ...
- uva 624 CD 01背包打印路径
// 集训最终開始了.来到水题先 #include <cstdio> #include <cstring> #include <algorithm> #includ ...
随机推荐
- wdcp centos6.5多网站部署
一.wdcp介绍 wdCP是WDlinux Control Panel的简称,是一套通过WEB控制和管理服务器的Linux服务器管理系统以及虚拟主机管理系统,旨在易于使用Linux系统做为我们的网站服 ...
- PHP 导入excel
db.php为数据库操作类, $config为数据库配置,PHPExcel版本为PHPExcel_1.8.0, PHP代码: header("Content-type:text/html; ...
- Code 49 码
Code 49码是一种多层.连续型.可变长度的条码符号,它可以表示全部的128个ASCII字符.每个Code 49条码符号由2到8层组成,每层有18个条和17个空.层与层之间由一个层分隔条分开.每层包 ...
- 动态弹球的实现 加入了多线程技术--javaSE游戏准备工作
任务描述:实现了动态弹球的功能,对于有弹球功能的SE游戏奠定了基础. package 运用线程技术的小球; import java.awt.*; import java.awt.event.*; im ...
- ShareSDK for Android 只有新浪微博分享
本文代码例子:http://pan.baidu.com/share/link?shareid=3710053477&uk=3189484501 ShareSDK 官方的例子非常好,但代码太多看 ...
- Android的AsyncTask类的解读
国庆节放假.搞了半个月都没有上班了,coding的时候一点都不在状态,本来这篇文章是在国庆节前写完的,可是由于自己的懒 惰,导致延期到国庆节,哎,这种习惯真心不好呀...不多说了以下来进入正题 之前我 ...
- 用了好多年的XP换成了Win7
因为懒,所以工作的笔记本XP一直没有换 网卡故障被逼无奈正好升成Win7 顺便看了一下市场占有率,Win7已经百分之四十多了,去年就超过XP了 不过XP也够持久的了,都十二年了,有这样长寿的产品对哪家 ...
- iOS 8 强制横屏
最近用到视频播放功能:(Vitamio, 注:在Build Setting 里面的 Other Link Flag 添加-all_load) iOS 8的屏幕旋转比较坑, 使用以下代码可以强制旋转 - ...
- 一天一个类 --- StringTokenizer
这是一个将字符串按照指定的delimiters(分隔符)进行分割的类. 首先看看他的构造函数: public StringTokenizer(String str, String delim, boo ...
- 面向对象程序设计-C++ Class & Object & Friend Function & Constructor & Destructor【第五次上课笔记】
大家可以下载后用Vim 或者 Sublime Text等文本编辑器查看 以下代码均已折叠,点击“+“即可打开 一开始老师用C语言大作业的例子,写了个 Student 的结构以及相关操作 #includ ...