栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2022-01-22:力扣411,最短独占单词缩写。 给一个字符串数组strs和一个目标字符串target。target的简写不能跟strs打架。 strs是[“abcdefg“,“ccc“],tar

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

执行结果如下:


左神java代码

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/710733.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号