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. LeetCode OJ:Construct Binary Tree from Inorder and Postorder Traversal(从中序以及后序遍历结果中构造二叉树)

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  2. List集合添加自定义对象

    public class Student { private String name; private int age; public Student() { super(); } public St ...

  3. windows文件名非法字符过滤检测-正则表达式

    过滤文件名非法字符 windows现在已知的文件名非法字符有 \ / : * ? " < > | var reg = new RegExp('[\\\\/:*?\"&l ...

  4. Jupyterhub Error 503: Proxy Target Missing

    Jupyterhub Error 503: Proxy Target Missing 请求太频繁

  5. java-08多态与异常处理

    1.运行以下代码: public class ParentChildTest { public static void main(String[] args) { Parent parent=new ...

  6. dockerfile http_php

    FROM centos6.6-php5.5:0.0.1 MAINTAINER syberos:wangmo RUN mv /etc/php.ini /etc/php.ini.bak COPY ./ph ...

  7. java代码随机数100个,10个一输出显示======

    总结:空格???懂否?如何显示 for(int i=0;i<100;i++){ if(i%10==0){ System.out.println(); } System.out.print(n[i ...

  8. c# 几种singleton 实现

    http://csharpindepth.com/Articles/General/Singleton.aspx#introduction 4th在线看 https://www.manning.com ...

  9. 【转】gem install libv8 错误

    转自:http://my.oschina.net/moks/blog/200344 [摘要]Because libv8 is the interface for the V8 engine used ...

  10. 【Java】java.util.Objects 源码学习

    2017-02-10 by 安静的下雪天  http://www.cnblogs.com/quiet-snowy-day/p/6387321.html    本篇概要 Objects 与 Object ...