UVA10317- Equating Equations(回溯+剪枝)
题意:给出一个式子,但这个式子不一定是等式,在‘+’,‘-’,‘=’符号位置不变的情况下,又一次排列数字的位置,使其成为等式。假设能够的话。输出当中一种排列方式。
思路:我们将等号右边的数所有移动到等号右边,比如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(回溯+剪枝)的更多相关文章
- HDU 5113 Black And White 回溯+剪枝
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5113 Black And White Time Limit: 2000/2000 MS (Java/ ...
- UVA 10317 - Equating Equations (背包)
Problem F Equating Equations Input: standard input Output: standard output Time Limit: 6 seconds Mem ...
- HDU 2553 N皇后问题(回溯 + 剪枝)
本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398797 题意: 在N*N(N <= 10)的方格棋盘放置了N个皇后,使得它们不相互攻击(即 ...
- HDU1010 Tempter of the Bone(回溯 + 剪枝)
本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398734 题意: 输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D ...
- HDU1016 Prime Ring Problem (回溯 + 剪枝)
本文链接:http://www.cnblogs.com/Ash-ly/p/5398684.html 题意: 给你一个数字N(N <= 20),要求你把这N个数组成一个环,环内的数字不能重复,左右 ...
- 回溯剪枝,dfs,bfs
dfs: 给定一个整数n,将数字1~n排成一排,将会有很多种排列方法. 现在,请你按照字典序将所有的排列方法输出. 输入格式 共一行,包含一个整数n. 输出格式 按字典序输出所有排列方案,每个方案占一 ...
- [算法专题] 深度优先搜索&回溯剪枝
1. Palindrome Partitioning https://leetcode.com/problems/palindrome-partitioning/ Given a string s, ...
- 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 ...
- Leetcode题目39.组合总和(回溯+剪枝-中等)
题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无 ...
随机推荐
- iptables之ipset集群工具
ipset介绍 ipset是iptables的扩展,它允许你创建 匹配整个地址集合的规则.而不像普通的iptables链只能单IP匹配, ip集合存储在带索引的数据结构中,这种结构即时集合比较大也可以 ...
- iOS键盘高度的获取
代码如下: - (void)viewDidLoad { [super viewDidLoad]; //增加监听,当键盘出现或改变时收出消息 [[NSNotificationCenter default ...
- 微软2014实习生及秋令营技术类职位在线测试(题目1 : String reorder)
题目1 : String reorder 时间限制:10000ms 单点时限:1000ms 内存限制:256MB Description For this question, your program ...
- 关于python数据类型的一些举例
if True: name2=1 print(name2) 输出:1 if False: name1=1 print(name1) 报错: NameError: name 'name1' is not ...
- bzoj 4555 NTT优化子集斯特林
题目大意 读入n 求\(f(n)=\sum_{i=0}^n\sum_{j=0}^i\left\{\begin{matrix}i \\ j\end{matrix}\right\}*2^j*j!\) 分析 ...
- ADO:用代码调用存储过程
原文发布时间为:2008-08-02 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- Sequelize的增删改查
//启动mysql数据库 net start mysql //新建index.js //建立连接var Sequelize=require("sequelize");var mys ...
- LeetCode OJ--Binary Tree Level Order Traversal II
http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 树的层序遍历,和上一道题相比,对结果做一个顺序调整 reve ...
- git add 错误修改方法
git add a.txt 报如下错误: fatal: Unable to create 'E:/git/.git/index.lock': File exists. Another git proc ...
- FMDB支持的事务类型
FMDB支持的事务类型 在数据库中,事务可以保证数据操作的完整性.当存在大量并发操作,容易出现死锁问题.在SQLite中,为了解决该问题,提供三种事务模式,分别为DEFFERED.IMMEDIAT ...