成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

大展身手的字典樹(shù)

Anchorer / 1807人閱讀

摘要:原文地址在簡(jiǎn)單字典樹(shù)的實(shí)現(xiàn)一文中,我們以單詞輸入自動(dòng)提示為引子,簡(jiǎn)單介紹了字典樹(shù)的實(shí)現(xiàn)。前綴匹配本文講述前綴匹配的字典樹(shù)實(shí)現(xiàn)方案。在簡(jiǎn)單字典樹(shù)的實(shí)現(xiàn)一文中,我們已經(jīng)實(shí)現(xiàn)了字典樹(shù)的基本操作,這里只需要再加上一個(gè)前綴匹配方法即可。

原文地址

在簡(jiǎn)單字典樹(shù)(Trie)的實(shí)現(xiàn)一文中,我們以單詞輸入自動(dòng)提示為引子,簡(jiǎn)單介紹了字典樹(shù)的實(shí)現(xiàn)。那么,字典樹(shù)到底可以用于哪些場(chǎng)合呢?

前綴匹配:給定字典庫(kù),輸入一段字符,返回以該字符串為前綴的所有單詞。

字頻統(tǒng)計(jì):給出一段文本,統(tǒng)計(jì)其中指定單詞出現(xiàn)的頻數(shù)。

前綴匹配

本文講述前綴匹配的字典樹(shù)實(shí)現(xiàn)方案。仍然假設(shè)我們有以下單詞:apps apple cook cookie cold,當(dāng)我們想獲得以co為前綴的單詞時(shí),只需要在字典樹(shù)中依次找到c、o節(jié)點(diǎn),然后搜索o節(jié)點(diǎn)的所有子樹(shù),取出其中的單詞即可。

在簡(jiǎn)單字典樹(shù)(Trie)的實(shí)現(xiàn)一文中,我們已經(jīng)實(shí)現(xiàn)了字典樹(shù)的基本操作,這里只需要再加上一個(gè)前綴匹配方法即可。具體流程如下,將前綴字符串標(biāo)記為當(dāng)前前綴,將根節(jié)點(diǎn)標(biāo)記為當(dāng)前節(jié)點(diǎn),執(zhí)行操作1:

當(dāng)前前綴為空,對(duì)當(dāng)前節(jié)點(diǎn)執(zhí)行操作2。否則,取出當(dāng)前單詞的首字符,標(biāo)記為X,遍歷當(dāng)前節(jié)點(diǎn)的子節(jié)點(diǎn),如果X存在于子節(jié)點(diǎn)N中,將N標(biāo)記為當(dāng)前節(jié)點(diǎn),將剩余字符串標(biāo)記為當(dāng)前單詞,重復(fù)操作1;如果X不存在于子節(jié)點(diǎn)中,返回None。

以當(dāng)前節(jié)點(diǎn)為根節(jié)點(diǎn),進(jìn)行深度優(yōu)先搜索,取得當(dāng)前節(jié)點(diǎn)所有子樹(shù)下的所有單詞。

實(shí)現(xiàn)的偽代碼如下:

def pre_match_op(current_word, current_node):
    if current_word not empty:
        X = current_word[0]
        if X in current_node.child_node:
            current_word = current_word[1:]
            current_node = child_node
            return pre_match_op(current_word, current_node)
        else:
            return None
    else:
        return pre_match_bfs("", current_node)
        
def pre_match_dfs(keep_char, current_node):
    match_word = []
    for child in current_node.child_node:
        current_pre = pre_str + keep_char
        if child.isword = True:
            word = current_pre + child.char
            match_word.append(word)
        else:
            pass

        pre_match_dfs(current_pre, child)

    return match_word

具體程序以及測(cè)試?yán)臃旁趃ist上,可以在這里找到。測(cè)試了一下,兩千多個(gè)單詞,尋找共同前綴的單詞,速度還是蠻快的。

字頻統(tǒng)計(jì)

有時(shí)候我們需要統(tǒng)計(jì)一篇文章中一些單詞出現(xiàn)的次數(shù),這個(gè)時(shí)候用字典樹(shù)可以很方便的解決這個(gè)問(wèn)題。

