<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>

class Solution {
public:
    pair<int, long> findSum(string num, long total, int strIndex) {
        string sum = "";
        for(int i = strIndex; i < num.size(); i++) {
            if (i - strIndex > 10) return {-1, -1};
            sum += num[i];
            if (num[strIndex] == '0' && stol(sum) != 0) return {-1, -1};
            if (stol(sum) == total) return {i, stol(sum)};
            if (stol(sum) > total) return {-1, -1};
        }
        return {-1, -1};
    }

    vector<long> checkTail(string num, long first, long second, int index) {
        long fi = first, se = second;
        // cout << "initial: " << first << " " << second << " index: " << index << endl;
        
        pair<int, long> f = findSum(num, first + second, index);
        if (f.first == num.size() - 1) return { f.second };
        vector<long> result = { f.second };
        while(f.first != -1) {
            fi = se;
            se = f.second;
            // cout << "ini: " << fi << " " << se << " index: " << f.first + 1 << endl;

            f = findSum(num, fi + se, f.first + 1);
            // if (f.second > INT_MAX) return {};
            result.push_back(f.second);
            if (f.first == num.size() - 1) return result;
        }
        if (f.first == num.size() - 1) return result;
        return {};
    }

    vector<int> sss(vector<long> l, vector<long> r) {
        vector<int> res;
        for(auto d: l) {
            if (d < 0) return {};
            res.push_back(d);
        }
        for(auto dd: r) {
            int d = dd;
            if (d < 0) return {};
            res.push_back(d);
        }
        return res;
    }

    vector<int> splitIntoFibonacci(string num) {
        string first = "", second = "";
        vector<long> result;
        for (int l = 0; l < num.size() - l - 1; l++) {
            if (l > 10) break;
            first += num[l];
            if (first[0] == '0' && l > 0) break;
            second = "";
            for(int s = l+1; s < num.size() - second.size(); s++) {
                if (s - l > 10) break;
                second += num[s];
                long fl = stol(first), fs = stol(second);
                if (fs > INT_MAX) break;
                result = {fl, fs};
                // cout << "f: " << fl << " s: " << fs << endl; 
                if (second[0] == '0' && stol(second) != 0) break;
                vector<long> tail = checkTail(num, fl, fs, s+1);
                if (!tail.empty()) return sss(result, tail);
            }
        }
        return {};
    }
};
11600