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 ...
随机推荐
- [LeetCode]题解(python):018-4Sum
题目来源: https://leetcode.com/problems/4sum/ 题意分析: 这道题目和3Sum的题目类似,找出所有的4个数,使得这4个数等于target. 题目思路: 这道题做法和 ...
- DFS(White-Gray-Black)
参考<数据结构与算法> 本书在复杂深度优先遍历图时,采用三种颜色标记图中节点 1 white 表示未访问 2 gray 表示已经正在访问,其相邻节点 3 black 表示该节点所有的相邻节 ...
- openrisc 之 Wishbone总线学习笔记——接口信号定义
这部分内容就是copy下来的,网上到处都有.先看看接口啥样子,在详细说明 接口定义copy http://blog.csdn.net/ce123/article/details/6929897.百度文 ...
- js正则表达式的使用
正则表达式的常用字符: 符号 描述 /../ 代表一个模式的开始和结束 ^ 匹配字符串的开始 $ 匹配字符串的结束 \s 任何空白字符 \S 任何非空白字符 \d 匹配一个数字字符,等价于[1-9 ...
- Publisher/Subscriber 订阅-发布模式
Publisher/Subscriber 订阅-发布模式 本博后续将陆续整理这些年做的一些预研demo,及一些前沿技术的研究,与大家共研技术,共同进步. 关于发布订阅有很多种实现方式,下面主要介绍WC ...
- 用JS判断用户使用的是手机端还是pc端访问
最近项目中用到一个应用,当访问同一个网站地址的时候,例如:www.xxx.com的时候,如果当前客户端是pc则跳转到专注于pc的部分,如果当前客户机是手机,则跳转到专注于手机的部分,秉承一贯的习惯,b ...
- 搜狐畅游:每月给员工直系长辈2000元爱孝薪_企业新闻_265G产业频道
搜狐畅游:每月给员工直系长辈2000元爱孝薪_企业新闻_265G产业频道 搜狐畅游:每月给员工直系长辈2000元爱孝薪
- HDU4731+找规律
规律题!!! /* */ #include<algorithm> #include<iostream> #include<string.h> #include< ...
- leetcode先刷_Search in Rotated Sorted Array II
上一页下一页,找到相同的旋转阵列的问题.假设数组元素一再怎么办呢?会发生什么? 我给大家举一个极端的例子.如果是这样的阵列中的元件.1,1,2,1,1,1,1,我们想看看这个数组2,刚开始A[midd ...
- SQL Server 自学笔记
--★★★SQL语句本身区分大小写吗 --SQLServer 不区分大小写 --Oracle 默认是区分大小写的 --datetime的输入格式,2008-01-07输入进去后显示为1905-06-2 ...