Algorithms-3.5 Symbol Table Applications 符号表应用

1 sets 集合的API

  • 比symbol table简单,因为不需要处理值,只处理表中所有键的集合,和相应的值无关
    在这里插入图片描述
    在这里插入图片描述
  • 源码见课本p315
    在这里插入图片描述
    在这里插入图片描述
package Chapter03;

import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.SET;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

public class WhiteList {
    public static void main(String[] args) {
        SET<String> set = new SET<String>();//create empty set of strings

        //read in whitelist
        In in = new In(args[0]);
        while (!in.isEmpty()){
            set.add(in.readString());
        }

        //print words in list
        while (!StdIn.isEmpty()){
            String word = StdIn.readString();
            if (set.contains(word)){
                StdOut.println(word);
            }
        }
    }
}

2 dictionary clients 字典类用例

在这里插入图片描述

package Chapter03;

import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.ST;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

public class LookupCSV {
    public static void main(String[] args) {
        //process input file
        In in = new In(args[0]);
        int keyField = Integer.parseInt(args[1]);
        int valField = Integer.parseInt(args[2]);

        //build symbol table
        ST<String, String> st = new ST<String, String>();
        while (in.hasNextLine()){
            String line = in.readLine();
            String[] tokens = line.split(",");
            String key = tokens[keyField];
            String val = tokens[valField];
            st.put(key, val);
        }

        //process lookups with standard I/O
        while (!StdIn.isEmpty()){
            String s = StdIn.readString();//s是一个key,需要在terminal输入想要查询的key,回车,显示value
            if (!st.contains(s)){
                StdOut.println("Not found");
            }
            else{
                StdOut.println(st.get(s));//返回key和value
            }
        }
    }
}

3 indexing clients 索引类用例

在这里插入图片描述

package Chapter03;

import edu.princeton.cs.algs4.*;

import java.io.File;

//文件索引
public class FileIndex {
    public static void main(String[] args) {
        //symbol table
        ST<String, SET<File>> st = new ST<String, SET<File>>();

        //list of file names from command line
        for (String filename : args) {//增强for循环 for(声明新局部变量: 要访问/遍历的) --> 遍历所有输入的文件名
            File file = new File(filename);
            In in = new In(file);
            //for each word in file, add file to corresponding set
            while (!in.isEmpty()){
                String key = in.readString();//读取每个file的具体单词
                if (!st.contains(key)){
                    st.put(key, new SET<File>());//当key原来不被包含,建立一个新的file set
                }
                SET<File> set = st.get(key);
                set.add(file);
            }
        }

        //process queries
        while (!StdIn.isEmpty()){
            String query = StdIn.readString();
            StdOut.println(st.get(query));
        }
    }
}
  • 书籍中的查找功能
    在这里插入图片描述
  • concordance --> 用语索引;著作或作家全集的重要用字索引
package Chapter03;

import edu.princeton.cs.algs4.*;

public class Concordance {
    public static void main(String[] args) {
        //read text and build index
        In in = new In(args[0]);
        String[] words = StdIn.readAll().split("\\s+");//根据空白分割
        ST<String, SET<Integer>> st = new ST<String, SET<Integer>>();
        for (int i = 0; i < words.length; i++) {
            String s = words[i];
            if (!st.contains(s)){
                st.put(s, new SET<Integer>());
            }
            SET<Integer> set = st.get(s);
            set.add(i);
        }

        //process queries and print concordances
        while (!StdIn.isEmpty()){
            String query = StdIn.readString();
            SET<Integer> set = st.get(query);
            for (int k : set) {
                //print words[k-4] to words[k+4]
            }
        }
    }
}

4 sparse vectors 稀疏向量

在这里插入图片描述

  • 当matrix小,这样做可以,但N往往很大
    在这里插入图片描述
    在这里插入图片描述
package Chapter03;

import edu.princeton.cs.algs4.ST;

public class SparseVector {
    //HashST because order not important
    private ST<Integer, Double> v;
    //构造器 empty ST represents all 0s vector
    public SparseVector(){
        v = new ST<Integer, Double>();
    }

    //a[i] = value
    public void put(int i, double x){
        v.put(i, x);
    }

    //return a[i]
    public double get(int i){
        if (!v.contains(i)) return 0.0;
        else                return v.get(i);
    }

    public Iterable<Integer> indices(){//指数
        return v.keys();
    }

    //dot product is constant time for sparse vectors
    public double dot(double[] that){
        double sum = 0.0;
        for (int i : indices()){
            sum += that[i]*this.get(i);//this是v
        }
        return sum;
    }
}

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值