算法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; /******* ...
随机推荐
- Django 基础 web框架本质
Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. import socket sk ...
- @angular/cli项目构建--Dynamic.Form
导入所需模块: ReactiveFormsModule DynamicFormComponent.html <div [formGroup]="form"> <l ...
- PHP封装返回Ajax字符串和JSON数组
<?php class DBDA { public $host="localhost"; public $uid = "root"; public $pw ...
- 浅学soap--------1
无wsdl文件: Clint.php //客户端 <?php $soap = new SoapClient(null,array('uri'=>'server','location'=&g ...
- Redis底层探秘(六):对象多态及回收
本篇是我们redis系列的最后一篇,整个系列其实是我学习<redis设计与实现>的笔记,这本书感觉不错,推荐使用redis的小伙伴都可以看看. 整个系列的文字都比较干,很多数据结构和C语言 ...
- LeetCode Base 7
原题链接在这里:https://leetcode.com/problems/base-7/#/description 题目: Given an integer, return its base 7 s ...
- 【java规则引擎】模拟rete算法的网络节点以及匹配过程
转载请注明:http://www.cnblogs.com/shangxiaofei/p/6340655.html 本文只用于理解rete算法,通过一个规则的编译成的网络结构,以及匹配过程去理解rete ...
- centos安装教程
centos7和6.x有很大的差别,在第一次安装的时候遇到了很多坑,一共安装了三次才成功 如果是用window自带的Hyper-v装centos时 安装包在\\10.10.10.1\ShareDo ...
- 修改Linux安装软件镜像源为阿里云
CentOS系统更换软件安装源: 第一步:安装wget.如果你的系统已安装了wget可以直接跳到下一步. [root@local~]#yum install wget 第二步:备份你的原镜像文件,避免 ...
- ajax中error函数参数详解
xhr.status和error函数中的status是不一样的,error函数中的status主要包括:"success"."notmodified".&quo ...