标签:find col use not res ssi cts pop size
A string such as "word" contains the following abbreviations:
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
Given a target string and a set of strings in a dictionary, find an abbreviation of this target string with the smallest possible length such that it does not conflict with abbreviations of the strings in the dictionary.
Each number or letter in the abbreviation is considered length = 1. For example, the abbreviation "a32bc" has length = 4.
Note:
? In the case of multiple answers as shown in the second example below, you may return any one of them.
? Assume length of target string = m, and dictionary size = n. You may assume that m ≤ 21, n ≤ 1000, and log2(n) + m ≤ 20.
Examples:
"apple", ["blade"] -> "a4" (because "5" or "4e" conflicts with "blade")
"apple", ["plain", "amber", "blade"] -> "1p3" (other valid answers include "ap3", "a3e", "2p2", "3le", “3l1").
class Solution { public: string minAbbreviation(string target, vector<string>& dictionary) { if (dictionary.empty()) return to_string((int)target.size()); priority_queue<pair<int, string>, vector<pair<int, string>>, greater<pair<int, string>>> q; vector<string> abbrs = generateAbbreviations(target); for(auto abbr:abbrs) q.push({abbr.size(),abbr}); while (!q.empty()) { auto t = q.top(); q.pop(); bool no_conflict = true; for (string word : dictionary) { if (valid(word, t.second)) { no_conflict = false; break; } } if (no_conflict) return t.second; } return ""; } private: bool valid(string word, string abbr) { int m = word.size(), n = abbr.size(), p = 0, cnt = 0; for (int i = 0; i < abbr.size(); ++i) { if (abbr[i] >= ‘0‘ && abbr[i] <= ‘9‘) { if (cnt == 0 && abbr[i] == ‘0‘) return false; cnt = 10 * cnt + abbr[i] - ‘0‘; } else { p += cnt; if (p >= m || word[p++] != abbr[i]) return false; cnt = 0; } } return p + cnt == m; } vector<string> generateAbbreviations(string word) { vector<string>res; dfs(0,"",res,false,word); return res; } void dfs(int idx,string tmp,vector<string>&res,bool prevNum,string word) { if(idx==word.size()){ res.push_back(tmp); return; } dfs(idx+1,tmp+word[idx],res,false,word); if(!prevNum) { for(int len = 1;len+idx<=word.size();len++) { dfs(idx+len,tmp+to_string(len),res,true,word); } } } };
411. Minimum Unique Word Abbreviation
标签:find col use not res ssi cts pop size
原文地址:http://www.cnblogs.com/jxr041100/p/7899097.html