[LeetCode] 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 thesmallest 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").
这道题实际上是之前那两道Valid Word Abbreviation和Generalized Abbreviation的合体,我们的思路其实很简单,首先找出target的所有的单词缩写的形式,然后按照长度来排序,小的排前面,我们用优先队列来自动排序,里面存一个pair,保存单词缩写及其长度,然后我们从最短的单词缩写开始,跟dictionary中所有的单词一一进行验证,利用Valid Word Abbreviation中的方法,看其是否是合法的单词的缩写,如果是,说明有冲突,直接break,进行下一个单词缩写的验证,参见代码如下:
- 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;
- q = generate(target);
- 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 "";
- }
- priority_queue<pair<int, string>, vector<pair<int, string>>, greater<pair<int, string>>> generate(string target) {
- priority_queue<pair<int, string>, vector<pair<int, string>>, greater<pair<int, string>>> res;
- for (int i = ; i < pow(, target.size()); ++i) {
- string out = "";
- int cnt = , size = ;
- for (int j = ; j < target.size(); ++j) {
- if ((i >> j) & ) ++cnt;
- else {
- if (cnt != ) {
- out += to_string(cnt);
- cnt = ;
- ++size;
- }
- out += target[j];
- ++size;
- }
- }
- if (cnt > ) {
- out += to_string(cnt);
- ++size;
- }
- res.push({size, out});
- }
- return res;
- }
- bool valid(string word, string abbr) {
- int m = word.size(), n = abbr.size(), p = , cnt = ;
- for (int i = ; i < abbr.size(); ++i) {
- if (abbr[i] >= '' && abbr[i] <= '') {
- if (cnt == && abbr[i] == '') return false;
- cnt = * cnt + abbr[i] - '';
- } else {
- p += cnt;
- if (p >= m || word[p++] != abbr[i]) return false;
- cnt = ;
- }
- }
- return p + cnt == m;
- }
- };
类似题目:
参考资料:
https://leetcode.com/problems/minimum-unique-word-abbreviation/
https://discuss.leetcode.com/topic/61457/c-bit-manipulation-dfs-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写的更多相关文章
- Leetcode: Minimum Unique Word Abbreviation
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [LeetCode] 288.Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- 411. Minimum Unique Word Abbreviation
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [Locked] Unique Word Abbreviation
Unique Word Abbreviation An abbreviation of a word follows the form <first letter><number&g ...
- [LeetCode] Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- Leetcode Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- 288. Unique Word Abbreviation
题目: An abbreviation of a word follows the form <first letter><number><last letter> ...
- [Swift]LeetCode288. 唯一单词缩写 $ Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [LeetCode] 243. Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
随机推荐
- iOS: 在UIViewController 中添加Static UITableView
如果你直接在 UIViewController 中加入一个 UITableView 并将其 Content 属性设置为 Static Cells,此时 Xcode 会报错: Static table ...
- internet协议入门
前言 劳于读书,逸于作文. 原文地址:internet协议入门 博主博客地址:Damonare的个人博客 博主之前写过一篇博客:网络协议分析,在这篇博客里通过抓包,具体的分析了不同网络协议的传送的数据 ...
- request 对象和 response 对象
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象 HttpServletResponse HttpServletR ...
- Razor基础语法一
目录: 什么是Razor? 渲染HTML Razor语法 隐式 Razor 表达式 显式 Razor 表达式 什么是Razor? Razor是基于服务端代码转换成网页的标记语法.语法主要包括Razor ...
- C# - Networkcomms
来自英国的用C#语言编写的开源的TCP/UDP网络通信框架,简单方便,性能稳定. 参考: NetworkComms官网: NetworkComms通信框架中文网: 介绍开源的.net通信框架: Ne ...
- 深度|作为C端应用的代表,成功的陌生社交应用是什么样子的?
作 为C端应用的代表,成功的陌生社交应用是什么样子的?活跃用户数?收益回报率?在实际社交产品设计中,我们一直为这些所谓的KPI左右,具体到设计行为 上:摆弄相应的界面元素,优化一下文案.页面流,但却很 ...
- 20个非常有用的Java程序片段
下面是20个非常有用的Java程序片段,希望能对你有用. 1. 字符串有整型的相互转换 String a = String.valueOf(2); //integer to numeric strin ...
- Delphi_05_Delphi_Object_Pascal_基本语法_03
继续Delphi的学习之旅, 废话不多说,直接贴代码. { Delphi基本语法 1.对象 2.指针 3.类型别名 和 类型转换 } program DelphiObject; {$APPTYPE C ...
- [FromBody]与[FromUrl]
我们都知道,前台请求后台控制的方法有get方法和post方法两种, get:只支持ulr传数据,不管你是手动把参数拼接在Url里面还是写在data里面,只要是用get方法,都会自动绑定到url里面的形 ...
- 【原】JAVA开发环境搭建
1.JDK下载并安装,以jdk-7u45-windows-i586.exe为例(注意JDK的安装和JRE的安装是分开的) 2.“我的电脑”右键属性,找到“高级系统设置”,找到“高级”tab下的“环境变 ...