[Leetcode] 411. Minimum Unique Word Abbreviation 解题报告

来源:互联网 发布:青少年犯罪率数据2016 编辑:程序博客网 时间:2024/06/05 10:28

题目

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 ≤ 21n ≤ 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").

思路

题目的基本思路是深度优先搜索(DFS),但是单纯的DFS会比较耗时,可以在DFS的过程中加入有效剪枝来提高效率。我们首先将dictionary中长度与target不同的单词去掉,以提高效率(因为长度不同的单词其缩写不可能发生冲突)。DFS从空字符串开发,逐一增加target中的字母,并尝试越过一些字母,插入数字。遍历dictionary中的单词,检查冲突,如果不存在冲突,则递归,并更新最优解。

怎么剪枝呢?可以用一个变量来记录当前时刻的最优的单词缩写长度,若DFS分支的长度大于该长度,则进行剪枝。

代码

class Solution {public:    string minAbbreviation(string target, vector<string>& dictionary) {        int len = target.size();        if(len == 0) {            return "";        }        vector<string> dic;        for(string s : dictionary) {    // only the word that has the same length is possible to cause conflict            if(s.size() == len) {                dic.push_back(s);            }        }        int len_d = dic.size();        if(len_d == 0) {            return to_string(len);        }        string res = target;        dfs("", 0, target, 0, dic, res, len);        return res;    }private:    void dfs(string cur, int cur_len, string& target, int pos, vector<string>&dic, string &res, int& minlen) {        if(pos >= (int)target.size()) {            if(cur_len < minlen) {         // if cur_len >= minLen, do nothing                bool f = true;             // indicates whether valid                for(string s:dic) {                    if(check(s,cur)) {                        f = false;                        break;                    }                }                if(f) {                    res = cur;                    minlen = cur_len;                }            }            return;        }        if(minlen == cur_len) {            return;          }        if(cur.empty() || !isdigit(cur.back())) {                               // try to replace some characters            for(int i = target.size() - 1; i >= pos; --i) {                 string add = to_string(i - pos + 1);                 dfs(cur + add, cur_len + 1, target, i + 1, dic, res, minlen);            }        }        dfs(cur + target[pos], cur_len + 1, target, pos + 1, dic, res, minlen); // do not replace anything    }        bool check(string s1, string s2) {        int len1 = s1.size();        int len2 = s2.size();        int l = 0 , r = 0;        while(l < len1 && r < len2) {            if(isdigit(s2[r])) {                int dis = 0;                while(r < len2 && isdigit(s2[r])) {                    dis = dis * 10 + s2[r++] - '0';                }                l += dis;            }            else if (s2[r] == '0') {                return false;            }            else {                if(s1[l] == s2[r]) {                    l++;                    r++;                }                else{                    return false;                }            }         }         if(l >= len1 && r >= len2) {            return true;         }         return false;    }};

原创粉丝点击