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的更多相关文章

  1. 算法Sedgewick第四版-第1章基础-001递归

    一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...

  2. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)

    一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...

  3. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)

    一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...

  4. 算法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 ...

  5. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版

    package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...

  6. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)

    一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...

  7. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)

    一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...

  8. 算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的

    1. package algorithms.stacks13; /******************************************************************* ...

  9. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法

    1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...

  10. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法

    1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /******* ...

随机推荐

  1. 使用zip()并行迭代

  2. list的内存分配机制分析

    该程序演示了list在内存分配时候的问题.里面的备注信息是我的想法. /* 功能说明: list的内存分配机制分析. 代码说明: list所管理的内存地址可以是不连续的.程序在不断的push_back ...

  3. burpsuite使用以及repeater模块实现重放攻击

    第一.burp suit是什么? Burp Suite 包含了一系列burp 工具,这些工具之间有大量接口可以互相通信,之所以这样设计的目的是为了促进和提高 整个攻击的效率.平台中所有工具共享同一ro ...

  4. identityservice4使用案例

    一 使用缘由 最近写微服务的blog,研读了o’reilly出的 <building Microservices With Asp.net Core>,其中使用的微服务分布式权限组件是mi ...

  5. curl获取图片

    <?php set_time_limit(0); //执行30秒超时后继续执行 header("Content-type:text/html;charset=utf-8"); ...

  6. ipad与iphone的屏幕分辨率

    1.ipad分辨率,iphone 6 iPhone设备      尺寸 分辨率                   点iPhone 3和3s  3.5英寸    (320×480)         3 ...

  7. bzoj 2744 [HEOI2012]朋友圈——补图!+匈牙利算法

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2744 求最大的团<==>补图(有边的变成没边.没边的变成有边)的最大独立集! A ...

  8. 锁存器 Latch v.s. 触发器 Flip-Flop

    转载  http://guqian110.github.io/pages/2014/09/23/latch_versus_flip_flop.html 根据 Wiki: Flip-flop (elec ...

  9. Installing Redis more properly

    Installing Redis more properly Running Redis from the command line is fine just to hack a bit with i ...

  10. C# Chat曲线图,在发布之后出现错误 Invalid temp directory in chart handler configuration c:\TempImageFiles\

    First error message: Invalid temp directory in chart handler configuration c:\TempImageFiles\ Soluti ...