在字典樹(shù)的簡(jiǎn)單實(shí)現(xiàn)中,我們?cè)O(shè)計(jì)的節(jié)點(diǎn)數(shù)據(jù)結(jié)構(gòu)如下:


圖1. 用list實(shí)現(xiàn)字典樹(shù)

只要對(duì)這里節(jié)點(diǎn)的數(shù)據(jù)結(jié)構(gòu)稍作修改,就可以用于統(tǒng)計(jì)字頻了。把原來(lái)數(shù)據(jù)結(jié)構(gòu)中的標(biāo)記位改為頻數(shù)位,即保存該單詞出現(xiàn)的次數(shù)。然后,再把原有字典樹(shù)實(shí)現(xiàn)中的插入操作和查找操作稍微改動(dòng),就可以實(shí)現(xiàn)字頻統(tǒng)計(jì)功能了。

插入操作:將單詞標(biāo)記為當(dāng)前單詞,將根節(jié)點(diǎn)標(biāo)記為當(dāng)前節(jié)點(diǎn),執(zhí)行操作1:

當(dāng)前單詞為空,當(dāng)前節(jié)點(diǎn)單詞出現(xiàn)頻數(shù)加1,終止操作;否則取出當(dāng)前單詞的首字符記為X,遍歷當(dāng)前節(jié)點(diǎn)的子節(jié)點(diǎn):如果X存在于子節(jié)點(diǎn)N,將剩余字符標(biāo)記為當(dāng)前單詞,將N標(biāo)記為當(dāng)前節(jié)點(diǎn),重復(fù)操作1,如果X不存在于當(dāng)前節(jié)點(diǎn)的子節(jié)點(diǎn)中,那么進(jìn)入操作2。

取出當(dāng)前單詞的首字符記為X,新建一個(gè)節(jié)點(diǎn)M存儲(chǔ)X,M的父節(jié)點(diǎn)為當(dāng)前節(jié)點(diǎn)。剩余字符串記為當(dāng)前單詞,如果當(dāng)前單詞為空,M節(jié)點(diǎn)單詞出現(xiàn)頻數(shù)加1,終止操作;否則,將M標(biāo)記為當(dāng)前節(jié)點(diǎn),重復(fù)操作2。

查詢操作:將單詞標(biāo)記為當(dāng)前單詞,將根節(jié)點(diǎn)標(biāo)記為當(dāng)前節(jié)點(diǎn),執(zhí)行操作1:

當(dāng)前單詞為空,返回當(dāng)前節(jié)點(diǎn)字頻數(shù),即為該單詞出現(xiàn)的次數(shù)。否則,取出當(dāng)前單詞的首字符,標(biāo)記為X,遍歷當(dāng)前節(jié)點(diǎn)的子節(jié)點(diǎn),如果X存在于子節(jié)點(diǎn)N中,將N標(biāo)記為當(dāng)前節(jié)點(diǎn),將剩余字符串標(biāo)記為當(dāng)前單詞,重復(fù)操作1;如果X不存在于子節(jié)點(diǎn)中,返回0。

實(shí)現(xiàn)偽代碼如下,插入操作如下:

def insert(word):
    current_word = word
    current_node = root
    insert_operation_1(current_word, current_node)

def insert_operation_1(current_word, current_node):
    if current_word not empty:
        X = current_word[0]

        if X in current_node.child:
            current_word = current_word[1:]
            current_node = child_node
            insert_operation_1(current_word, current_node)
        else:
            insert_operation_2(current_word, current_node)

    else:
        current_node.count++

def insert_operation_2(current_word, current_node):
    X = current_word[0]
    M.value = x
    M.father = current_node
    current_node.child = M

    current_word = current_word[1:]
    if current_word not empty:
        current_node = M
        insert_operation_2(current_word, current_node)

    else:
        current_node.count++

查詢操作:

def count(word):
    current_word = word
    current_node = root
    return find_opration(current_word, current_node)

