<p>Given an array of strings <code>words</code>, return <em>the smallest string that contains each string in</em> <code>words</code> <em>as a substring</em>. If there are multiple valid strings of the smallest length, return <strong>any of them</strong>.</p>

<p>You may assume that no string in <code>words</code> is a substring of another string in <code>words</code>.</p>

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

<pre>
<strong>Input:</strong> words = [&quot;alex&quot;,&quot;loves&quot;,&quot;leetcode&quot;]
<strong>Output:</strong> &quot;alexlovesleetcode&quot;
<strong>Explanation:</strong> All permutations of &quot;alex&quot;,&quot;loves&quot;,&quot;leetcode&quot; would also be accepted.
</pre>

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

<pre>
<strong>Input:</strong> words = [&quot;catg&quot;,&quot;ctaagt&quot;,&quot;gcta&quot;,&quot;ttca&quot;,&quot;atgcatc&quot;]
<strong>Output:</strong> &quot;gctaagttcatgcatc&quot;
</pre>

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

<ul>
	<li><code>1 &lt;= words.length &lt;= 12</code></li>
	<li><code>1 &lt;= words[i].length &lt;= 20</code></li>
	<li><code>words[i]</code> consists of lowercase English letters.</li>
	<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
</ul>

class Solution {
    int overlap[13][13];
    vector<string> words;
    int n;
    int find_overlap(const string& a, const string& b) {
        const int n = a.size();
        const int m = b.size();

        for (int i = max(n - m, 0); i < n; i++) {
            bool ok = true;
            for (int j = i, k = 0; j < n && k < m;) {
                if (a[j++] != b[k++]) {
                    ok = false;
                    break;
                }
            }
            if (ok)
                return n - i;
        }
        return 0;
    }

    map<pair<int, int>, pair<int, int>> memo;

    pair<int, int> dp(const int bitmask, const int idx) {
        const int all = (1 << n) - 1;
        if (bitmask == all)
            return {words[idx].size(), -1};

        pair<int,int> res{1e9, -1};
        if (memo.count({bitmask, idx}))
            return memo[{bitmask, idx}];

        for (int i = 0; i < n; i++) {
            if ((bitmask & (1 << i)) != 0)
                continue;
            const int word_len = words[idx].size() - overlap[idx][i];
            const int length = word_len + dp(bitmask | (1 << i), i).first;
            res = min(res, {length, i});
        }
        return memo[{bitmask, idx}] = res;
    }

public:
    string shortestSuperstring(vector<string>& words) {
        memset(overlap, 0, sizeof overlap);
        this->words = words;
        this->n = words.size();
        for (int i = 0; i < n; i++) {
            const string& a = words[i];
            for (int j = 0; j < n; j++) {
                if (i == j)
                    continue;
                const string& b = words[j];
                overlap[i][j] = find_overlap(a, b);
            }
        }

        pair<int,int> min_elem{1e9, -1};
        for (int i = 0; i < n; i++) {
            min_elem = min(min_elem, {dp(1 << i, i).first, i});
        }

        string final_ans = "";
        int bitmask = 0;
        int curr_idx = min_elem.second;
        while (curr_idx != -1) {
            int overlap = find_overlap(final_ans, words[curr_idx]);
            while (overlap--)
                final_ans.pop_back();

            final_ans += words[curr_idx];
            bitmask |= 1 << curr_idx;
            curr_idx = dp(bitmask, curr_idx).second;
        }

        return final_ans;
    }
};
37920