Hut 1997 Seven tombs

题目连接:http://openoj.awaysoft.com/JudgeOnline/problem.php?id=1997

表达式树的应用。

本题可以使用中缀表达式转后缀表达式,然后枚举求解。也可以直接构建表达式树进行枚举求解。

关键是寻找最后计算的运算符,然后递归建立表达式树。

具体做法参考刘汝佳《算法竞赛入门经典》198页。

另外练习:next_permutation()

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;

#define maxn 105

int lch[2][maxn];
int rch[2][maxn];
string op[2][maxn];
//节点数
int nc = 0;

int f[7] = {1,2,3,4,5,6,7};

//建立表达式树
int build_tree(char *s,int x,int y,int t)
{
    int p = 0;
    int c1 = -1;
    int c2 = -1;
    int flag = 0;
    int u = 0;
    for(int i=x; i<y; i++)
    {
        if(s[i] == '(' || s[i] == ')' || s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/')
        {
            flag = 1;
            break;
        }
    }
    //没有运算符了
    if(flag == 0)
    {
        u = ++nc;
        lch[t][u] = 0;
        rch[t][u] = 0;
        for(int i=x; i<y; i++)
        {
            op[t][u].push_back(s[i]);
        }
        return u;
    }
    for(int i=x; i<y; i++)
    {
        switch(s[i])
        {
        case '(':
            p++;
            break;
        case ')':
            p--;
            break;
        case '+':
        case '-':
            if(!p) c1 = i;
            break;
        case '*':
        case '/':
            if(!p) c2 = i;
            break;

        }
    }
    if(c1<0) c1 = c2;
    if(c1<0) return build_tree(s,x+1,y-1,t);
    u = ++nc;


    lch[t][u] = build_tree(s,x,c1,t);
    rch[t][u] = build_tree(s,c1+1,y,t);
    op[t][u].push_back(s[c1]);
    return u;
}
int num(int u,int t)
{
    int temp = 1;
    int sum = 0;
    if(op[t][u][0] == '+')
    {
        return num(lch[t][u],t) + num(rch[t][u],t);
    }
    if(op[t][u][0] == '-')
    {
        return num(lch[t][u],t) - num(rch[t][u],t);
    }
    if(op[t][u][0] == '*')
    {
        return num(lch[t][u],t) * num(rch[t][u],t);
    }
    for(int i=op[t][u].size()-1; i>=0; i--)
    {
        sum += f[op[t][u][i] - 'A'] * temp;
        temp *= 10;
    }
    return sum;
}
void init()
{
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<maxn;j++)
        {
            op[i][j].clear();
        }
    }
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    char expression[105];
    char left_exp[105];
    char right_exp[105];

    while(scanf(" %s",expression)!=EOF)
    {
        init();
        int len = strlen(expression);
        int len1 = 0,len2 = 0;
        int flag = 0;
        for(int i=0; i<len; i++)
        {
            if(expression[i] == '=')
            {
                flag = 1;
                continue;
            }
            if(flag == 0) left_exp[len1++] = expression[i];
            else right_exp[len2++] = expression[i];
        }
        left_exp[len1] = '\0';
        right_exp[len2] = '\0';

        nc = 0;
        int node1 = build_tree(left_exp,0,len1,0);
        nc = 0;
        int node2 = build_tree(right_exp,0,len2,1);
        int lsum,rsum;
        do
        {
            lsum=num(node1,0);
            rsum=num(node2,1);
            if(lsum==rsum)
                break;
        }
        while(next_permutation(f,f+7));
        for(int i=0; i<len; i++)
        {
            if(expression[i]>='A' && expression[i]<='G')
                printf("%d",f[expression[i]-'A']);
            else
                printf("%c",expression[i]);
        }
        printf("\n");

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值