2022-01-22:力扣411,最短独占单词缩写。 给一个字符串数组strs和一个目标字符串target。target的简写不能跟strs打架。 strs是[“abcdefg“,“ccc“],tar
2022-01-22:力扣411,最短独占单词缩写。
给一个字符串数组strs和一个目标字符串target。target的简写不能跟strs打架。
strs是[“abcdefg”,“ccc”],target是"moonfdd"。target简写形式如果是"7",会跟strs中的"abcdefg"打架,因为"abcdefg"简写形式也可以是"7"。target简写形式如果是"m6",这就不会打架了,因为strs中没有以m开头,并且还有6个字符的字符串。
所以target的缩写就是"m6"。
答案2022-01-22:
递归。target的每个字符保留还是不保留。
字符串数组中跟目标字符串一样长的字符串,可以用位运算。比如"abcdefg"可以表示成0b111111,"moxnfdd"可以表示成0b0010000。相同为0,不同为1。
代码用golang编写。代码如下:
package main
import (
"fmt"
"math"
)
func main() {
dictionary := []string{"abcdefd", "ccc"}
target := "moonfdd"
ret := minAbbreviation1(target, dictionary)
fmt.Println(ret)
ret = minAbbreviation2(target, dictionary)
fmt.Println(ret)
}
// 区分出来之后,缩写的长度,最短是多少?
var min = math.MaxInt64
// 取得缩写的长度最短的时候,决定是什么(fix)
var best = 0
func minAbbreviation2(target string, dictionary []string) string {
min = math.MaxInt64
best = 0
//char[] t = target.toCharArray();
t := []byte(target)
len0 := len(t)
siz := 0
for _, word := range dictionary {
if len(word) == len0 {
siz++
}
}
words := make([]int, siz)
index := 0
// 用来剪枝
diff := 0
for _, word := range dictionary {
if len(word) == len0 {
w := []byte(word)
status := 0
for j := 0; j < len0; j++ {
if t[j] != w[j] {
status |= 1 << j
}
}
words[index] = status
index++
diff |= status
}
}
dfs2(words, len0, diff, 0, 0)
builder := ""
count := 0
for i := 0; i < len0; i++ {
if (best & (1 << i)) != 0 {
if count > 0 {
builder += fmt.Sprint(count)
}
builder += fmt.Sprintf("%c", t[i])
count = 0
} else {
count++
}
}
if count > 0 {
builder += fmt.Sprint(count)
}
return builder
}
func dfs2(words []int, len0, diff, fix, index int) {
if !canFix(words, fix) {
if index < len0 {
dfs2(words, len0, diff, fix, index+1)
if (diff & (1 << index)) != 0 {
dfs2(words, len0, diff, fix|(1<<index), index+1)
}
}
} else {
ans := abbrLen(fix, len0)
if ans < min {
min = ans
best = fix
}
}
}
// 原始的字典,被改了
// target : abc 字典中的词 : bbb -> 101 -> int ->
// fix -> int -> 根本不用值,用状态 -> 每一位保留还是不保留的决定
func canFix(words []int, fix int) bool {
for _, word := range words {
if (fix & word) == 0 {
return false
}
}
return true
}
func abbrLen(fix, len0 int) int {
ans := 0
cnt := 0
for i := 0; i < len0; i++ {
if (fix & (1 << i)) != 0 {
ans++
if cnt != 0 {
if cnt > 9 {
ans += 2 - cnt
} else {
ans += 1 - cnt
}
}
cnt = 0
} else {
cnt++
}
}
if cnt != 0 {
if cnt > 9 {
ans += 2 - cnt
} else {
ans += 1 - cnt
}
}
return ans
}
// 利用位运算加速
func minAbbreviation1(target string, dictionary []string) string {
min = math.MaxInt64
best = 0
t := []byte(target)
len0 := len(t)
siz := 0
for _, word := range dictionary {
if len(word) == len0 {
siz++
}
}
words := make([]int, siz)
index := 0
for _, word := range dictionary {
if len(word) == len0 {
w := []byte(word)
status := 0
for j := 0; j < len0; j++ {
if t[j] != w[j] {
status |= 1 << j
}
}
words[index] = status
index++
}
}
dfs1(words, len0, 0, 0)
//StringBuilder builder = new StringBuilder();
builder := ""
count := 0
for i := 0; i < len0; i++ {
if (best & (1 << i)) != 0 {
if count > 0 {
builder += fmt.Sprint(count)
}
builder += fmt.Sprintf("%c", t[i])
count = 0
} else {
count++
}
}
if count > 0 {
builder += fmt.Sprint(count)
}
return builder
}
// 所有字典中的单词现在都变成了int,放在words里
// 0....len-1 位去决定保留还是不保留!当前来到index位
// 之前做出的决定!
func dfs1(words []int, len0, fix, index int) {
if !canFix(words, fix) {
if index < len0 {
dfs1(words, len0, fix, index+1)
dfs1(words, len0, fix|(1<<index), index+1)
}
} else {
// 决定是fix,一共的长度是len,求出缩写是多长?
ans := abbrLen(fix, len0)
if ans < min {
min = ans
best = fix
}
}
}
执行结果如下:

