JavaScript中資料結構與演算法(四):串(BF)

來源:互聯網
上載者:User

   這篇文章主要介紹了JavaScript中資料結構與演算法(四):串(BF),串是由零個或多個字元組成的有限序列,又叫做字串,本文著重講解了BF(Brute Force)演算法,需要的朋友可以參考下

  串是由零個或多個字元組成的有限序列,又叫做字串

  串的邏輯結構和線性表很相似的,不同的是串針對是是字元集,所以在操作上與線性表還是有很大區別的。線性表更關注的是單個元素的操作CURD,串則是關注尋找子串的位置,替換等操作。

  當然不同的進階語言對串的基本操作都有不同的定義方法,但是總的來說操作的本質都是相似的。比如javascrript尋找就是indexOf, 去空白就是trim,轉化大小寫toLowerCase/toUpperCase等等

  這裡主要討論下字串模式比對的幾種經典的演算法:BF、BM、KMP

  BF(Brute Force)演算法

  Brute-Force演算法的基本思想:

  從目標串s 的第一個字元起和模式串t的第一個字元進行比較,若相等,則繼續逐個比較後續字元,否則從串s 的第二個字元起再重新和串t進行比較。

  依此類推,直至串t 中的每個字元依次和串s的一個連續的字元序列相等,則稱模式比對成功,此時串t的第一個字元在串s 中的位置就是t 在s中的位置,否則模式比對不成功

  可見BF演算法是一種暴力演算法,又稱為樸素匹配演算法或蠻力演算法。

  主串 BBC ABB ABCF

  子串 ABC

  在主串中找出子串的位置,對應了其實就是javascript的indexOf尋找方法的實現了

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 var sourceStr = "BBC ABB ABCF"; var searchStr = "ABC";   function BF_Ordinary(sourceStr, searchStr) { var sourceLength = sourceStr.length; var searchLength = searchStr.length; var padding = sourceLength - searchLength; //迴圈的次數 //BBC ABB ABCF =>ABC => 搜尋9次 for (var i = 0; i <= padding; i++) { //如果滿足了第一個charAt是相等的 //開始子迴圈檢測 //其中sourceStr的取值是需要疊加i的值 if (sourceStr.charAt(i) == searchStr.charAt(0)) { //匹配成功的資料 var complete = searchLength; for (var j = 0; j < searchLength; j++) { if (sourceStr.charAt(i + j) == searchStr.charAt(j)) { --complete if (!complete) { return i; } } } } } return -1; }

  BF演算法就是簡單粗暴,直接把BBC ABB ABCF母串的每一個字元的下表取出來與模式串的第一個字元匹配,如果相等就進去字串的再次匹配

  這裡值得注意:

  1:最外圍迴圈的次數sourceLength - searchLength,因為我們匹配的母串至少要大於等於子串

  2:在子串的繼續匹配中,母串的起點是需要疊加的(i+j)

  3:通過一個條件判斷是否完全符合complete,BBC ABB ABCF中,我們在ABB的時候就需要跳過去

  上面是最簡單的一個演算法了,代碼上還有更優的處理,比如在自串的匹配上可以採取取反的演算法

  最佳化演算法(一)

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 function BF_Optimize(sourceStr, searchStr) { var mainLength = sourceStr.length; var searchLength = searchStr.length; var padding = mainLength - searchLength; for (var offset = 0; offset <= padding; offset++) { var match = true; for (var i = 0; i < searchLength; i++) { //取反,如果只要不相等 if (searchStr.charAt(i) !== sourceStr.charAt(offset + i)) { match = false; break; } } if (match) return offset; } return -1; }

  我們不需要判斷為真的情況,我們只要判斷為假的情況就可以了,當子匹配結束後match沒有被修改過的話,則說明此匹配是完全符合

  以上2種方法我們都用到了子迴圈,我們能否改成一個迴圈體呢?

  其實我們可以看到規律,主串每次都只會遞增+1,子串每次匹配也是從頭開始匹配,所以我們可以改成一個while,控制下標指標就可以了

  最佳化演算法(二)

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 function BF_Optimize_2(sourceStr, searchStr) { var i = 0, j = 0;   while (i < sourceStr.length) { // 兩字母相等則繼續 if (sourceStr.charAt(i) == searchStr.charAt(j)) { i++; j++; } else { // 兩字母不等則角標後退重新開始匹配 i = i - j + 1; // i 回退到上次匹配首位的下一位 j = 0; // j 回退到子串的首位 }   if (j == searchStr.length) { return i - j; }   } }

  i就是主串的下標定位,j就是子串的下標定位

  當主串子串相等的時候,就進入了子串的迴圈模式,當子迴圈的次數j滿足子串長度時,就驗證是完全符合

  當主串子串不相等的時候,就需要把主串的下標往後移一位,當然i的時候,因為可能經過子串的處理,所以需要i-j+1, 然後複位子串

  具體我們可以看看代碼比較

  基於BF演算法的四種結構,for/while/遞迴

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 <!doctype html>由於電腦效能的不斷提高,測試的資料量的大小,可能會導致得到的結果不太準確;<script type="text/javascript">   ///////// //暴力演算法 // //普通版 ///////// function BF_Ordinary(sourceStr, searchStr) { var sourceLength = sourceStr.length; var searchLength = searchStr.length; var padding = sourceLength - searchLength; //迴圈的次數   //BBC ABB ABCF =>ABC => 搜尋9次 for (var i = 0; i <= padding; i++) { //如果滿足了第一個charAt是相等的 //開始子迴圈檢測 //其中sourceStr的取值是需要疊加i的值 if (sourceStr.charAt(i) == searchStr.charAt(0)) { //匹配成功的資料 var complete = searchLength; for (var j = 0; j < searchLength; j++) { if (sourceStr.charAt(i + j) == searchStr.charAt(j)) { --complete if (!complete) { return i; } } } } } return -1; }     ///////// //暴力演算法 // //最佳化版 ///////// function BF_Optimize_1(sourceStr, searchStr) { var mainLength = sourceStr.length; var searchLength = searchStr.length; var padding = mainLength - searchLength; for (var offset = 0; offset <= padding; offset++) { var match = true; for (var i = 0; i < searchLength; i++) { //取反,如果只要不相等 if (searchStr.charAt(i) !== sourceStr.charAt(offset + i)) { match = false; break; } } if (match) return offset; } return -1; }     //////// //最佳化版 // //while //////// function BF_Optimize_2(sourceStr, searchStr) { var i = 0, j = 0;   while (i < sourceStr.length) { // 兩字母相等則繼續 if (sourceStr.charAt(i) == searchStr.charAt(j)) { i++; j++; } else { // 兩字母不等則角標後退重新開始匹配 i = i - j + 1; // i 回退到上次匹配首位的下一位 j = 0; // j 回退到子串的首位 }   if (j == searchStr.length) { return i - j; }   } }     ///////// //暴力演算法 //遞迴版本 ///////// function BF_Recursive(sourceStr, searchStr, offset) { var mainLength = sourceStr.length; var searchLength = searchStr.length; if (searchLength > mainLength - offset) { return -1; } offset = offset || 0; for (var i = 0; searchLength > i; i++) { if (searchStr.charAt(i) !== sourceStr.charAt(offset + i)) { return BF_Recursive(sourceStr, searchStr, offset + 1) } } return offset; }       var sourceStr = "There are some times wThere are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely thinkhen clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer There are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely think that that Facebook would “definitely think about” adding a dislike button. “People definitely seem to want it,” Zuckerberg said. Four years later — Zuckerberg says Facebook is still “thinking about” adding the oft-requested sdfafd button, Zuckerberg says Facebook is still “thinking about” adding the oft-requested button. At a town hall meeting on Thursday, the CEO revealed he has some reservations about the feature. “There are two things that it can mean,” Zuckerberg said of the potential button, which could be used in a mean spirited way or to express empathy. Finding how to limit it to the latter is the challenge. Zuckerberg said he doesn't want the button to turn into a “voting mechanism” or something that isn't “socially valuable.” “Often people will tell us they don't feel comfortable pressing ‘like,'” Zuckerberg said. “What's the right way to make it so people can easier express a wide range of emotions?” One suggestion percolating online: Aaron Roll out the feature under a different name. However, an “empathy button” just may not have the same ring to it as “dislike.”"; var searchStr = "adding the oft-requested sdf";       function show(bf_name,fn) { var myDate = +new Date() var r = fn(); var div = document.createElement('div') div.innerHTML = bf_name +'演算法,搜尋位置:' + r + ",耗時" + (+new Date() - myDate) + "ms"; document.body.appendChild(div); }   show('BF_Ordinary',function() { return BF_Ordinary(sourceStr, searchStr) })   show('BF_Optimize_1',function() { return BF_Optimize_1(sourceStr, searchStr) })   show('BF_Optimize_2',function() { return BF_Optimize_2(sourceStr, searchStr) })   show('BF_Recursive',function() { return BF_Recursive(sourceStr, searchStr) })   </script>

  BF也是經典的首碼匹配演算法,首碼還包括KMP,我們可見這種演算法最大缺點就是字元匹配失敗指標就要回溯,所以效能很低,之後會寫一下KMP與BM演算法針對BF的的升級

相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。