国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁 > news >正文

每天做特賣的網(wǎng)站是哪個關(guān)鍵詞優(yōu)化推廣排名

每天做特賣的網(wǎng)站是哪個,關(guān)鍵詞優(yōu)化推廣排名,知乎 淘寶網(wǎng)站建設(shè),網(wǎng)絡(luò)銷售工作怎么樣文章目錄 一、KMP算法說明二、詳細(xì)實現(xiàn)1. next數(shù)組定義2. 使用next加速匹配3. next數(shù)組如何快速生成4. 時間復(fù)雜度O(mn)的證明a) next生成的時間復(fù)雜度b) 匹配過程時間復(fù)雜度 三、例題1. [leetcode#572](https://leetcode.cn/problems/subtree-of-another-tree/description/)2.…

文章目錄

    • 一、KMP算法說明
    • 二、詳細(xì)實現(xiàn)
      • 1. next數(shù)組定義
      • 2. 使用next加速匹配
      • 3. next數(shù)組如何快速生成
      • 4. 時間復(fù)雜度O(m+n)的證明
        • a) next生成的時間復(fù)雜度
        • b) 匹配過程時間復(fù)雜度
    • 三、例題
      • 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整體,時間復(fù)雜度O(n*m),其中n為s1長度,m為s2長度

KMP算法可以做到時間復(fù)雜度O(n+m)

二、詳細(xì)實現(xiàn)

1. next數(shù)組定義

字符串s的next數(shù)組為int數(shù)組,長度等于s的長度。next[i]表示在s中下標(biāo)i之前子串的前綴和后綴的最大匹配長度(不包含整體)

以字符串"aabaabs"為例

// i=0,規(guī)定next[0]為-1
// i=1,由于s[1]之前只有a,除去整體,前綴和后綴只能是空,所以規(guī)定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
// 擴(kuò)充的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數(shù)組如何快速生成

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. 時間復(fù)雜度O(m+n)的證明

a) next生成的時間復(fù)雜度
// for循環(huán)中我們關(guān)注i和i-cp
// i的范圍是2~m
// i-cp的范圍是0~m
// 分支1:i變大, i-cp不變
// 分支2:i-cp變大
// 分支3:i變大,i-cp變大
// 因此時間復(fù)雜度O(m)
b) 匹配過程時間復(fù)雜度
// for循環(huán)中關(guān)注x和x-y
// ...
// 同理時間復(fù)雜度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://aloenet.com.cn/news/41990.html

相關(guān)文章:

  • 蘇州企業(yè)網(wǎng)站seo怎么關(guān)閉seo綜合查詢
  • 聊城做網(wǎng)站最好的網(wǎng)絡(luò)公司網(wǎng)絡(luò)宣傳方案
  • 無限制的網(wǎng)站訪問網(wǎng)站推廣的具體方案
  • 點墨網(wǎng)站網(wǎng)站seo設(shè)置是什么
  • 手機(jī)咋做網(wǎng)站微博指數(shù)查詢
  • 動漫設(shè)計與游戲制作專業(yè)長沙seo招聘
  • 網(wǎng)站正在升級建設(shè)中代碼seo優(yōu)化培訓(xùn)課程
  • 模板網(wǎng)站建設(shè)珠海廣東又出現(xiàn)新病毒
  • 杭州建設(shè)網(wǎng) 執(zhí)法人員名單seo營銷名詞解釋
  • 國外網(wǎng)站國內(nèi)做二維碼企業(yè)網(wǎng)站營銷的典型案例
  • 學(xué)生管理系統(tǒng) 靜態(tài)網(wǎng)站源碼如何開展網(wǎng)絡(luò)營銷活動
  • 做分銷的官網(wǎng)網(wǎng)站北京整站線上推廣優(yōu)化
  • 如何做行業(yè)網(wǎng)站寧波網(wǎng)站優(yōu)化
  • 怎么做學(xué)校網(wǎng)站和微信公眾號域名查詢 ip
  • 東阿網(wǎng)站建設(shè)百度seo流量
  • 我想注冊公司怎么注冊網(wǎng)站排名優(yōu)化服務(wù)
  • wordpress拷站發(fā)布外鏈的平臺有哪些
  • 網(wǎng)站被入侵后需做的檢測(1)唯尚廣告聯(lián)盟
  • 重慶云陽網(wǎng)站建設(shè)公司推薦沈陽網(wǎng)站seo公司
  • 建設(shè)廳官方網(wǎng)站企業(yè)庫網(wǎng)絡(luò)賺錢推廣
  • 江蘇建設(shè)網(wǎng)站bt磁力兔子引擎
  • 青建集團(tuán)股份有限公司sem推廣優(yōu)化
  • 搭建什么網(wǎng)站好玩唐山百度seo公司
  • wordpress和laravel鄭州seo關(guān)鍵詞
  • 做良心網(wǎng)站seo的優(yōu)化步驟
  • 企業(yè)網(wǎng)站管理seo1視頻發(fā)布會
  • 怎么樣做國際網(wǎng)站生意seo知識培訓(xùn)
  • 鄭州旅游網(wǎng)站設(shè)計小學(xué)生一分鐘新聞播報
  • 企業(yè)管理培訓(xùn)課程課件南寧seo外包服務(wù)
  • 網(wǎng)站開發(fā)到發(fā)布佛山優(yōu)化推廣