//*************************************************************************//Topic Requirements: 4, remove the maximum of two strings in the same substring. //************************************************************************* Public classsearchmaxsamestring { Public Static voidMain (string[] args) {String str1=NULL; String str2=NULL; String Maxstr=NULL; //Enter two stringsScanner input =NewScanner (system.in); System.out.print ("Please enter string str1:"); STR1=Input.nextline (); System.out.print ("Please enter string str2:"); STR2=Input.nextline (); //separates the two strings entered into aString shortstr = Str1.length () > Str2.length ()?str2:str1; String Longstr= Shortstr.equals (str1)?str2:str1; //call Maxserach to get the maximum stringMaxstr =maxstring (Shortstr, LONGSTR); //Output StringSYSTEM.OUT.PRINTLN ("Maximum same substring is:" +maxstr); Input.close (); } //define a static method Maxserach to get the maximum string Public Staticstring maxstring (String shortstr, String longstr) {//defines an empty string to store the maximum stringString maxstr = ""; //The maximum same string is obtained by the short string in two strings for(inti = 0; I < shortstr.length (); i++) { //from the go, the truncated substring is held in the temporary string Temp1String Temp1 =shortstr.substring (i); for(intj = Temp1.length ()-1; J >= 0; j--) { //The obtained temporary substring Temp1 is then intercepted from the back, to get all substrings stored in the Temp2String temp2 = temp1.substring (0, J); //If there is a substring, and the substring has the largest length, it is paid to Maxstr if(Longstr.indexof (TEMP2)! =-1 && temp2.length () >maxstr.length ()) {Maxstr=Temp2; } } } //back to Maxstr returnMaxstr; }}// *************************************************************************//Test Results://Please enter a string STR1:FFDSAJOIFWWQN3342NKJNF23N42FSD//Please enter a string STR2:FDAFIOENRJOIFWWQN3342NKJNF23N4023JFS,ADFJ//The maximum same substring is: Joifwwqn3342nkjnf23n4// *************************************************************************
The code that asks for the most identical string has forgotten to refer to the man's, and I can't!
Remove the maximum of the same substring from two strings