classSolution { public: intminimumLengthEncoding(vector<string>& words){ int ans = 0; int n = words.size(); vector<bool> contained(n); vector<bool> encoded(n); for (int i = 0; i < n; i++){ contained[i] = false; encoded[i] = false; } for (int i = 0; i < n; i++) { if (contained[i]) continue; for (int j = 0; j < n; j++) { if (i != j && !contained[j]) { int ilen = words[i].length(), jlen = words[j].length(); if (jlen > ilen) continue; bool isContained = true; while (jlen > 0) { if (words[i][--ilen] != words[j][--jlen]) { isContained = false; break; } } if (isContained) { contained[j] = true; if (encoded[i] && encoded[j]) ans--; if (!encoded[i] && !encoded[j]) { encoded[i] = true; ans++; } } } } }
for (int i = 0; i < n; i++) { if (encoded[i] && !contained[i]) ans += words[i].length(); elseif (!encoded[i] && !contained[i]) ans += words[i].length() + 1; }