[Leetcode 411]Minimum Unique Word Abbreviation

32 篇文章 0 订阅
15 篇文章 0 订阅

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").


题解: 回溯。谷歌题目。


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) {
            if(s.size()==len) {
                dic.push_back(s);
            }
        }
        int len_d=dic.size();
        if(len_d==0) return intTostring(len);
        string res=target;
        dfs("",0,target,0,dic,res,len);
        return res;
    }

    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) {  //剪枝
                bool f=true;
                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())) {   //
            for(int i=target.size()-1;i>=pos;i--) {
                 string add=intTostring(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);
    }

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

    string intTostring(int num) {
        stringstream ss;
        ss<<num;
        return ss.str();
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值