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 中的数字可以无 ...
随机推荐
- webstorm自带debugger服务器
打开webstorm->settings->Build,Execution,Deployment->Debugger->把端口Port改成8089或者其他80端口,按确定就可以 ...
- chef and churu 分块 好题
题目大意 有一个长度为n的数组A 有n个函数,第i个函数 \[f(l[i],r[i])=\sum_{k=l[i]}^{r[i]}A_k\] 有两种操作: 1)修改A[i] 2)询问第x-y个函数值的和 ...
- 完全平方数(bzoj 2440)
Description 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而这丝毫不影响他对其他数的热爱. 这天是 ...
- scss 侧边栏_图片
html <!doctype html><html lang="zh-CN"><head> <meta charset="UTF ...
- 长度rem的使用
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- yii批量插入数据
现在有如下数据: 查看代码 打印 1 $user=array( 2 0=>array('id'=>1,'name'=>'张三'), 3 0=>array(' ...
- dedecms--二次开发文章内容页未登录禁止访问和同一个帐号只允许一个ip登录
最近在用dedecms二次开发会员功能,领导要求,会员未登录不允许访问文章内容页,和同一个账号只允许一个ip登录,我是将这两个在一起判断的,判断session存不存在,不存在的情况下就是未登录,这时候 ...
- 遍历删除List中的元素,会报错?
经常会碰到遍历集合,然后删除里面的对象报错, 纠结半天, 百度了一下,有大神说不能用for-each, for , 只能用迭代器,真的吗? 我就删成功了呢,看代码,请大神们指正! public s ...
- 使用vue-cli搭建element-ui项目
最近在使用element-ui搭建项目时发现若只纯用webpack来运行element-ui,要配置各种文件,对于新手来说实在太不友好了, 就想到用vue-cli来搭建整个vue项目 1.安装node ...
- Ubuntu 16.04下使用Wine安装Xshell 4和Xftp 4
说明: 1.使用的Wine版本是深度出品(Deepin),已经精简了很多没用的配置,使启动能非常快,占用资源小. 2.由于Xshell 5的C++库无法在这个Wine版本运行,即使升级官方原版的2+版 ...