演算法—符號表,演算法符號

來源:互聯網
上載者:User

演算法—符號表,演算法符號

定義:符號表是一種儲存索引值對的資料結構,支援兩種操作:插入(put),即將一組新的索引值對存入表中;尋找(get),即根據給定的鍵得到相應的值。

1.有序符號表

2.成本模型

尋找的成本模型:在學習符號表的實現時,我們會統計比較的次數(等價性測試或是鍵的相互比較)。在內迴圈不進行比較(極少)的情況下,我們會統計數組的訪問次數。

 3.符號表的用例

/** * 符號表用例 */public class FrequencyCounter {public static void main(String[] args) {int minlen = Integer.parseInt(args[0]);//最小鍵長ST<String, Integer> st = new ST<String, Integer>();//構造符號表並統計頻率while(!StdIn.isEmpty()){String word = StdIn.readString();//忽略較短的單詞if(word.length() < minlen){continue;}if(!st.contains(word)){st.put(word, 1);}else{st.put(word, st.get(word) + 1);}}//找出出現頻率最高的單詞String max = " ";st.put(max, 0);for (String word : st.keys()) {if(st.get(word) > st.get(max)){max = word;}}StdOut.println(max + " " + st.get(max));}}

 

源碼下載

聯繫我們

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

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.