题目链接

题意:给出一个式子,但这个式子不一定是等式,在‘+’,‘-’,‘=’符号位置不变的情况下,又一次排列数字的位置,使其成为等式。假设能够的话。输出当中一种排列方式。

思路:我们将等号右边的数所有移动到等号右边,比如a+b-c=d-e,移动后变成a+b+e-(c+d)=0。也就是a+b+e=c+d。所以当式子能够变化成等式时,所有数的和必定是偶数。那么问题能够转化为在n个数中找出m个数(m的值为等号左边的整数的数量),使m个 数的和为从和的一半。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int MAXN = 1005; char str[MAXN], Lsy[MAXN], Rsy[MAXN], vis[MAXN];
int arr[MAXN], front[MAXN], back[MAXN];
int cnt1, cnt2, eql, Lnum, ans, flag, L, R; int init() {
cnt1 = 1, cnt2 = eql = L = R = 0;
int l = strlen(str), sum = 0;
sscanf(str, "%d", &arr[0]);
sum = arr[0];
for (int i = 0; i < l; i++) {
if (str[i] == '+' || str[i] == '-' || str[i] == '=') {
if (str[i] == '=')
eql = i;
if (!eql) {
Lsy[L] = str[i];
L++;
}
else if (eql != i) {
Rsy[R] = str[i];
R++;
}
sscanf(str + i + 1, "%d", &arr[cnt1]);
sum += arr[cnt1++];
}
} Lnum = 1;
for (int i = 0; i < l; i++) {
if (i < eql && str[i] == '+')
Lnum++;
else if (i > eql && str[i] == '-')
Lnum++;
}
return sum;
} int dfs(int k, int pos, int cur) {
if (k == Lnum) {
if (cur == ans)
return true;
return false;
}
if (Lnum - k > cnt1 - pos)
return false;
if (pos < cnt1 && cur + arr[pos] <= ans) {
vis[pos] = 1;
if (dfs(k + 1, pos + 1, cur + arr[pos]))
return true;
vis[pos] = 0;
}
if (pos < cnt1 && dfs(k, pos + 1, cur))
return true;
return false;
} void outPut() {
int x = 0, y = 0;
for (int i = 0; i < cnt1; i++) {
if (vis[i])
front[x++] = arr[i];
else
back[y++] = arr[i];
} printf("%d", front[--x]);
for (int i = 0; i < L; i++) {
printf(" %c ", Lsy[i]);
if (Lsy[i] == '+')
printf("%d", front[--x]);
if (Lsy[i] == '-')
printf("%d", back[--y]);
}
printf(" = ");
printf("%d", back[--y]);
for (int i = 0; i < R; i++) {
printf(" %c ", Rsy[i]);
if (Rsy[i] == '+')
printf("%d", back[--y]);
if (Rsy[i] == '-')
printf("%d", front[--x]);
}
printf("\n");
} int main() {
while (gets(str)) {
int s = init();
if (s % 2)
printf("no solution\n");
else {
ans = s / 2;
memset(vis, 0, sizeof(vis));
if (dfs(0, 0, 0))
outPut();
else
printf("no solution\n");
}
}
return 0;
}

UVA10317- Equating Equations(回溯+剪枝)的更多相关文章

  1. HDU 5113 Black And White 回溯+剪枝

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5113 Black And White Time Limit: 2000/2000 MS (Java/ ...

  2. UVA 10317 - Equating Equations (背包)

    Problem F Equating Equations Input: standard input Output: standard output Time Limit: 6 seconds Mem ...

  3. HDU 2553 N皇后问题(回溯 + 剪枝)

    本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398797 题意: 在N*N(N <= 10)的方格棋盘放置了N个皇后,使得它们不相互攻击(即 ...

  4. HDU1010 Tempter of the Bone(回溯 + 剪枝)

    本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398734 题意: 输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D ...

  5. HDU1016 Prime Ring Problem (回溯 + 剪枝)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5398684.html 题意: 给你一个数字N(N <= 20),要求你把这N个数组成一个环,环内的数字不能重复,左右 ...

  6. 回溯剪枝,dfs,bfs

    dfs: 给定一个整数n,将数字1~n排成一排,将会有很多种排列方法. 现在,请你按照字典序将所有的排列方法输出. 输入格式 共一行,包含一个整数n. 输出格式 按字典序输出所有排列方案,每个方案占一 ...

  7. [算法专题] 深度优先搜索&回溯剪枝

    1. Palindrome Partitioning https://leetcode.com/problems/palindrome-partitioning/ Given a string s, ...

  8. TZOJ 1221 Tempter of the Bone(回溯+剪枝)

    描述 The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked i ...

  9. Leetcode题目39.组合总和(回溯+剪枝-中等)

    题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无 ...

随机推荐

  1. c++中set容器的功能及应用。

    set的特性是,所有元素都会根据元素的键值自动排序(默认为升序),set中不允许两个元素有相同的键值. set基本操作: 1.头文件 #include<set>. 注:一定要加上using ...

  2. 九度oj 题目1041:Simple Sorting

    题目描述: You are given an unsorted array of integer numbers. Your task is to sort this array and kill p ...

  3. 九度oj 题目1357:疯狂地Jobdu序列

    题目描述: 阳仔作为OJ的数据管理员,每一周的题目录入都让其很抓狂,因为题目不是他出的,他控制不了出题的速度……在等题目的时候,阳仔又不敢出去打篮球,所以只能在纸上乱涂乱写,这天,阳仔在纸上写下了这样 ...

  4. 利用Python从文件中读取字符串(解决乱码问题)

    首先声明这篇学习记录是基于python3的. python3中,py文件中默认的文件编码就是unicode,不用像python2中那样加u,比如u'中文'. 不过在涉及路径时,比如C:\Users\A ...

  5. HDU——1003Max Sum(子序列最大和)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  6. [LOJ#114]k 大异或和

    [LOJ#114]k 大异或和 试题描述 这是一道模板题. 给由 n 个数组成的一个可重集 S,每次给定一个数 k,求一个集合 T⊆S,使得集合 T 在 S 的所有非空子集的不同的异或和中,其异或和  ...

  7. ubuntu14.04 安装 tensorflow9.0

    ubuntu14.04 安装 tensorflow9.0 文章目录 ubuntu14.04 安装 tensorflow9.0 安装pip(笔者的版本为9.0) 仅使用 CPU 的版本的tensorfl ...

  8. ECharts学习总结(二)-----图表组件漏斗图(funnel)

    今天在学习ECharts时,想要在ECharts图表的原生态Demo中抠出漏斗图,却不知如何下手,经过一番研究,特总结如下: 首先我们需要这样做 1.拷贝出两个js文件:esl.js 和echarts ...

  9. cf496D Tennis Game

    Petya and Gena love playing table tennis. A single match is played according to the following rules: ...

  10. The reference to entity "characterEncoding" must end with the ';' delimiter (Mybatis + Mysql)

    数据源配置时加上编码转换格式后出问题了: The reference to entity "characterEncoding" must end with the ';' del ...