当前位置: 首页 > news >正文

如何做网站收录排名前50名免费的网站

如何做网站收录,排名前50名免费的网站,苏州网站设计公司价格,word模板免费下载文章目录 一、KMP算法说明二、详细实现1. next数组定义2. 使用next加速匹配3. next数组如何快速生成4. 时间复杂度O(mn)的证明a) next生成的时间复杂度b) 匹配过程时间复杂度 三、例题1. [leetcode#572](https://leetcode.cn/problems/subtree-of-another-tree/description/)2.…

文章目录

    • 一、KMP算法说明
    • 二、详细实现
      • 1. next数组定义
      • 2. 使用next加速匹配
      • 3. next数组如何快速生成
      • 4. 时间复杂度O(m+n)的证明
        • a) next生成的时间复杂度
        • b) 匹配过程时间复杂度
    • 三、例题
      • 1. [leetcode#572](https://leetcode.cn/problems/subtree-of-another-tree/description/)
      • 2. [leetcode#1367](https://leetcode.cn/problems/linked-list-in-binary-tree/description/)

一、KMP算法说明

要判断s1字符串是否包含s2字符串,如果包含返回s1中包含s2的最左开头位置,不包含返回-1

暴力方法就是s1的每个位置都做开头,然后去匹配s2整体,时间复杂度O(n*m),其中n为s1长度,m为s2长度

KMP算法可以做到时间复杂度O(n+m)

二、详细实现

1. next数组定义

字符串s的next数组为int数组,长度等于s的长度。next[i]表示在s中下标i之前子串的前缀和后缀的最大匹配长度(不包含整体)

以字符串"aabaabs"为例

// i=0,规定next[0]为-1
// i=1,由于s[1]之前只有a,除去整体,前缀和后缀只能是空,所以规定next[1]=0
// i=2, "aa",前缀"a",后缀"a",最大匹配长度1,next[2]=1
// i=3, "aab",没有可以匹配的前缀和后缀,next[3]=0
// i=4, "aaba", 前缀"a", 后缀"a", next[4]=1
// i=5, "aabaa", 前缀"aa", 后缀"aa", next[5]=2
// i=6, "aabaab", 前缀"aab", 后缀"aab", next[6]=3
// 扩充的next是可以多计算一位的
// i=7, "aabaabs",没有可以匹配的前缀和后缀,next[7]=0

2. 使用next加速匹配

func kmp(s1, s2 string) int {if len(s1) < len(s2) {return -1}next := nextArr(s2)x, y := 0, 0for x < len(s1) && y < len(s2) {if s1[x] == s2[y] {x++y++} else if y > 0 {y = next[y]} else {x++}}if y == len(s2) {return x - y} else {return -1}
}

3. next数组如何快速生成

func nextArr(s string) []int {if len(s) <= 1 {return []int{-1}}next := make([]int, len(s))next[0], next[1] = -1, 0cp := 0for i := 2; i < len(s); {if s[i-1] == s[cp] {cp++next[i] = cpi++} else if cp > 0 {cp = next[cp]} else {next[i] = 0i++}}return next
}

4. 时间复杂度O(m+n)的证明

a) next生成的时间复杂度
// for循环中我们关注i和i-cp
// i的范围是2~m
// i-cp的范围是0~m
// 分支1:i变大, i-cp不变
// 分支2:i-cp变大
// 分支3:i变大,i-cp变大
// 因此时间复杂度O(m)
b) 匹配过程时间复杂度
// for循环中关注x和x-y
// ...
// 同理时间复杂度O(n)

三、例题

1. leetcode#572

image-20240328005922263

// 思路:将两棵树都序列化为sRoot和sSubRoot,然后判断sSubRoot是否为sRoot的子串func isSubtree(root *TreeNode, subRoot *TreeNode) bool {const nullVal = 1e4 + 1var s1, s2 []ints1 = encode(root, make([]int, 0), nullVal)s2 = encode(subRoot, make([]int, 0), nullVal)return kmp2(s1, s2) >= 0
}
func encode(root *TreeNode, list []int, nullVal int) []int {if root == nil {list = append(list, nullVal)return list}list = append(list, root.Val)list = encode(root.Left, list, nullVal)list = encode(root.Right, list, nullVal)return list
}
func kmp2(s1, s2 []int) int {if len(s1) < len(s2) {return -1}next := nextArrInt(s2)x, y := 0, 0for x < len(s1) && y < len(s2) {if s1[x] == s2[y] {x++y++} else if y > 0 {y = next[y]} else {x++}}if y == len(s2) {return x - y} else {return -1}
}
func nextArrInt(s []int) []int {if len(s) <= 1 {return []int{-1}}next := make([]int, len(s))next[0], next[1] = -1, 0cp := 0for i := 2; i < len(s); {if s[i-1] == s[cp] {cp++next[i] = cpi++} else if cp > 0 {cp = next[cp]} else {next[i] = 0i++}}return next
}

2. leetcode#1367

image-20240328010252351

func isSubPath(head *ListNode, root *TreeNode) bool {if head == nil {return true}if root == nil {return false}list := make([]int, 0)for head != nil {list = append(list, head.Val)head = head.Next}next := nextArrInt(list)return find(root, list, next, 0)
}func find(cur *TreeNode, list []int, next []int, index int) bool {if index == len(list) {return true}if cur == nil {return false}for index >= 0 && cur.Val != list[index] {index = next[index]}// index=-1 => index=0// 匹配 => index+1index++return find(cur.Left, list, next, index) || find(cur.Right, list, next, index)
}
http://www.hengruixuexiao.com/news/48139.html

相关文章:

  • wordpress 查询模板东营seo整站优化
  • 企业网页与网站区别8大营销工具
  • 网络科技公司网站源码下载百度风云榜小说排行榜历届榜单
  • 城乡建设官方网站福州seo扣费
  • wordpress 屏蔽ftp广州seo招聘
  • 成都高端网站建设悟空建站seo服务
  • 手机网站类型网络营销课程感悟
  • win2003 做网站服务器seo快速优化
  • 什么网站可以免费发布招聘信息河北百度推广客服电话
  • 石家庄做外贸的网站网站注册地址查询
  • 广西人社服务器异常网站优化推广seo
  • 临沂网站建设公司厦门人才网招聘官网
  • app软件程序开发长沙seo袁飞
  • 什么网站可以在线做高中题目中国职业培训在线官方网站
  • 佛山做网站的哪个好百度排名优化工具
  • 石家庄网站建设行业公司东莞网络营销网站建设
  • 做网站的劣势seo站长工具平台
  • 企业解决方案供应商有哪些保定seo推广
  • 如何做一家专门卖零食的网站百度指数如何分析
  • 东莞企石网站建设广州seo排名收费
  • 做网站卖产品要注册公司吗百度有钱花人工客服
  • 管局审核网站名称微信营销软件手机版
  • 北京专业制作网站公司seo主要做什么
  • 长春建设网站六年级上册数学优化设计答案
  • 惠州 家具 网站上线百度关键词优化专家
  • 区块链媒体网站建设网络营销主要特点有哪些
  • 普宁网站建设站长之家网站介绍
  • wordpress 用户站点搜索引擎调词工具哪个好
  • 怎样做网站4042023新闻大事10条
  • 河北新亚建设集团网站百度推广平台登录入口