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

/* TLE 
class Solution {
public:
    string shortestSuperstring(vector<string>& words) {
        unordered_map<string, string> m;

        // mark every word as unused
        int unused = 0;  // integer is used as a bit array
        for(int i = 0; i < words.size(); i++) {

            unused |= 1 << i;
        }

        return shortestSuperstring(words, "", unused, m);
    }
    string shortestSuperstring(vector<string>& words, string startWord, int unused,unordered_map<string, string> &m) {
        if (unused == 0) {
            return startWord;
        }

        // check 
        string key = startWord + "|" + to_string(unused);
        if (m.find(key)!=m.end()) {
            return m[key];
        }

        string shortest = "";
        for(int i = 0; i < words.size(); i++) {
            if (!isConsumed(unused, i)) {
                // get the shortest superstring starting from an unused word
                string superstring = shortestSuperstring(words, words[i], consume(unused, i), m);

                // "append" the shortest superstring to the start word
                string appended = overlapAppend(startWord, superstring);

                // keep the shortest
                if (shortest == "" || appended.size() < shortest.size()) {
                    shortest = appended;
                }
            }
        }
        m[key] = shortest;
        return shortest;
    }


    // Concat string a to b . For example, "bake" and "kelt" => "bakelt"
    string overlapAppend(string &a, string &b) {
        for(int i = max(1, (int)(a.size() - b.size())); i < a.size(); i++) {
            bool match = true;
            for(int j = i; j < a.size(); j++) {
                if (a[j] != b[j-i]) {
                    match = false;
                    break;
                }
            }

            if (match) {
                return a.substr(0, i) + b;
            }
        }
        return a + b;
    }

    bool isConsumed(int unused, int i) {
        return ((unused >> i) & 1) == 0;
    }

    int consume(int unused, int i) {
        return unused & ~(1 << i);
    }
};
*/

class Solution {
public:
    int n,all;
    vector<string> wrds;
    vector<vector<int>> dp,dist,path;
    int calculateDist(string a, string b){
        int l=a.size(),m=b.size();
        for(int i=l-min(l,m);i<l;i++){
            if(a.substr(i)==b.substr(0,l-i)) return m-l+i;
        }
        return m;
    }
    string construct(int idx, vector<string>& words){
        int curr=idx;
        string result = words[idx];
        int mask = (1<<idx);
        int parent = path[mask][idx];
        while(parent!=-1){
            result += words[parent].substr(words[parent].size()-dist[curr][parent]);
            mask |= (1<<parent);
            curr = parent;
            parent = path[mask][parent];
        }
        return result;
    }
    int tsp(int mask,int idx){
        if(mask==all) return 0;
        int ans = INT_MAX,p,temp;
        if(dp[mask][idx]!=-1) return dp[mask][idx];
        
        for(int i=0;i<n;i++){
            if(mask&(1<<i)) continue;
            temp = dist[idx][i]+tsp((mask|(1<<i)),i);
            if(temp<ans){
                ans = temp;
                p=i;
            }
        }
        path[mask][idx]=p;
        return dp[mask][idx]=ans;
    }
    string shortestSuperstring(vector<string>& words) {
        n = words.size();
        all = (1<<n)-1;
        dist.resize(n,vector<int>(n,0));
        dp.resize((1<<n),vector<int>(n,-1));
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                dist[i][j]=calculateDist(words[i],words[j]);
            }
        }
        int len = INT_MAX;
        string temp;
        string ans="";
        wrds=words;
        for(int i=0;i<n;i++){
            dp = {};
            path = {};
            dp.resize((1<<n),vector<int>(n,-1));
            path.resize((1<<n),vector<int>(n,-1));
            tsp((1<<i),i);
            temp = construct(i,words);
            if(temp.size()<len){
                len = temp.size();
                ans = temp;
            }
        }
        return ans;
    }
};
56400