Sicily 6137. Removing Brackets

本文介绍了一个算法问题,即从一个包含括号的有效数学表达式中去除有效括号对的所有可能性,并输出所有不同的结果。该算法使用了深度优先搜索(DFS)来枚举所有可能的状态。

6137. Removing Brackets

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

Mirko was bored at his chemistry class, so he played Bomb Switcher on his cell phone. Unfortunately, he was spotted and was given a ridiculously heavy assignment for homework. For a given valid math expression with brackets, he must find all different expressions that can be obtained by removing valid pairs of brackets from the original expression. Two expressions are different if there is a character at which they differ.
For example, given (2+(2*2)+2), one can get (2+2*2+2), 2+(2*2)+2, and 2+2*2+2. (2+2*2)+2 and 2+(2*2+2) can?t be reached, since we would have to remove pairs of brackets that are not valid. More than one pairs of brackets can surround the same part of the expression.

Input

The first and only line of input contains one valid mathematical expression composed of nonnegative integers, basic arithmetic operations denoted with characters ?+?, ?*?, ?-? and ?/?, and brackets ?(? and ?)?.
Given expression won?t have more than 200 characters, and will have at least one, and no more than 10 pairs of brackets. Each expression is guaranteed to have at least one pair of brackets.
 

Output

Output all different expressions that can be obtained by removing valid pairs of brackets, sorted lexicographically.

Sample Input

样例1:
(0/(0))
样例2:
(2+(2*2)+2)
样例3:
(1+(2*(3+4)))

Sample Output

样例1:
(0/0) 
0/(0) 
0/0
样例2:
(2+2*2+2) 
2+(2*2)+2 
2+2*2+2
样例3:
(1+(2*3+4)) 
(1+2*(3+4)) 
(1+2*3+4) 
1+(2*(3+4)) 
1+(2*3+4) 
1+2*(3+4) 
1+2*3+4

Problem Source

COCI 2012.4 2012年每周一赛第十一场

DFS,枚举所有,注意有可能会有((1 + 2))的情况;

#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;

string c[1050];//答案string数组
char ori_in[205];//原来读入的字符串,用于比较
char in[205];//不断在DFS中变换的字符串
int b_pos[11];//左括号位置
int to_b_pos[11];//与左括号位置对应的用括号位置
int counter = 0;//总共不同的答案数
int brackets = 0;//括号对数

bool was_here(int pos) {//检查是否已经出现过
    for (int i = 0; i < pos; i++) {
        if (c[i] == c[pos]) {
            return true;
        }
    }
    return false;
}

void push_in() {//存入答案数组
    for (int i = 0; i < (int)strlen(in); i++) {
        if (in[i] != ' ') {
            c[counter].push_back(in[i]);
        }
    }
    if (!was_here(counter)) {
        counter++;
    } else {
        c[counter].clear();
    }
}

void DFS(int pos) {
    
    if (pos == brackets) {
        if (strcmp(ori_in, in))
            push_in();
        return;
    }

    DFS(pos + 1);//当前位置括号不变
        
    in[b_pos[pos]] = ' ';
    in[to_b_pos[pos]] = ' ';
    DFS(pos + 1);//当前位置括号去掉
    in[b_pos[pos]] = '(';
    in[to_b_pos[pos]] = ')';
}

bool is_new(int pos, int count) {//计算括号数目的时候用于判断(是否已经出现过
    for (int i = 0; i < count; i++) {
        if (b_pos[i] == pos) {
            return false;
        }
    }
    return true;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin >> in;
    strcpy(ori_in, in);
    brackets = 0;
    for (int i = 0; i < (int)strlen(in); i++) {
        if (in[i] == '(') {
            brackets++;
        }
    }
    int k = 0;
    for (int i = 0; i < (int)strlen(in); i++) {
        if (in[i] == ')') {
            for (int j = i - 1; j >= 0; j--) {
                if (in[j] == '(' && is_new(j, k)) {
                    b_pos[k] = j;
                    to_b_pos[k++] = i;
                    break;
                }
            }
        }
    }
    DFS(0);
    sort(c, c + counter);//排序得字典序
    for (int i = 0; i < counter; i++) {
        cout << c[i] << endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值