UVA 10317 Equating Equations (状态压缩)

题意:

给一个等式,但是这个等式不一定是正确的,要你对等式中的数字重新排序,使得等式成立。等式只有+和-,数字个数小于16。

分析:

由于数字只有16,总共有2^16种可能,所以可以用状态压缩求解。
以a + b - c = d - e为例子。
1. 我们把等式右边的各项都换到左边,a + b - c - d + e = 0
2. 把+项和-项放一起,就变成(a + b + e) - (c + d) = 0,所以所有数字的和sum应该为偶数。如果不是剪枝。
3. 然后(a + b + e) = (c + d)
4. 令+为1,-为0,枚举所有的状态。
5. 然后我们只要找到多少个-项,状态压缩枚举同样多的数,且和正好是sum / 2,那等式就能成立了。

AC代码

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
vector<int> rec; //保存数字
vector<char> op; //保存操作符
int pos , neg; //记录正负数的个数
void init() {
    rec.clear();
    op.clear();
}
void preProc(string str) {
    string buf;
    stringstream ss(str);
    int x;
    bool equal = false;
    pos = 1, neg = 0;
    while(ss >> buf) {
        if(isdigit(buf[0])) {
            x = atoi(buf.c_str());
            rec.push_back(x);
        }else {
            op.push_back(buf[0]);
            if(buf[0] == '=')
                equal = true, neg++;
            else if(equal ^ (buf[0] == '+'))
                pos++;
            else
                neg++;
        }
    }
}
int getSum() {
    int sum = 0;
    for(int i = 0; i < rec.size(); i++) {
        sum += rec[i];
    }
    return sum;
}
void print_state(int st, int cnt) {
    vector<int> postive, negative; //新建两个向量用于保存正数和负数

    for(int i = 0; i < rec.size(); i++) {
        if(st & (1 << i)) //保存正数
            postive.push_back(rec[i]);
        else //保存负数
            negative.push_back(rec[i]);
    }

    int np = 0, nn = 0; //保存正数的个数,保存负数的个数
    bool equal = false;
    if(cnt == pos) {
        printf("%d", postive[np++]);
        for(int i = 0; i < op.size(); i++) {
            if(op[i] == '=') {
                equal = true; printf(" = %d", negative[nn++]);
            }else if(op[i] == '+') {
                equal ? printf(" + %d",negative[nn++]) : printf(" + %d",postive[np++]);
            }else {
                equal ? printf(" - %d",postive[np++]) : printf(" - %d",negative[nn++]);
            }
        }
    }else {
        printf("%d", negative[nn++]);
        for(int i = 0; i < op.size(); i++) {
            if(op[i] == '=') {
                equal = true; printf(" = %d", postive[np++]);
            }else if(op[i] == '+') {
                equal ? printf(" + %d",postive[np++]) : printf(" + %d",negative[nn++]);
            }else {
                equal ? printf(" - %d",negative[nn++]) : printf(" - %d",postive[np++]);
            }
        }
    }
    puts("");
}
void solve() {
    int posSum, negSum, cnt;
    int sum = getSum();
    if(sum & 1) { //如果sum为奇数不符合条件
        puts("no solution");
        return;
    }

    sum = sum / 2;

    int end = 1 << rec.size();
    for(int i = 0; i < end; i++) { //遍历最多 2^16种情况
        posSum = negSum = cnt = 0;
        for(int j = 0; j < rec.size(); j++) {
            if(posSum > sum || neg > sum) break;
            if(i & (1 << j))
                posSum += rec[j] , cnt++;
            else
                negSum += rec[j];
        }
        if(posSum == negSum && (cnt == pos || cnt == neg)) {
            print_state(i, cnt);
            return ;
        }
    }
    puts("no solution");
}
int main() {
    string str;
    while(getline(cin, str)) {
        init();
        preProc(str);
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值