《算法》第三章部分程序 part 6
▶ 书中第三章部分程序,加上自己补充的代码,包含双向索引表、文建索引、稀疏向量类型
● 双向索引表
package package01; import edu.princeton.cs.algs4.ST;
import edu.princeton.cs.algs4.Queue;
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class class01
{
private class01() {} public static void main(String[] args)
{
String filename = args[0];
String separator = args[1];
In in = new In(filename); ST<String, Queue<String>> st = new ST<String, Queue<String>>();
ST<String, Queue<String>> ts = new ST<String, Queue<String>>(); // 反向索引 for(;in.hasNextLine();)
{
String line = in.readLine();
String[] fields = line.split(separator);
String key = fields[0];
for (int i = 1; i < fields.length; i++) // 一个 key 对应多个值,分别存放,以后可以根据值反向查找键
{
String val = fields[i];
if (!st.contains(key))
st.put(key, new Queue<String>());
if (!ts.contains(val))
ts.put(val, new Queue<String>());
st.get(key).enqueue(val);
ts.get(val).enqueue(key);
}
}
StdOut.println("Done indexing");
for(;!StdIn.isEmpty();) // 交互式查找,可以正向也可以反向
{
String query = StdIn.readLine();
if (st.contains(query))
{
for (String vals : st.get(query))
StdOut.println(" " + vals);
}
if (ts.contains(query))
{
for (String keys : ts.get(query))
StdOut.println(" " + keys);
}
}
}
}
● 文件索引
package package01; import java.io.File;
import edu.princeton.cs.algs4.SET;
import edu.princeton.cs.algs4.ST;
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class class01
{
private class01() {} public static void main(String[] args)
{
ST<String, SET<File>> st = new ST<String, SET<File>>();
StdOut.println("Indexing files");
for (String filename : args)
{
StdOut.println(" " + filename);
File file = new File(filename);
for(In in = new In(file);!in.isEmpty();)
{
String word = in.readString();
if (!st.contains(word))
st.put(word, new SET<File>());
SET<File> set = st.get(word);
set.add(file);
}
}
for(;StdIn.isEmpty();) // 交互式查找包含特定单词的文件
{
String query = StdIn.readString();
if (st.contains(query))
{
SET<File> set = st.get(query);
for (File file : set)
StdOut.println(" " + file.getName());
}
}
}
}
● 稀疏向量类型
package package01; import edu.princeton.cs.algs4.ST;
import edu.princeton.cs.algs4.StdOut; public class class01
{
private int d; // 向量维数
private ST<Integer, Double> st; // 向量 index - value 对 public class01(int dim)
{
d = dim;
st = new ST<Integer, Double>();
} public void put(int i, double value)
{
if (i < 0 || i >= d)
throw new IllegalArgumentException("\n<put> i < 0 || i >= d.\n");
if (value == 0.0)
st.delete(i);
else
st.put(i, value);
} public double get(int i)
{
if (i < 0 || i >= d)
throw new IllegalArgumentException("\n<get> i < 0 || i >= d.\n");
return (st.contains(i)) ? st.get(i) : 0.0;
} public int nnz()
{
return st.size();
} @Deprecated
public int dimension() { return d; } public double dot(class01 that)
{
if (d != that.d)
throw new IllegalArgumentException("\n<dot> dimension disagree.\n");
double sum = 0.0;
if (st.size() <= that.st.size()) // 遍历元素较少的向量,去元素较多的向量中查找
{
for (int i : st.keys())
{
if (that.st.contains(i))
sum += get(i) * that.get(i);
}
}
else
{
for (int i : that.st.keys())
{
if (st.contains(i))
sum += get(i) * that.get(i);
}
}
return sum;
} public double dot(double[] that)
{
double sum = 0.0;
for (int i : st.keys())
sum += that[i] * get(i);
return sum;
} public double magnitude()
{
return Math.sqrt(dot(this));
} public class01 scale(double alpha)
{
class01 c = new class01(d);
for (int i : st.keys())
c.put(i, alpha * get(i));
return c;
} public class01 plus(class01 that)
{
if (d != that.d)
throw new IllegalArgumentException("\n<plus> dimension disagree.\n");
class01 c = new class01(d); // 新建一个向量存放结果
for (int i : st.keys())
c.put(i, get(i));
for (int i : that.st.keys())
c.put(i, that.get(i) + c.get(i));
return c;
} public String toString() // toString 接口
{
StringBuilder s = new StringBuilder();
for (int i : st.keys())
s.append("(" + i + ", " + st.get(i) + ") ");
return s.toString();
} public static void main(String[] args)
{
class01 a = new class01(10);
class01 b = new class01(10);
a.put(3, 0.50);
a.put(9, 0.75);
a.put(6, 0.11);
a.put(6, 0.00);
b.put(3, 0.60);
b.put(4, 0.90);
StdOut.println("a = " + a);
StdOut.println("b = " + b);
StdOut.println("a dot b = " + a.dot(b));
StdOut.println("a + b = " + a.plus(b));
}
}
《算法》第三章部分程序 part 6的更多相关文章
- 《算法》第三章部分程序 part 5
▶ 书中第三章部分程序,加上自己补充的代码,包含公共符号表.集合类型 ● 公共符号表,用于普通查找表的基本类 package package01; import java.util.NoSuchEle ...
- 《算法》第三章部分程序 part 4
▶ 书中第三章部分程序,加上自己补充的代码,包括散列表.线性探查表 ● 散列表 package package01; import edu.princeton.cs.algs4.Queue; impo ...
- 《算法》第三章部分程序 part 3
▶ 书中第三章部分程序,加上自己补充的代码,红黑树 ● 红黑树,大部分方法与注释与二叉树相同 package package01; import java.util.NoSuchElementExce ...
- 《算法》第三章部分程序 part 2
▶ 书中第三章部分程序,加上自己补充的代码,平衡二叉搜索树 ● 平衡二叉搜索树 package package01; import java.util.NoSuchElementException; ...
- 《算法》第三章部分程序 part 1
▶ 书中第三章部分程序,加上自己补充的代码,包括单词频率统计,(单链表)顺序查找表,二分查找表 ● 单词频率统计 package package01; import edu.princeton.cs. ...
- 《算法》第二章部分程序 part 3
▶ 书中第二章部分程序,加上自己补充的代码,包括各种优化的快排 package package01; import edu.princeton.cs.algs4.In; import edu.prin ...
- 《算法》第一章部分程序 part 1
▶ 书中第一章部分程序,加上自己补充的代码,包括若干种二分搜索,寻找图上连通分量数的两种算法 ● 代码,二分搜索 package package01; import java.util.Arrays; ...
- 《算法》第二章部分程序 part 5
▶ 书中第二章部分程序,加上自己补充的代码,包括利用优先队列进行多路归并和堆排序 ● 利用优先队列进行多路归并 package package01; import edu.princeton.cs.a ...
- 《算法》第二章部分程序 part 4
▶ 书中第二章部分程序,加上自己补充的代码,包括优先队列和索引优先队列 ● 优先队列 package package01; import java.util.Comparator; import ja ...
随机推荐
- Styles in Windows Phone
1. Use resources in internal style file: <Application.Resources> <ResourceDictionary Source ...
- VS2017 远程调试linux(centos).net core
第一步建立链接 Tools > Options > Cross Platform > Connection Manage 工具>选项>跨平台>连接管理器 第二步骤 ...
- SpringSecurity的Filter执行顺序在源码中的体现
在网上看各种SpringSecurity教程时,都讲到了SpringSecurity的Filter顺序.但是一直不知道这个顺序在源码中是如何体现的.今天一步一步的查找,最终找到顺序是在FilterCo ...
- mysql备份学习笔记及xtrabackup安装
(参考书籍:<深入浅出MySQL>) 一.备份恢复策略 a) 确定要备份的表的存储引擎是事务型还是非事务型 b) 确定使用全备份还是增量备份 c) 定期做备份 ...
- Oracle随机排序函数和行数字段
随机排序函数dbms_random.value()用法:select * from tablename order by dbms_random.value() 行数字段rownum用法:select ...
- 理解 neutron(15):Neutron Linux Bridge + VLAN/VXLAN 虚拟网络
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
- selector的小箭头去除
selector的小箭头去除 .not-arrow{ -webkit-appearance:none; -moz-appearance:none; appearance:none; /*去掉下拉箭头* ...
- git遇到的问题之“Please make sure you have the correct access rights and the repository exists.”
对于git的提交一直很小心翼翼,感觉一不小心就会踩到莫名的坑. 这不, 某天commit 就遇到了On branch master nothing to commit (working directo ...
- SQL-sqlHelper001
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- window.open 子窗口关闭刷新父页面
function JsMod(htmlurl,tmpWidth,tmpHeight){ htmlurl=getRandomUrl(htmlurl); var winObj = window.open( ...