<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 {
public:
    int sz(string &s) {
        return (int)(s.size());
    }
    int calCommon(string a, string b) {
        int i = 0, com = 0;
        while(i < sz(b) and i < sz(a)) {
            if(b.substr(0,i+1) == a.substr(sz(a)-1-i, sz(b))) {
                com = max(com, i+ 1); 
            }
            i++;
        }
        return sz(b) - com;
    } 
    int dp[14][1 << 13];
    string shortestSuperstring(vector<string>& words) {
        int n = words.size();
        if(n == 1) return words[0];
        memset(dp,-1,sizeof(dp));
        function<int(int,int)> f = [&](int prev,int mask) -> int {
            int i = __builtin_popcount(mask);
            if(i == n) return 0;
            if(dp[prev][mask] != -1) return dp[prev][mask];
            int ans = 1e9;
           
            for(int j = 0;j < n;j++) {
                if(!(mask & (1 << j))) {
                    int size = calCommon(words[prev], words[j]);
                    ans = min(ans, size + f(j, mask | (1 << j)));
                }
            }
            // 110
            return dp[prev][mask] = ans;
                        
        };
        int ans = 1e9, starting = -1;
        for(int i = 0;i < n;i++) {
            int temp = sz(words[i]) + f(i, 1 << i);
            if(ans > temp) {
                ans = temp;
                starting = i;
            }
        }
        // cout << ans << ' ' << starting << endl;
        // for(int i = 0;i < n;i++) {
        //     for(int j = 0;j < (1 << n);j++) {
        //         cout << dp[i][j] << ' ';
        //     }
        //     cout << endl;
        // }
        string res;
        function<void(int,int)> backtrack = [&](int prev,int mask) -> void {
            int cnt = 0;
            while(__builtin_popcount(mask) != n-1 or res.size() == ans) {
                for(int j = 0;j < n;j++) {
                    if(!(mask & (1 << j))) {
                        int com = calCommon(words[prev], words[j]);
                        if(dp[prev][mask] - com == dp[j][mask | (1 << j)]) {
                            string temp = words[j].substr(sz(words[j]) - com);
                            cout << temp << endl;
                            res += temp;
                            prev = j;mask |= (1 << j);
                            break;
                        }
                    }
                }
            }
            for(int i = 0;i < n;i++) {
                int com = calCommon(words[prev], words[i]);
                if(!(mask & (1 << i))) {
                    string temp = words[i].substr(sz(words[i]) - com);
                    res += temp;
                    prev = i;
                }
            }

        };
        res += words[starting];
        backtrack(starting, 1 << starting);
        return res;
    }
};
179600