fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.util.Vector;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. // your code goes here
  15. String document1 = "many google employees many google employees can program can program";
  16. String[] searchTerms1 = {"google", "program"};
  17. System.out.println(answer(document1, searchTerms1));
  18.  
  19. String document2 = "a b c d a";
  20. String[] searchTerms2 = {"a", "c", "d"};
  21. System.out.println(answer(document2, searchTerms2));
  22.  
  23. String document3 = "world there hello hello where world";
  24. String[] searchTerms3 = {"hello", "world"};
  25. System.out.println(answer(document3, searchTerms3));
  26.  
  27. String document4 = "a b a c c e a d c d a";
  28. String[] searchTerms4 = {"a", "c", "d", "a"};
  29. System.out.println(answer(document4, searchTerms4));
  30.  
  31. }
  32.  
  33. static class snippet
  34. {
  35. public int startIndex;
  36. public int endIndex;
  37. public int containeTerms;
  38. public Vector<String> stringList;
  39.  
  40. public snippet(int start, String wrd)
  41. {
  42. startIndex = start;
  43. endIndex = start;
  44. stringList = new Vector<String>();
  45. stringList.add(wrd);
  46. containeTerms = 1;
  47. }
  48.  
  49. public Boolean isBiggerThan(snippet other)
  50. {
  51. if(startIndex == -1)
  52. {
  53. return true;
  54. }
  55.  
  56. int length = stringList.size();
  57. int lengthOther = other.stringList.size();
  58.  
  59. if(length > lengthOther)
  60. {
  61. return true;
  62. }
  63. else if((length == lengthOther) && (startIndex > other.startIndex))
  64. {
  65. return true;
  66. }
  67.  
  68. return false;
  69. }
  70.  
  71. public String toStr()
  72. {
  73. String result = "";
  74.  
  75. for(int i = 0; i < stringList.size(); i++)
  76. {
  77. if(i == 0)
  78. {
  79. result = stringList.get(i);
  80. }
  81. else
  82. {
  83. result += " ";
  84. result += stringList.get(i);
  85. }
  86. }
  87.  
  88. return result;
  89. }
  90. }
  91.  
  92. public static String answer(String document, String[] searchTerms)
  93. {
  94. // Your code goes here.
  95. String[] docWords = document.split(" ");
  96. Vector<snippet> snippetList = new Vector<snippet>();
  97. Vector<String> searchTermsList = new Vector<String>();
  98.  
  99. Arrays.sort(searchTerms);
  100.  
  101. for(int j = 0; j < searchTerms.length - 1; j++)
  102. {
  103. if(searchTerms[j].compareTo(searchTerms[j + 1]) == 0)
  104. {
  105. searchTerms[j] = "";
  106. }
  107. }
  108.  
  109. for(int i = 0; i < searchTerms.length; i++)
  110. {
  111. if(searchTerms[i] != "")
  112. {
  113. searchTermsList.add(searchTerms[i]);
  114. }
  115. }
  116.  
  117. for(int i = 0; i < docWords.length; i++)
  118. {
  119. String w = docWords[i];
  120. Boolean isSearchItem = searchTermsList.contains(w);
  121.  
  122. if(isSearchItem == true)
  123. {
  124. for(int si = 0; si < snippetList.size(); si++)
  125. {
  126. snippet snip = snippetList.get(si);
  127.  
  128. if(snip.startIndex == -1 || snip.containeTerms == searchTermsList.size())
  129. {
  130. continue;
  131. }
  132.  
  133. if(snip.stringList.contains(w) == true)
  134. {
  135. snip.startIndex = -1;
  136. snip.containeTerms = 0;
  137. snip.stringList.removeAllElements();
  138. }
  139. else
  140. {
  141. snip.containeTerms += 1;
  142. if(snip.containeTerms == searchTermsList.size())
  143. {
  144. snip.stringList.add(w);
  145. snip.endIndex = i;
  146. }
  147. }
  148. }
  149. }
  150.  
  151. for(int si = 0; si < snippetList.size(); si++)
  152. {
  153. snippet snip = snippetList.get(si);
  154.  
  155. if(snip.startIndex == -1 || snip.containeTerms == searchTermsList.size())
  156. {
  157. continue;
  158. }
  159.  
  160. snip.stringList.add(w);
  161. snip.endIndex = i;
  162. }
  163.  
  164. if(isSearchItem == true)
  165. {
  166. snippetList.add(new snippet(i, w));
  167. }
  168. }
  169.  
  170. snippet minSnip = snippetList.get(0);
  171.  
  172. for(int i = 1; i < snippetList.size(); i++)
  173. {
  174. snippet snip = snippetList.get(i);
  175.  
  176. if(snip.containeTerms == searchTermsList.size())
  177. {
  178. if(minSnip == null)
  179. {
  180. minSnip = snip;
  181. }
  182. else if(minSnip.isBiggerThan(snip))
  183. {
  184. minSnip = snip;
  185. }
  186. //System.out.println(snip.toString());
  187. //System.out.println(snip.stringList.size() + " " + snip.containeTerms);
  188. }
  189. }
  190.  
  191. return minSnip.toStr();
  192. }
  193. }
Success #stdin #stdout 0.07s 381184KB
stdin
Standard input is empty
stdout
google employees can program
c d a
world there hello
a d c