<p>You are given a string of digits <code>num</code>, such as <code>&quot;123456579&quot;</code>. We can split it into a Fibonacci-like sequence <code>[123, 456, 579]</code>.</p>

<p>Formally, a <strong>Fibonacci-like</strong> sequence is a list <code>f</code> of non-negative integers such that:</p>

<ul>
	<li><code>0 &lt;= f[i] &lt; 2<sup>31</sup></code>, (that is, each integer fits in a <strong>32-bit</strong> signed integer type),</li>
	<li><code>f.length &gt;= 3</code>, and</li>
	<li><code>f[i] + f[i + 1] == f[i + 2]</code> for all <code>0 &lt;= i &lt; f.length - 2</code>.</li>
</ul>

<p>Note that when splitting the string into pieces, each piece must not have extra leading zeroes, except if the piece is the number <code>0</code> itself.</p>

<p>Return any Fibonacci-like sequence split from <code>num</code>, or return <code>[]</code> if it cannot be done.</p>

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

<pre>
<strong>Input:</strong> num = &quot;1101111&quot;
<strong>Output:</strong> [11,0,11,11]
<strong>Explanation:</strong> The output [110, 1, 111] would also be accepted.
</pre>

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

<pre>
<strong>Input:</strong> num = &quot;112358130&quot;
<strong>Output:</strong> []
<strong>Explanation:</strong> The task is impossible.
</pre>

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

<pre>
<strong>Input:</strong> num = &quot;0123&quot;
<strong>Output:</strong> []
<strong>Explanation:</strong> Leading zeroes are not allowed, so &quot;01&quot;, &quot;2&quot;, &quot;3&quot; is not valid.
</pre>

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

<ul>
	<li><code>1 &lt;= num.length &lt;= 200</code></li>
	<li><code>num</code> contains only digits.</li>
</ul>

const long long maxval = 1ll << 31;

class Solution {
public:
    vector<int> ans;
    string s;
    int n;

    Solution() : ans(2) {}

    bool dfs(int b) {
        if (b == n && size(ans) >= 3) return true;
        int target = 0;
        if (int d = ans[size(ans) - 2]; maxval - d <= ans.back())
            return false;
        else
            target = ans.back() + d;
        int num = 0;
        if (s[b] == '0')
            ++b;
        else {
            for (; b < n && num < target; ++b) {
                if (int d = s[b] - '0'; 10ll * num + d >= maxval)
                    break;
                else
                    num = 10 * num + d;
            }
        }
        if (num == target) {
            ans.push_back(target);
            if (dfs(b)) return true;
            ans.pop_back();
        }       
        return false;
    }

    vector<int> splitIntoFibonacci(string num) {
        n = size(num);
        s = move(num);
        if (s[0] == '0') {
            if (s[1] == '0') {
                if (count(begin(s), end(s), '0') == n)
                    return vector<int>(n, 0);
                return {};
            }
            else {
				for (int j = 1; j + j < n; ++j) {
					if (int d = s[j] - '0'; 10ll * ans[1] + d >= maxval)
						break;
					else
						ans[1] = 10 * ans[1] + d;
					if (dfs(j + 1))
						return move(ans);
				}
            }
        }
        else {
			for (int i = 0; i + 2 < n; ++i) {
				if (int d = s[i] - '0'; 10ll * ans[0] + d >= maxval)
					break;
				else
					ans[0] = 10 * ans[0] + d;
                ans[1] = 0;
                if (s[i + 1] == '0') {
                    if (dfs(i + 2))
                        return move(ans);                
                }
                else {
					for (int j = i + 1; j + i + 1 < n; ++j) {
						if (int d = s[j] - '0'; 10ll * ans[1] + d >= maxval)
							break;
						else
							ans[1] = 10 * ans[1] + d;
						if (dfs(j + 1))
							return move(ans);
					}
                }
			}
        }
        return {};
    }
};

8200