算法Sedgewick第四版-第1章基础-023-MultiwordSearch.java
Multi-word search. Program MultiwordSearch.java reads a sequence of query words q[1], ..., q[k] from the command line and a sequence of documents words d[1], ..., d[N] from standard input and finds the shortest interval in which the k words appear in the same order. (Here shortest means the number of words in the interval.) That is find indices i and j such that d[i1] = q[1], d[i2] = q[2], ..., d[ik] = q[k] and i1 < i2 < ... < ik.
Answer: for each query word, create a sorted list of the indices where it appears in the document. Scan through lists 2 to k in that order, deleting indices at the front of each list until the the first elements of the resulting k lists are in ascending order.
The sequence of first elements on the lists forms the shortest interval containing the first element on list 1.
Now delete the first element on list 1. Repeatedly delete elements from list 2 until it agrees with list 1. Repeat for list 3, and so on until the whole array is in ascending order. Check this sequence of first elements, etc.
/******************************************************************************
* Compilation: javac MultiwordSearch.java
* Execution: java MultiwordSearch query1 query2 ... < input.txt
* Dependencies: Queue.java StdIn.java
*
* Find the shortest interval (number of words) in the input file
* that contains the query words in the order specified on the command line.
*
******************************************************************************/ public class MultiwordSearch {
public static void main(String[] args) {
String[] words = StdIn.readAllStrings(); // construct queues[j] = sequence of positions of jth query word
Queue<Integer>[] queues = (Queue<Integer>[]) new Queue[args.length];
for (int j = 0; j < args.length; j++) {
queues[j] = new Queue<Integer>();
}
for (int i = 0; i < words.length; i++) {
for (int j = 0; j < args.length; j++) {
if (words[i].equals(args[j])) queues[j].enqueue(i);
}
} // repeatedly find smallest interval starting at position of queues[0]
boolean done = false;
int bestlo = -1, besthi = words.length;
while (!queues[0].isEmpty()) {
int lo = queues[0].dequeue();
int hi = lo;
for (int j = 1; j < args.length; j++) {
while (!queues[j].isEmpty() && queues[j].peek() <= hi) {
queues[j].dequeue();
}
if (queues[j].isEmpty()) {
done = true;
break;
}
else hi = queues[j].peek();
}
if (!done && hi - lo < besthi - bestlo) {
besthi = hi;
bestlo = lo;
} } if (bestlo >= 0) {
for (int i = bestlo; i <= besthi; i++)
StdOut.print(words[i] + " ");
StdOut.println();
}
else
StdOut.println("NOT FOUND");
}
}
算法Sedgewick第四版-第1章基础-023-MultiwordSearch.java的更多相关文章
- 算法Sedgewick第四版-第1章基础-001递归
一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)
一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)
一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)
一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursivel ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版
package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)
一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)
一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...
- 算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的
1. package algorithms.stacks13; /******************************************************************* ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法
1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法
1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /******* ...
随机推荐
- Git_学习_01_ git 安装与配置
参考:windows下Git BASH安装 二.参考资料 1. windows下Git BASH安装
- 20165210 学习基础和C语言基础调查
20165210 学习基础和C语言基础调查 一.技能学习过程和心得 读了娄老师<做中学>自己还是深有感受的,对于运动.音乐.棋牌都会一点,我觉得做中学可以概括为三点:做,学,学做结合,所谓 ...
- nginx负载均衡的简单实现
负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦. 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可 ...
- PHP数据结构之实现栈
接着前面PHP数据结构来学习,今天写的是实现栈. <?php class stack //定义一个栈的类 { private $size; //栈的空间大小 private $top; // 栈 ...
- net Core 2.1新功能Generic Host(通用主机)
net Core 2.1新功能Generic Host(通用主机) http://doc.okbase.net/CoderAyu/archive/301859.html 什么是Generic Host ...
- [BZOJ1797][AHOI2009]最小割Mincut
bzoj luogu sol 一条边出现在最小割集中的必要条件和充分条件. 先跑出任意一个最小割,然后在残余网络上跑出\(scc\). 一条边\((u,v)\)在最小割集中的必要条件:\(bel[u] ...
- phoneGap入门教程
地址: http://mobile.51cto.com/hot-273792.htm
- svn的ignor也是要提交的
刚才一直奇怪为什么svn管理某个路径下总是报要提交,但是进入同步模式,看不到任何内容,就是告诉该文件夹要提交:后来才发现原来是我添加了一个该文件夹下的文件为svn:ignor,所以要提交以下.
- Nexus安装以及2,3比较
解压缩之后, 进入nexus-3.6.2-01/bin文件夹中,执行: ./nexus start 如果使用root将会得到一个告警:Detected execution as "root& ...
- docker registry 镜像删除
registry:2.5.0版本的镜像,将镜像默认存放在了/var/lib/registry 目录下 /var/lib/registry/docker/registry/v2/repositories ...