<p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>&#39;+&#39;</code><em>, </em><code>&#39;-&#39;</code><em>, and/or </em><code>&#39;*&#39;</code><em> between the digits of </em><code>num</code><em> so that the resultant expression evaluates to the </em><code>target</code><em> value</em>.</p>

<p>Note that operands in the returned expressions <strong>should not</strong> contain leading zeros.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> num = &quot;123&quot;, target = 6
<strong>Output:</strong> [&quot;1*2*3&quot;,&quot;1+2+3&quot;]
<strong>Explanation:</strong> Both &quot;1*2*3&quot; and &quot;1+2+3&quot; evaluate to 6.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> num = &quot;232&quot;, target = 8
<strong>Output:</strong> [&quot;2*3+2&quot;,&quot;2+3*2&quot;]
<strong>Explanation:</strong> Both &quot;2*3+2&quot; and &quot;2+3*2&quot; evaluate to 8.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> num = &quot;3456237490&quot;, target = 9191
<strong>Output:</strong> []
<strong>Explanation:</strong> There are no expressions that can be created from &quot;3456237490&quot; to evaluate to 9191.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= num.length &lt;= 10</code></li>
	<li><code>num</code> consists of only digits.</li>
	<li><code>-2<sup>31</sup> &lt;= target &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

class Solution {
public:
    int atoi(const string s)
    {
        long long tmp = 0;
        for (auto c : s)
        {
            tmp *= 10;
            tmp += c-'0';
            if (tmp > INT_MAX)
                return -1;
        }
        return tmp;
    }
    bool check(const string& num, const string& op, const int target)
    {
        // cout << num << " " << op << endl;
        int tot = 0, seg_tot = 1;
        string cur_num = "";
        for (auto i = 0; i < num.size(); i ++)
        {
            if (i == 0)
            {
                cur_num += num[i];
                if (num[i] == '0' && op[i] == ' ')
                    return false;
            }
            else
            {   
                if (num[i] == '0' && op[i-1] != ' ' && op[i] == ' ')
                    return false;
                if (op[i-1] == '+')
                {
                    int tmp = atoi(cur_num);
                    if (tmp == -1)
                        return false;
                    if (seg_tot != 0 && tmp > INT_MAX*1.0/max(-seg_tot, seg_tot))
                        return false;
                    seg_tot *= tmp;
                    tot += seg_tot;
                    seg_tot = 1;
                    cur_num = "";
                    cur_num += num[i];
                }
                else if (op[i-1] == '-')
                {
                    int tmp = atoi(cur_num);
                    if (tmp == -1)
                        return false;
                    if (seg_tot != 0 && tmp > INT_MAX*1.0/max(-seg_tot, seg_tot))
                        return false;
                    seg_tot *= tmp;
                    tot += seg_tot;
                    seg_tot = -1;
                    cur_num = "";
                    cur_num += num[i];
                }
                else if (op[i-1] == '*')
                {
                    int tmp = atoi(cur_num);
                    if (tmp == -1)
                        return false;
                    if (seg_tot != 0 && tmp > INT_MAX*1.0/max(-seg_tot, seg_tot))
                        return false;
                    seg_tot *= tmp;
                    cur_num = "";
                    cur_num += num[i];
                }
                else if (op[i-1] == ' ')
                {
                    cur_num += num[i];
                }
            }
            if (i == num.size()-1)
            {
                int tmp = atoi(cur_num);
                if (tmp == -1)
                    return false;
                if (seg_tot != 0 && tmp > INT_MAX*1.0/max(-seg_tot, seg_tot))
                        return false;
                seg_tot *= tmp;
                tot += seg_tot;
            }
            // cout << tot << " " << seg_tot << " " << cur_num << endl;
        }
        return (tot == target);
    }
    void dfs(int d, const int d_max, vector<string>& ops, string& op, const string& num, const int target)
    {
        if (d == d_max-1)
        {
            if (check(num, op, target))
                ops.push_back(op);
            return;
        }
        vector<char> tmp = {'+', '-', '*', ' '};
        for (char c : tmp)
        {
            op += c;
            dfs(d+1, d_max, ops, op, num, target);
            op.pop_back();
        }
        return;
    }
    vector<string> addOperators(string num, int target) {
        string op = "";
        int len = num.size();
        vector<string> ans;
        dfs(0, len, ans, op, num, target);
        for (auto& op : ans)
        {
            string tmp = "";
            for (auto i = 0; i < num.size(); i ++)
            {
                if (i > 0 && op[i-1] != ' ')
                    tmp += op[i-1];
                tmp += num[i];
            }
            op = tmp;
        }
        return ans;
    }
};
31596