def count_opration(current_word, current_node):
    if current_word not empty:
        X = current_word[0]
        if X in current_node.child_node:
            current_word = current_word[1:]
            current_node = child_node
            return find_opration(current_word, current_node)
        else:
            return 0
    else:
        return current_node.count

具體程序以及測(cè)試?yán)臃旁趃ist上,可以在這里找到。

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/37340.html

相關(guān)文章

  • 簡(jiǎn)單字典樹(shù)實(shí)現(xiàn)

    摘要:查找操作查詢指定單詞是否在字典樹(shù)中。將單詞標(biāo)記為當(dāng)前單詞,將根節(jié)點(diǎn)標(biāo)記為當(dāng)前節(jié)點(diǎn),執(zhí)行操作當(dāng)前單詞為空,那么返回,即字典樹(shù)中存在該單詞。字典樹(shù)的簡(jiǎn)單實(shí)現(xiàn)插入操作查找操作刪除操作具體實(shí)現(xiàn)放在上,可以在這里找到。 原文地址 字典樹(shù)介紹 我們經(jīng)常會(huì)在網(wǎng)上輸入一些單詞,一般情況下,當(dāng)我們輸入幾個(gè)字母時(shí),輸入框中會(huì)自動(dòng)彈出以這些字母開(kāi)頭的單詞供我們選擇,用戶體驗(yàn)非常好。 不過(guò)這種自動(dòng)提示功能到底...

    MonoLog 評(píng)論0 收藏0
  • Trie樹(shù) php 實(shí)現(xiàn)敏感詞過(guò)濾

    摘要:在樹(shù)中,每個(gè)節(jié)點(diǎn)表示一個(gè)狀態(tài),每條邊表示一個(gè)字符,從根節(jié)點(diǎn)到葉子節(jié)點(diǎn)經(jīng)過(guò)的邊即表示一個(gè)詞條。查找一個(gè)詞條最多耗費(fèi)的時(shí)間只受詞條長(zhǎng)度影響,因此的查找性能是很高的,跟哈希算法的性能相當(dāng)。 Last-Modified: 2019年5月10日15:25:35 參考文章 c++ 使用map實(shí)現(xiàn)Trie樹(shù) 關(guān)鍵詞過(guò)濾擴(kuò)展,用于檢查一段文本中是否出現(xiàn)敏感詞,基于Double-Array Trie...

    王笑朝 評(píng)論0 收藏0
  • 一種字典樹(shù)結(jié)構(gòu)高效實(shí)現(xiàn)

    摘要:另一種高效實(shí)現(xiàn)下面介紹另一種實(shí)現(xiàn),它將字典樹(shù)用數(shù)組存儲(chǔ)起來(lái),不僅壓縮了數(shù)組,而且不降低查找效率。這就是雙數(shù)組字典樹(shù)。 字典樹(shù)的心得體會(huì) 常見(jiàn)的字典樹(shù)實(shí)現(xiàn)方法 class Node{ uint node ; uint[] next; }; 或者類似如下結(jié)構(gòu) class Node{ uint node; map n...

    kycool 評(píng)論0 收藏0
  • 字典樹(shù)實(shí)現(xiàn)和介紹

    摘要:優(yōu)化老代碼的時(shí)候,用到了字典樹(shù)。我用寫(xiě)了一個(gè)字典樹(shù)。因?yàn)槭嵌嗖鏄?shù)結(jié)構(gòu),可能這兩個(gè)單詞,,需要一個(gè)結(jié)束的標(biāo)識(shí)位。但是應(yīng)該有相關(guān)的文本搜索算法和字典樹(shù)相結(jié)合。如果字典樹(shù)更新不頻繁,比如地名,字典樹(shù)是可以化,保存到中。 優(yōu)化老代碼的時(shí)候,用到了字典樹(shù)。我用Java寫(xiě)了一個(gè)字典樹(shù)。分享一下。 先說(shuō)一下常見(jiàn)的引用場(chǎng)景,單詞匹配,統(tǒng)計(jì)(敏感詞檢測(cè),單詞檢測(cè)),還有輸入提示等等。 下面是代碼了nod...

    cheukyin 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

Anchorer

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<