题目链接

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

思路:我们将等号右边的数所有移动到等号右边,比如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. 【mysql优化 3】嵌套循环连接算法

    原文地址:Nested-Loop Join Algorithms mysql在表之间执行连接操作,包括了使用循环嵌套算法或者其他在此基础上的变形. 循环嵌套连接算法: 一个简单的嵌套循环连接(NLJ: ...

  2. 九度oj 题目1552:座位问题

    题目描述: 计算机学院的男生和女生共n个人要坐成一排玩游戏,因为计算机的女生都非常害羞,男生又很主动,所以活动的组织者要求在任何时候,一个女生的左边或者右边至少有一个女生,即每个女生均不会只与男生相邻 ...

  3. js 抓取页面数据

    数据抓取 主要思路和原理 在根节点document中监听所有需要抓取的事件 在元素事件传递中,捕获阶段获取事件信息,进行埋点 通过getBoundingClientRect() 方法可获取元素的大小和 ...

  4. aoj 2226 Merry Christmas

    Merry Christmas Time Limit : 8 sec, Memory Limit : 65536 KB Problem J: Merry Christmas International ...

  5. Codevs 3287 货车运输 == 洛谷P1967

    3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description A 国有 n 座城市,编 ...

  6. DataSet中的表动态设置主键外键的方法

    原文发布时间为:2008-08-01 -- 来源于本人的百度文章 [由搬家工具导入] protected void pk_Click(object sender, EventArgs e)    {  ...

  7. 如何用github展示前端页面

    如何在github上展示你的前端页面 参考:https://luozhihao.github.io/demo/ 感谢作者 1.New reposipory 2.进入你本机目录 我是在d:vuedemo ...

  8. Scrapy学习-11-Selector对象使用

    Selector使用 使用背景 我需要使用类似spider项目中,response使用的xpath和css获取页面指定数据,但因为爬取页面较小我们不想创建一个spider项目时,就可以使用scrapy ...

  9. C#连接OleDBConnection数据库的操作

    对于不同的.net数据提供者,ADO.NET采用不同的Connection对象连接数据库.这些Connection对我们屏蔽了具体的实现细节,并提供了一种统一的实现方法. Connection类有四种 ...

  10. AC日记——[网络流24题]骑士共存 cogs 746

    746. [网络流24题] 骑士共存 ★★☆   输入文件:knight.in   输出文件:knight.out   简单对比时间限制:1 s   内存限制:128 MB 骑士共存问题 «问题描述: ...