Find shortest snippet
来源:互联网 发布:淘宝怎么拉黑店铺 编辑:程序博客网 时间:2024/06/06 10:40
参考:点击打开链接
Find shortest snippet,比如给一个Document是A,X,X,B,A,X,B,Query是A,B,要求返回shortestSnippet
第一问:如果Query有序(即A一定要在B前面),那么要返回A,X,BFollow up:如果Query无序(即B在A前面也可以),那么要返回B,A
再Follow up:如果Document非常大,如何再优化?
public static void main(String[] args) {String[] document = "A B C D E C B C A L I N".split(" "); ShortestSnippet sn = new ShortestSnippet(document); System.out.println(sn.query("A", "C")); System.out.println(sn.query2("A", "C"));}private HashMap<String, ArrayList<Integer>> hashmap;private String[] document;public ShortestSnippet(String[] document) {if (document == null || document.length == 0) {throw new IllegalArgumentException();}this.document = document;hashmap = new HashMap<String, ArrayList<Integer>>();for (int i = 0; i < document.length; i++) {String word = document[i];if (!hashmap.containsKey(word)) {hashmap.put(word, new ArrayList<Integer>());}hashmap.get(word).add(i);}}public String query(String wordA, String wordB) {if (wordA == null || wordB == null) {throw new IllegalArgumentException();}if (!hashmap.containsKey(wordA) || !hashmap.containsKey(wordB)) {return "not found";}ArrayList<Integer> listA = hashmap.get(wordA);ArrayList<Integer> listB = hashmap.get(wordB);int i = 0, j = 0, minA = 0, minB = 0, minDist = Integer.MAX_VALUE;//一定是要保证wordA在wordB的前面while (i < listA.size() && j < listB.size()) {if (listA.get(i) > listB.get(j)) {//wordA在wordB的后面,让wordB的指针后移j++;} else {//找到最小的距离if (minDist > listB.get(j) - listA.get(i)) {minDist = listB.get(j) - listA.get(i);minA = listA.get(i);minB = listB.get(j);}i++;}}StringBuilder sb = new StringBuilder();//输出最小距离中的所有单词for (int k = minA; k <= minB; k++) {sb.append(document[k]);}return sb.toString();}// follow uppublic String query2(String wordA, String wordB) {if (wordA == null || wordB == null) {throw new IllegalArgumentException();}if (!hashmap.containsKey(wordA) || !hashmap.containsKey(wordB)) {return "not found";}ArrayList<Integer> listA = hashmap.get(wordA);ArrayList<Integer> listB = hashmap.get(wordB);int i = 0, j = 0, minA = 0, minB = 0, minDist = Integer.MAX_VALUE;while (i < listA.size() && j < listB.size()) {int posA = listA.get(i), posB = listB.get(j);if (minDist > Math.abs(posA - posB)) {minDist = Math.abs(posA - posB);minA = posA;minB = posB;}if (posA < posB) {i++;} else {j++;}}StringBuilder sb = new StringBuilder();int start = Math.min(minA, minB), end = Math.max(minA, minB);for (int k = start; k <= end; k++) {sb.append(document[k]);}return sb.toString();}
0 0
- Find shortest snippet
- snippet
- Snippet
- A* search ,find the shortest path
- hdu1841 Find the Shortest Common Superstring-----KMP
- hdu1595-find the longest of the shortest
- hdu1841 Find the Shortest Common Superstring
- hdu 1841 Find the Shortest Common Superstring
- HDU1595---find the longest of the shortest
- hdu1595 find the longest of the shortest
- HDU1595 find the longest of the shortest
- Find The Shortest Path In Triangle Numbers
- HDU 1841: Find the Shortest Common Superstring
- HDU1595 find the longest of the shortest
- find the longest of the shortest
- HDU 1841Find the Shortest Common Superstring
- HDU 1841Find the Shortest Common Superstring
- find the longest of the shortest HDU
- 基于 Mesos、Marathon、Docker环境的 Jenkins 持续集成及配置管理
- 通道——旧照片——鼓浪屿旧巷
- groovy : 读 java bean 输出属性
- 【内推网】错失1800万融资之后,他用900元挽救了公司
- HDU3271 SNIBB(数位dp+二分)
- Find shortest snippet
- (LA3026) Period -- KMP again (KMP 模板)
- 开始使用SoapUI
- google-api-url
- Label--关于Label富文本
- 一道学java (二)类和对象
- 万恶的金山笔试题
- asp.net web应用程序问题
- Lodash中10个可被ES6替代的特性