Remember when the undergraduate algorithm in the class saw this topic, did not do it, today do 360 written test time unexpectedly still did not make out, the real egg hurts. Back to study for half a day, I rub, two hours to do so many topics, this certainly do not come out.
Topic:
The main idea is: given a string s: " blfbfsydleaklfbyM ", and a keyword T:" Lby ", find the smallest string containing T in s? Then we should find the minimum string: "lfby"
idea One: Traverse T, and then traverse S. (I see this, but the operation is more troublesome, I did not achieve)
idea two: Find out the position of the first character and the last character in S in the P1,P2, respectively, and the answer we ask is in the combination of P1 and P2. (extra waste of space)
According to reason, both of the ideas can be done. At present, I follow the idea of the second implementation of Java version:
<span style= "FONT-SIZE:18PX;" > </span><span style= "FONT-SIZE:14PX;" >public static void Main (String []args) {final string content= "Blfbfsydleaklfbym"; Final String key= "Lby"; System.out.println (getAbstract (content, key));} /* * content= "blfbfsydleaklfbym" * key= "Lby" * Find the smallest string containing key * Note that abstract is l***y, the beginning must be L, the end is Y */public static string Getabs Tract (string content,string key) {string tempstring= "";//* Find where L appears in content and where Y appears//* Then the requested summary may only appear in the position of L and Y in the combination of position arraylist<integer> keyfirstlist=new arraylist<integer> (); Arraylist<integer>keylastlist=new arraylist<integer> (); for (int i=0;i<content.length (); i++) {if ( Content.charat (i) ==key.charat (0)) Keyfirstlist.add (i); else if (Content.charat (i) ==key.charat (Key.length ()-1)) Keylastlist.add (i); else;//do noting}//get all possible position combinations//Note that many combinations are obviously inappropriate, the start position must be less than the position of the end int [][]maxabstact=new int[ Keyfirstlist.size ()][keylastlist.size ()];for (int i=0;i<keyfirstlist.size (); i++) for (int j=0;j<keylastlist.size (); j + +) {if (Keyfirstlist.get (i) >=keylastlist.get (j)) Maxabstact[i][j]=-1; else {maxabstact[i][j]=iscontainkey (content, key, Keyfirstlist.get (i), Keylastlist.get (j));}} int minlength=content.length (); int start = 0, end = 0;//to find the smallest value in the data, that is, the minimum length of the required string for (int i=0;i<keyfirstlist.size (); i++ ) for (int j=0;j<keylastlist.size (); j + +) {if (maxabstact[i][j]>-1&&maxabstact[i][j]<minlength) { Minlength=maxabstact[i][j];start=i;end=j;}} for (int i=keyfirstlist.get (start); I<=keylastlist.get (end); i++) {Tempstring+=content.charat (i);} return tempstring;} /* * The position from start to end contains key */public static int Iscontainkey (string content, string Key,int Start,int end) {int Newstart=sta rt;for (int i=1;i<key.length () -1;i++) {int j=newstart+1;for (; j<end;j++) {if (Content.charat (j) ==key.charat (i)) {newstart=j;break;}} if (j==end) return-1;} return End-start;} </span>
360 Written questions