九度 1498:寻找表达式(暴力搜索, 计算表达式)

题目描述:

现在有一个序列123......N,其中N介于3和15之间,要求在序列之间加入+、-或者空格,使得该序列组成的数学表达式的运算结果为0。

思路

1. 枚举运算符, 时间复杂度为 o(3^15)

2. 1_2 是 12, 10_11 是 1011, WA了很多次

3. 一个简单的计算器做了一下午, 思路忘了. 要注意案例, 1+2_3, 1+2_3-4

4. 下次要把中序表达式转后序表达式的算法实现以下

5. 使用了 STL, 超时了. 当然不需要使用 STL 也能做出来

代码

#include <iostream>
#include <stdio.h>
#include <string>
#include <deque>
#include <vector>
using namespace std;

int n;
int cnt;
char ops[5] = {' ', '+', '-'};

int calculation(const string &str, int n) {
    vector<int> stack1, stack2;
    stack1.push_back(1);
    stack1.push_back(2);
    stack2.push_back(str[0]-'0');


    for(int i = 2; i < n; i ++) {
        int newop = str[i-1]-'0';
        if(stack2.size() == 0) {
            stack1.push_back(i+1);
            stack2.push_back(newop);
            continue;
        }

        if(stack2.back() == 0) {
            int rt = stack1.back();
            stack1.pop_back();
            int lt = stack1.back();
            stack1.pop_back();
            if(rt < 10)
                stack1.push_back(lt*10 + rt);
            else
                stack1.push_back(lt*100 + rt);
            stack2.pop_back();
            
            i --;
            continue;    
        }

        if(stack2.back() == 1) {
            if(newop == 0) {
                stack1.push_back(i+1);
                stack2.push_back(0);
            }else {
                int rt = stack1.back();
                stack1.pop_back();
                int lt = stack1.back();
                stack1.pop_back();

                stack1.push_back(lt+rt);
                stack2.pop_back();
                stack2.push_back(newop);
                stack1.push_back(i+1);
            }
            continue;
        }

        if(stack2.back() == 2) {
            if(newop == 0) {
                stack1.push_back(i+1);
                stack2.push_back(0);
            }else {
                int rt = stack1.back();
                stack1.pop_back();
                int lt = stack1.back();
                stack1.pop_back();

                stack2.pop_back();
                stack1.push_back(lt-rt);
                stack2.push_back(newop);
                stack1.push_back(i+1);
            }
            continue;
        }

    }

    while(stack2.size()) {
        int newop = stack2.back();
        int rt = stack1.back();
        stack1.pop_back();
        int lt = stack1.back();
        stack1.pop_back();
        stack2.pop_back();

        if(newop == 0) {
            if(rt < 10)
                stack1.push_back(lt*10 + rt);
            else
                stack1.push_back(lt*100 + rt);
        }else if(newop == 1) {
            stack1.push_back(lt+rt);
        }else{
            stack1.push_back(lt-rt);
        }
    }
    
    return stack1.back();

    
}


void printResult(const string &str) {
    cnt ++;
    string res;
    res.push_back(1+'0');
    for(int i = 1; i < n; i ++) {
        res.push_back(ops[str[i-1]-'0']);
        if(i+1 < 10) {
            res.push_back(i+1+'0');
        }else{
            res.push_back('1');
            res.push_back((i+1)%10+'0');
        }
    }
    printf("%s
", res.c_str());

}

void dfs(string &str, int depth) {
    if(depth == 0) {
        int res = calculation(str, n);
        if(res == 0) {
            //cout << str << endl;
            printResult(str);
        }
            
        return;
    }

    for(int i = 0; i < 3; i ++) {
        //printf("depth = %d, i = %d
",depth, i);
        str.push_back(i+'0');
        dfs(str, depth-1);
        str = str.substr(0, str.size()-1);
    }
}


int main() {

    while(scanf("%d", &n) != EOF) {
        cnt = 0;
        string tmp;
        dfs(tmp, n-1);

    }
    return 0;

    
}
原文地址:https://www.cnblogs.com/xinsheng/p/3585403.html