/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Vector;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
// your code goes here
String document1
= "many google employees many google employees can program can program"; String[] searchTerms1
= {"google",
"program"}; System.
out.
println(answer
(document1, searchTerms1
));
String document2
= "a b c d a"; String[] searchTerms2
= {"a",
"c",
"d"}; System.
out.
println(answer
(document2, searchTerms2
));
String document3
= "world there hello hello where world"; String[] searchTerms3
= {"hello",
"world"}; System.
out.
println(answer
(document3, searchTerms3
));
String document4
= "a b a c c e a d c d a"; String[] searchTerms4
= {"a",
"c",
"d",
"a"}; System.
out.
println(answer
(document4, searchTerms4
));
}
static class snippet
{
public int startIndex;
public int endIndex;
public int containeTerms;
public Vector<String> stringList;
public snippet
(int start,
String wrd
) {
startIndex = start;
endIndex = start;
stringList = new Vector<String>();
stringList.add(wrd);
containeTerms = 1;
}
public Boolean isBiggerThan
(snippet other
) {
if(startIndex == -1)
{
return true;
}
int length = stringList.size();
int lengthOther = other.stringList.size();
if(length > lengthOther)
{
return true;
}
else if((length == lengthOther) && (startIndex > other.startIndex))
{
return true;
}
return false;
}
{
for(int i = 0; i < stringList.size(); i++)
{
if(i == 0)
{
result = stringList.get(i);
}
else
{
result += " ";
result += stringList.get(i);
}
}
return result;
}
}
{
// Your code goes here.
String[] docWords
= document.
split(" "); Vector<snippet> snippetList = new Vector<snippet>();
Vector<String> searchTermsList = new Vector<String>();
for(int j = 0; j < searchTerms.length - 1; j++)
{
if(searchTerms[j].compareTo(searchTerms[j + 1]) == 0)
{
searchTerms[j] = "";
}
}
for(int i = 0; i < searchTerms.length; i++)
{
if(searchTerms[i] != "")
{
searchTermsList.add(searchTerms[i]);
}
}
for(int i = 0; i < docWords.length; i++)
{
Boolean isSearchItem
= searchTermsList.
contains(w
);
if(isSearchItem == true)
{
for(int si = 0; si < snippetList.size(); si++)
{
snippet snip = snippetList.get(si);
if(snip.startIndex == -1 || snip.containeTerms == searchTermsList.size())
{
continue;
}
if(snip.stringList.contains(w) == true)
{
snip.startIndex = -1;
snip.containeTerms = 0;
snip.stringList.removeAllElements();
}
else
{
snip.containeTerms += 1;
if(snip.containeTerms == searchTermsList.size())
{
snip.stringList.add(w);
snip.endIndex = i;
}
}
}
}
for(int si = 0; si < snippetList.size(); si++)
{
snippet snip = snippetList.get(si);
if(snip.startIndex == -1 || snip.containeTerms == searchTermsList.size())
{
continue;
}
snip.stringList.add(w);
snip.endIndex = i;
}
if(isSearchItem == true)
{
snippetList.add(new snippet(i, w));
}
}
snippet minSnip = snippetList.get(0);
for(int i = 1; i < snippetList.size(); i++)
{
snippet snip = snippetList.get(i);
if(snip.containeTerms == searchTermsList.size())
{
if(minSnip == null)
{
minSnip = snip;
}
else if(minSnip.isBiggerThan(snip))
{
minSnip = snip;
}
//System.out.println(snip.toString());
//System.out.println(snip.stringList.size() + " " + snip.containeTerms);
}
}
return minSnip.toStr();
}
}