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. 使用PHP搭建自己的MVC框架

    一.什么是MVC MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller ...

  2. GIT简易使用流程

    git是目前世界上最先进的分布式版本控制系统(摘自廖雪峰官网) 首先需要在系统上安装git: Windows系统在这下载: RHEL/Centos/Fedora用户可以用以下命令安装:yum -y i ...

  3. linux 内核分析之list_head

    转自:http://www.cnblogs.com/riky/archive/2006/12/28/606242.html 一.链表数据结构简介 链表是一种常用的组织有序数据的数据结构,它通过指针将一 ...

  4. Windows Phone 8初学者开发—第5部分:布局和事件基础

    原文 Windows Phone 8初学者开发—第5部分:布局和事件基础 原文地址: http://channel9.msdn.com/Series/Windows-Phone-8-Developme ...

  5. 统计单词频率--map

    问题描述: 输入一个单词列表,每行一个单词,统计单词出现的频率 思路: 主要是使用c++中的map容器.map实质上是一个二叉查找树,可以做到插入.删除.查询,平均查询时间在O(logn).n为map ...

  6. sqlite3 脚本的使用

    本来想找个专门查看数据库的工具的,后来发现脚本也非常easy,就直接用脚本了. sqlite3的脚本能够方便的查看当前数据库里的内容,使用起来事实上非常easy,这一列举一些最简单的使用方法,以后假设 ...

  7. HtmlAgilityPack 抓取页面的乱码处理

    HtmlAgilityPack 抓取页面的乱码处理 用来解析 HTML 确实方便.不过直接读取网页时会出现乱码. 实际上,它是能正确读到有关字符集的信息,怎么会在输出时,没有取到正确内容. 因此,读两 ...

  8. HDU 4642 (13.08.25)

    Fliping game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. CodeForces 508C Anya and Ghosts 贪心

    做不出题目,只能怪自己不认真 题目: Click here 题意: 给你3个数m,t,r分别表示鬼的数量,每只蜡烛持续燃烧的时间,每个鬼来时要至少亮着的蜡烛数量,接下来m个数分别表示每个鬼来的时间点( ...

  10. c++,虚函数

    1.在声明函数时,在最前加上virtual,则该函数就是函虚数,基类的虚函数被派生类继承后仍是虚函数.2.派生类中可以重写基类的虚函数.3.用指针访问重写的虚函数时,被访问的虚函数是指针指向的对象所属 ...