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

题目

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;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值