{"id":7481,"date":"2023-05-12T13:23:23","date_gmt":"2023-05-12T05:23:23","guid":{"rendered":"https:\/\/www.5x44.cn\/?p=7481"},"modified":"2023-05-12T13:23:23","modified_gmt":"2023-05-12T05:23:23","slug":"ahocorasick%e5%ba%93%e7%9a%84%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=7481","title":{"rendered":"ahocorasick\u5e93\u7684\u5e94\u7528"},"content":{"rendered":"\n<p>ahocorasick\u5e93\u7684\u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/pyahocorasick.readthedocs.io\/en\/latest\/<\/p>\n\n\n\n<p><strong>Pyahocorasick<\/strong>\u662f\u4e00\u4e2a\u5feb\u901f\u4e14\u8282\u7701\u5185\u5b58\u7684\u5e93\uff0c\u7528\u4e8e\u7cbe\u786e\u6216\u6a21\u7cca\u7684\u591a\u6a21\u5f0f\u5b57\u7b26\u4e32\u641c\u7d22\uff0c\u8fd9\u610f\u5473\u7740\u60a8\u53ef\u4ee5\u5728\u8f93\u5165\u6587\u672c\u4e2d\u540c\u65f6\u53d1\u73b0\u591a\u4e2a\u5173\u952e\u5b57\u7b26\u4e32\u3002\u5b57\u7b26\u4e32\u201cindex\u201d\u53ef\u4ee5\u63d0\u524d\u6784\u5efa\u5e76\u4fdd\u5b58(\u4f5c\u4e3apickle)\u5230\u78c1\u76d8\uff0c\u4ee5\u4fbf\u91cd\u65b0\u52a0\u8f7d\u548c\u4ee5\u540e\u91cd\u7528\u3002\u8be5\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2aahocorasick Python\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u5c06\u5176\u7528\u4f5c\u666e\u901a\u7684\u7c7b\u4f3cdict\u7684Trie\u6811\uff0c\u4e5f\u53ef\u4ee5\u5c06Trie\u6811\u8f6c\u6362\u4e3a\u81ea\u52a8\u673a\uff0c\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684Aho-Corasick\u641c\u7d22\u3002<\/p>\n\n\n\n<p>\u7ec3\u4e60\u9898\uff1a\uff08\u8fd9\u9898\u7684\u672c\u610f\u662f\u8ba9\u7ec3\u4eba\u8005\u81ea\u5df1\u5b9e\u73b0Aho-Corasick\u641c\u7d22\uff0c\u600e\u5948\u81ea\u5df1\u7684\u6c34\u5e73\u4e0d\u884c\uff0c\u7406\u89e3\u4e0d\u4e86\u8fd9\u7b97\u6cd5\uff0c\u53ea\u80fd\u7528\u73b0\u6210\u7684\u5e93\u5b9e\u73b0\uff0c\u505a\u4e3a\u8bb0\u5f55\u3002\uff09<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u7a0b\u5e8f\u7684\u7b80\u5355\u8f93\u5165\u6d4b\u8bd5\u6570\u636e\uff0c\u6839\u636e\u8fd9\u4e2a\u6d4b\u8bd5\u6570\u636e\uff0c\u5c55\u5f00\u9898\u76ee\uff08\u56e0\u4e3a\u9898\u76ee\u592a\u957f\u4e86\uff09\u3002<\/p>\n\n\n\n<p>\u8f93\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">6\r\na b c aa d b\r\n1 2 3 4 5 6\r\n3\r\n1 5 caaab\r\n0 4 xyz\r\n2 4 bcdybc<\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">0 19<\/pre>\n\n\n\n<p>n=6\uff0c\u8fd9\u4ee3\u8868\u5173\u952e\u5b57genes\u67096\u4e2a\u3002<\/p>\n\n\n\n<p>genes=[&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;, &#8216;aa&#8217;, &#8216;d&#8217;, &#8216;b&#8217;]\uff0c\u8fd9\u662f\u5173\u952e\u5b57<\/p>\n\n\n\n<p>health=[1, 2, 3, 4, 5, 6]\uff0c\u8fd9\u662f\u503c<\/p>\n\n\n\n<p>s=3\uff0c\u4ee3\u8868\u4e0b\u9762\u8981\u8fdb\u884c3\u6b21\u5173\u952e\u5b57\u7684\u641c\u7d22<\/p>\n\n\n\n<p>[1, 5, &#8216;caaab&#8217;]\uff0cfirst=1\uff0clast=5\uff0cd=&#8217;caaab&#8217;\uff0cfirst\u4ee3\u8868\u641c\u7d22\u5173\u952e\u5b57\u7684\u5f00\u59cb\u4f4d\u7f6e\uff08\u5305\u542b\u8be5\u4f4d\u7f6e\uff0c\u4f4d\u7f6e\u4ece0\u5f00\u59cb\uff09\uff0clast\u4ee3\u8868\u641c\u7d22\u5173\u952e\u5b57\u7684\u7ed3\u675f\u4f4d\u7f6e\uff08\u5305\u542b\u8be5\u4f4d\u7f6e\uff0c\u4f4d\u7f6e\u4ece0\u5f00\u59cb\uff09\uff0cd\u4ee3\u8868\u641c\u7d22\u5b57\u7b26\u4e32<\/p>\n\n\n\n<p>[0,4, &#8216;xyz]\uff0c\u540c\u4e0a<\/p>\n\n\n\n<p>[2, 4, &#8216;bcdybc&#8217;]\uff0c\u540c\u4e0a<\/p>\n\n\n\n<p>\u5173\u952e\u5b57\u548c\u503c\u662f\u5bf9\u5e94\u7684\uff08\u53ef\u4ee5\u628a\u5176\u770b\u6210\u662f\u5b57\u5178\uff0c\u4f46\u662f\u8981\u6ce8\u610f\u5176\u4e2d\u662f\u6709\u91cd\u590d\u7684\u5173\u952e\u5b57\uff0c\u4f46\u662f\u62e5\u6709\u4e0d\u540c\u7684\u503c\uff09\uff0c\u5173\u952e\u5b57\u5728first\u548clast\u7684\u8303\u56f4\u5185\uff0c\u67e5\u627ed\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u5e76\u8ba1\u7b97\u5173\u952e\u5b57\u5bf9\u5e94\u7684\u503c\u7684\u603b\u548c\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64&#8217;caaab&#8217;\u7684\u603b\u5408\u662f3+4+4+2+6=19\uff0c\u5176\u4e2d\u7684&#8217;aaa&#8217;\u8981\u5206\u89e3\u6210&#8217;<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">aa<\/mark><\/strong>a&#8217;\u548c&#8217;a<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><strong>aa<\/strong><\/mark>&#8216;\uff0c\u6240\u4ee5\u662f\u4e24\u4e2a4\u3002\u56e0\u4e3afirst\u7684\u4f4d\u7f6e\u662f1,\u6240\u4ee5\u4f4d\u7f6e\u4e3a0\u7684\u5173\u952e\u5b57&#8217;a&#8217;\u5c31\u88ab\u8df3\u8fc7\u4e86\u3002\u540c\u65f6\u56e0\u4e3ab\u6709\u4e24\u4e2a\u503c\uff0c\u4e00\u4e2a\u662f2\uff0c\u53e6\u4e00\u4e2a\u662f6\uff0c\u800c\u53d6\u503c\u8303\u56f4\u8fd8\u5305\u542b\u4e86\u8fd9\u4e24\u4e2a\u503c\u7684\u4f4d\u7f6e\uff0c\u6240\u4ee5\u8fd9\u4e24\u4e2a\u503c\u90fd\u8981\u8ba1\u7b97\u3002<\/p>\n\n\n\n<p>\u800c&#8217;xyz&#8217;\u6ca1\u6709\u5bf9\u5e94\u7684\u5173\u952e\u5b57\uff0c\u56e0\u6b64\u603b\u5408\u662f0<\/p>\n\n\n\n<p>\u800c&#8217;bcdybc&#8217;\u4e2d\u53ea\u6709\u90e8\u5206\u5173\u952e\u5b57\u5b58\u5728\uff0c\u6240\u4ee5\u603b\u5408\u662f3+5+3=11<\/p>\n\n\n\n<p>\u9898\u76ee\uff1a\u5728s\u6b21\u7684\u641c\u7d22\u4e2d\u627e\u51fa\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u3002<\/p>\n\n\n\n<p>\u8c03\u7528ahocorasick\u5e93\u7684\u7a0b\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import ahocorasick\ndef func2():\r\n    trie=ahocorasick.Automaton()\r\n\r\n    n = int(input().strip())\r\n\r\n    genes = input().rstrip().split()\r\n\r\n    health = list(map(int, input().rstrip().split()))\r\n\r\n    s = int(input().strip())\r\n    i=0\r\n\r    #<strong>\u5411trie\u4e2d\u6dfb\u52a0\u5173\u952e\u5b57\uff0c\u5173\u952e\u5b57\u662f\u5217\u8868\u7684\u539f\u56e0\u662f\u8981\u8f93\u5165\u91cd\u590d\u7684\u5173\u952e\u5b57\uff0c\u800c\u4e14ahocorasick\u7684\u5b9e\u4f8b\u4e2d\u8f93\u5165\u91cd\u590d\u7684\u5173\u952e\u5b57\u65f6\uff0c\u4f1a\u5c06\u65e7\u7684\u503c\u8986\u76d6\u3002<\/strong>add_word(\u5173\u952e\u5b57,\u503c\uff09\n    for k,v in zip(genes,health):\r\n        if k in trie.keys():\r\n            trie.get(k).append((i,v,))\r\n        else:\r\n            trie.add_word(k,&#91;(i,v,)])\r\n        i = i + 1\r\n\r    # \u4f7ftrie\u53d8\u6210Aho-Corasick\u81ea\u52a8\u673a\uff0c\u8ba9trie\u53ef\u4ee5\u8fdb\u884c Aho-Corasick\u641c\u7d22\n    trie.make_automaton()\r\n    result = &#91;]\r\n    for s_itr in range(s):\r\n        first_multiple_input = input().rstrip().split()\r\n\r\n        first = int(first_multiple_input&#91;0])\r\n\r\n        last = int(first_multiple_input&#91;1])\r\n\r\n        d = first_multiple_input&#91;2]\r\n        count_genes=0\n        #\u5728trie\u4e2d\u8fdb\u884c Aho-Corasick\u641c\u7d22\uff0cd\u5c31\u662f\u88ab\u641c\u7d22\u7684\u5b57\u7b26\u4e32\uff0ctrie.iter(d)\u53ef\u4ee5\u8fd4\u56de\u6240\u6709\u627e\u5230\u7684\u5173\u952e\u5b57\uff0c\u5176\u8fd4\u56de\u503c\u662f\uff08\u4f4d\u7f6e\uff0c\u503c\uff09\u8fd9\u6837\u7684\u5143\u7956\u3002<strong>\u4f4d\u7f6e<\/strong>\u662f\u5173\u952e\u5b57\u5728d\u4e2d\u7684\u4f4d\u7f6e\uff0c<strong>\u503c<\/strong>\u5c31\u662f\u5173\u952e\u5b57\u5bf9\u5e94\u7684\u503c\u3002\r\n        for i in trie.iter(d):\r\n            for j in i&#91;1]:\r\n                if first&lt;=j&#91;0]&lt;=last:\r\n                    count_genes+=j&#91;1]\r\n        result.append(count_genes)\r\n    print(f'{min(result)} {max(result)}')<\/code><\/pre>\n\n\n\n<div class=\"wp-block-file\"><a id=\"wp-block-file--media-82baf652-e8a8-41f7-8515-8fdb3e519f3f\" href=\"https:\/\/www.5x44.cn\/wp-content\/uploads\/2023\/05\/Test.7z\">Test<\/a><a href=\"https:\/\/www.5x44.cn\/wp-content\/uploads\/2023\/05\/Test.7z\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-82baf652-e8a8-41f7-8515-8fdb3e519f3f\">\u4e0b\u8f7d<\/a><\/div>\n\n\n\n<pre class=\"wp-block-preformatted\">test\u4e2d\u6709\u4e09\u4e2a\u6d4b\u8bd5\u6570\u636e\uff0c\u7ed3\u679c\u5982\u4e0b\uff1a\n    result={\r\n        'test_data0.txt':'0 19',\r\n        'test_data1.txt':'11674463 11674463',\r\n        'test_data2.txt':'15806635 20688978289',\r\n    }<\/pre>\n\n\n\n<p>\u5982\u679c\u7528\u66b4\u529b\u7684\u6bd4\u5bf9\u65b9\u5f0f\u7684\u8bdd\uff0c\u540e\u4e24\u4e2a\u6d4b\u8bd5\u6570\u636e\u8fd0\u884c\u65f6\u95f4\u53ef\u9ad8\u8fbe\u5341\u51e0\u5206\u949f\uff0c\u4f7f\u7528 Aho-Corasick\u641c\u7d22\uff0c1\u5206\u949f\u5185\u5c31\u53ef\u4ee5\u5f97\u5230\u7ed3\u679c\u3002<\/p>\n\n\n\n<div data-wp-interactive=\"core\/file\" class=\"wp-block-file\"><object data-wp-bind--hidden=\"!state.hasPdfPreview\" hidden class=\"wp-block-file__embed\" data=\"https:\/\/www.5x44.cn\/wp-content\/uploads\/2023\/05\/Incremental-Construction-of-Minimal-Acyclic-Finite-State-Automata.pdf\" type=\"application\/pdf\" style=\"width:100%;height:600px\" aria-label=\"\u5d4c\u5165 Incremental-Construction-of-Minimal-Acyclic-Finite-State-Automata\"><\/object><a id=\"wp-block-file--media-bcf103f2-31fe-4573-9fdf-a9905415feec\" href=\"https:\/\/www.5x44.cn\/wp-content\/uploads\/2023\/05\/Incremental-Construction-of-Minimal-Acyclic-Finite-State-Automata.pdf\">Incremental-Construction-of-Minimal-Acyclic-Finite-State-Automata<\/a><a href=\"https:\/\/www.5x44.cn\/wp-content\/uploads\/2023\/05\/Incremental-Construction-of-Minimal-Acyclic-Finite-State-Automata.pdf\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-bcf103f2-31fe-4573-9fdf-a9905415feec\">\u4e0b\u8f7d<\/a><\/div>\n\n\n\n<p>\u8fd9\u7bc7\u7b97\u6cd5\u7684\u8bba\u6587\u53ef\u4ee5\u8ba9\u6211\u4eec\u66f4\u597d\u7684\u7406\u89e3Aho-Corasick\u641c\u7d22\uff0c\u4f46\u662f\u6211\u653e\u5f03\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ahocorasick\u5e93\u7684\u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/pyahocorasick.readthedocs.io\/en\/latest\/ Pyahocorasick\u662f&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=7481\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-7481","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7481"}],"version-history":[{"count":1,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7481\/revisions"}],"predecessor-version":[{"id":7484,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7481\/revisions\/7484"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}