klickh baith soluution
"When you meet your God tell him to leave me alone" - Guts\n\nclass Solution {\nprivate:\n    int calcOverlap(string a, string b){\n        if(a == "" || b == "
"When you meet your God tell him to leave me alone" - Guts\n```\nclass Solution {\nprivate:\n    int calcOverlap(string a, string b){\n        if(a == "" || b == "") return 0;\n        int n = a.length();\n        for(int len = min(a.length()-1, b.length()-1); len >= 0; len--){\n            bool found = true;\n            for(int i = 0; i < len; i++){\n                if(a[n-len+i] != b[i]){\n                    found = false; break;\n                }\n            }\n            if(found) return len;\n        }\n        return 0;\n    }\n    string dp(int idx, int state, vector<string>&ar, vector<vector<string>>&memo, vector<vector<int>>&overlap){\n        int n = ar.size();\n        if(state == (1<<n) - 1) return ar[idx];\n        if(memo[idx][state] != "-1") return memo[idx][state];\n        \n        string res = "-1";\n        for(int i = 0; i < n; i++){\n            if((state & (1<<i)) == 0){\n                string cur, next = dp(i, state | (1<<i), ar, memo, overlap);\n                if(idx < n){\n                    int len = overlap[idx][i];\n                    cur = ar[idx].substr(0, ar[idx].size()-len) + next;\n                } else cur = next;\n                \n                if(res == "-1" || cur.length() < res.length()){\n                    res = cur;\n                }\n            }\n        }\n\n        memo[idx][state] = res;\n        return res;\n    }\npublic:\n    string shortestSuperstring(vector<string>& ar) {\n        int n = ar.size();\n        vector<vector<string>> memo(15, vector<string> (1<<ar.size(), "-1"));\n        vector<vector<int>> overlap(n, vector<int>(n, 50));\n        for(int i = 0; i < n; i++){\n            for(int j = 0; j < n; j++){\n                if(i == j) continue;\n                overlap[i][j] = calcOverlap(ar[i], ar[j]);\n            }\n        }\n        return dp(14, 0, ar, memo, overlap);\n    }\n};\n```
[]