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

    string merger(string& a,string& b){
        // if(a.size()==0) return b; 
        // if(b.size()==0) return a;
        string c="";
        int n=b.size(),m=a.size();
        for(int i=n;i>=0;i--){
            if(i<=m && a.substr(m-i,i)==b.substr(0,i)){
                return b.substr(i,n-i);
            }
        }
        return c;
    }


    string cal(int state, int word, vector<string>& words, vector<vector<string>>& dp,vector<vector<string>>& merge){
        if(state==0) return "";
        if(dp[state][word]!="") return dp[state][word];
        int n=words.size();
        string ans="";
        

        state ^= (1<<word);
        if(!state) return dp[1<<word][word]=words[word];
        for( int i=0; i < n; i++){
            if( (1<<i) & state){
                string temp=cal(state,i,words,dp,merge);
              
                temp+=merge[i][word];
                if(ans=="" || temp.size()<ans.size()) ans=temp;
            }  
        }
        state ^= (1<<word);
        
        return dp[state][word]=ans;
    }

    string shortestSuperstring(vector<string>& words) {
        int n=words.size();
        vector<vector<string>> dp((1<<n),vector<string> (n,""));
        string ans="";
        int state= (1<<n)-1;
        vector<vector<string>> merge(n,vector<string> (n,""));
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(i!=j) merge[i][j]=merger(words[i],words[j]);
            }
        }
        for(int i=0;i<n;i++){
            // state^=(1<<i);
            string temp=cal((1<<n)-1, i , words,dp,merge);
            if(i==0 || temp.size()<ans.size()) ans=temp;
            // state^=(1<<i);
        }

        // for(int i=0;i<(1<<n);i++){
        //     cout<<"HI"<<' '<<i<<' ';
        //     for(int j=0;j<n;j++) cout<<dp[i][j]<<' ';
        //     cout<<endl;
        // }
        
        return ans;
    }
};
475280