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 (背包)的更多相关文章

  1. UVA 10306 e-Coins(全然背包: 二维限制条件)

    UVA 10306 e-Coins(全然背包: 二维限制条件) option=com_onlinejudge&Itemid=8&page=show_problem&proble ...

  2. UVA 624 - CD (01背包 + 打印物品)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  3. UVA - 11137 Ingenuous Cubrency[背包DP]

    People in Cubeland use cubic coins. Not only the unit of currency iscalled a cube but also the coins ...

  4. uva 147 Dollars(完全背包)

    题目连接:147 - Dollars 题目大意:有11种硬币, 现在输入一个金额, 输出有多少种组成方案. 解题思路:uva 674 的升级版,思路完全一样, 只要处理一下数值就可以了. #inclu ...

  5. UVA 624 CD (01背包)

    //路径记录方法:若是dp[j-value[i]]+value[i]>dp[j]说明拿了这个东西,标志为1, //for循环标志,发现是1,就打印出来,并把背包的容量减少,再在次容量中寻找标志: ...

  6. UVA 624 ---CD 01背包路径输出

    DescriptionCD You have a long drive by car ahead. You have a tape recorder, but unfortunately your b ...

  7. 紫书 习题 10-5 UVa 1213(01背包变形)

    这里就是01背包多了一维物品个数罢了 记得不能重复所以有一层循环顺序要倒着来 边界f[0][0] = 1 #include<cstdio> #include<vector> # ...

  8. UVA 624 (0 1背包 + 打印路径)

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #i ...

  9. uva 624 CD 01背包打印路径

    // 集训最终開始了.来到水题先 #include <cstdio> #include <cstring> #include <algorithm> #includ ...

随机推荐

  1. windos下安装PEAR 注意

    1.在这里下载PEAR http://pear.php.net/go-pear.phar 在页面右键另存为 go-pear.phar 到PHP的根目录,并去目录查看是否保存为了go-pear.phar ...

  2. 安装duetdisplay遇到的问题

    1.报错failed to correctly acquire vcredist_x64.exe ifle:CRC error 已经确认了 和墙有关系,通过FQ可以正常安装了. 2.在PAD屏幕上面播 ...

  3. poj 3335 /poj 3130/ poj 1474 半平面交 判断核是否存在 / poj1279 半平面交 求核的面积

    /*************** poj 3335 点序顺时针 ***************/ #include <iostream> #include <cmath> #i ...

  4. 关于 IE firefox Chrome下的通过用js 关闭窗口的一些问题

    首先IE是可以通过window.close()来关闭浏览器窗口的,但是在firefox和Chrome下是无效的,原因在于: ~~~ie可直接<button onclick="windo ...

  5. XPath与多线程爬虫

    XPath是一门在xml中查询信息的语言安装使用XPath 1.安装lxml库 window:pip install lxmllinux:sudo pip install lxml国内安装缓慢,建议到 ...

  6. Windows Phone 8初学者开发—第6部分:设置应用程序的样式

    原文 Windows Phone 8初学者开发—第6部分:设置应用程序的样式 Source Code: http://aka.ms/absbeginnerdevwp8  PDF Version: ht ...

  7. 基于visual Studio2013解决C语言竞赛题之0204实数求值

     题目

  8. 数据结构——表(list)

    #include <iostream> #include <list> using namespace std; 标准类的存储方式为双向循环链表 list类 class lis ...

  9. LoaderManager使用具体解释(三)---实现Loaders

    这篇文字将介绍Loader<D>类,而且介绍自己定义Loader的实现.这是本系列的第三篇文章. 一:Loaders之前世界 二:了解LoaderManager 三:实现Loaders 四 ...

  10. 浅析C++基础知识

    近期想对C++的面试题目进行一下更加详细的整理.事实上认真思考一下C++程序猿的面试,我们能够发现对程序猿的能力的考察总是万变不离当中,这些基础知识主要分为五部分:一. C/C++基础知识 二. C/ ...