2022-01-22:力扣411,最短独占单词缩写。 给一个字符串数组strs和一个目标字符串target。target的简写不能跟strs打架。 strs是[“abcdefg“,“ccc“],tar的更多相关文章
- 力扣算法题—079单词搜索【DFS】
给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. ...
- 力扣(LeetCode)453. 最小移动次数使数组元素相等
给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数.每次移动可以使 n - 1 个元素增加 1. 示例: 输入: [1,2,3] 输出: 3 解释: 只需要3次移动(注意每次移动 ...
- 力扣 —— Two Sum ( 两数之和) python实现
题目描述: 中文: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利 ...
- 力扣题解-LCP 06. 拿硬币
题目描述 桌上有 n 堆力扣币,每堆的数量保存在数组 coins 中.我们每次可以选择任意一堆,拿走其中的一枚或者两枚,求拿完所有力扣币的最少次数. 示例 1: 输入:[4,2,1] 输出:4 解释: ...
- 力扣算法经典第一题——两数之和(Java两种方式实现)
一.题目 难度:简单 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数, 并返回它们的数组下标. 你可以假设每种输入只会对应一 ...
- 力扣(leetcode)题库0001-python3
试一下leetcode的题库,不知道对于我这种小白要多长时,但是目标已经种下,去做就是了.You can do anything you set your mind to. 题目:题库链接 中:给定一 ...
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- JS数据结构第六篇 --- 二叉树力扣练习题
1.第226题:翻转二叉树 递归+迭代两种实现方式: /** 反转二叉树 * Definition for a binary tree node. * function TreeNode(val) { ...
- 用GA算法设计22个地点之间最短旅程-R语言实现
数据挖掘入门与实战 公众号: datadw 相关帖子 转载︱案例 基于贪心算法的特征选择 用GA算法设计22个地点之间最短旅程-R语言实现 ----------------------------- ...
- 力扣1438. 绝对差不超过限制的最长连续子数组-C语言实现-中等难度
题目 传送门 文本 给你一个整数数组 nums ,和一个表示限制的整数 limit,请你返回最长连续子数组的长度,该子数组中的任意两个元素之间的绝对差必须小于或者等于 limit . 如果不存在满足条 ...
随机推荐
- c++实现类似python的map一样,批量操作一个vector的功能【python一样写c++、三】
python里有一个东西,叫map. 它可以实现像这样,对list每个元素进行操作,并返回新的list(python3是迭代器) 像这样 a=list(map(int,input().split()) ...
- 基于TDesign风格的Blazor企业级UI组件库
作为一名Web开发人员,开发前端少不了使用JavaScript,而Blazor就是微软推出的基于.net平台交互式客户 Web UI 框架,可以使用C#替代JavaScript,减少我们的技术栈.降低 ...
- 继续Vue的探索
接上集 上次到了想要利用Vue实现隔行变色的请求,但是由于使用的代码过于"高级"导致无法识别,这就需要利用webpack来解决它! webpack的基本使用 1.首先,在项目中安装 ...
- java多线程--5 同步方法和同步块synchronized
java多线程--5 同步方法和同步块synchronized 同步方法和同步块 同步方法:关键字synchronized,包括synchronized方法和synchronized块 public ...
- 针对im输入框的一种处理方式
针对im输入框的一种处理方式 <template> <div class="chatInput"> <!-- 通过contenteditable使普通 ...
- Mybatis分页插件PageHelper的配置及使用方法
尊重人家的知识成果 推荐 该作者总结的不错! --->> --->> @author 扎心了老铁 Mybatis分页插件PageHelper的配置及使用方法
- upload-labs游戏
upload-labs游戏 目录 upload-labs游戏 黑名单绕过方式 第1关:JavaScript前端验证 第2关:MIME类型的验证 第3关:特殊后缀的黑名单验证 第4关:.htaccess ...
- Docker 容器上部署 Zabbix
首先,从 Docker Hub 上拉取 Zabbix 镜像.可以使用以下命令: docker pull zabbix/zabbix-server-mysql:latest 这会下载最新版本的 Zabb ...
- GitHub+Hexo 搭建博客网站
Hexo是一款基于Node.js的静态博客框架,依赖少易于安装使用,可以方便的生成静态网页托管在GitHub和Heroku上,是搭建博客的首选框架. 配置Github root@hello:~/cby ...
- Sqlmap注入dvwa平台low级别
工具介绍:sqlmap是一款开源的软件 SQL注入攻击是黑客对数据库进行攻击的常用手段之一.随着B/S模式应用开发的发展,使用这种模式编写应用程序的程序员也越来越多.但是由于程序员的水平及经验也参差不 ...