write in front
Winter vacation idle come to nothing, whim, want a two questions to play, a long time no a over the problem, think of that finger violence hit the keyboard feeling is a burst of acid cool AH.
Do the first few questions are quite shunliu, about half an hour a question bar, a three or four, who knows to fifth card this.
A seemingly simple problem is stuck with me for two hours, but the result is pretty good.
Broke a record, unexpectedly accidentally became the problem of the optimal solution ....
Detailed description Click here to see the original question
The general is:
Give you two collections that require {A} + {B}.
Note: There will not be two identical elements in the same collection.
Bitterness Analysis Road
(1) A tool that comes with Java
Because it is playing Java itself, the first thing to think of is definitely a set set of Java comes with, request a set directly loaded two sets a, a, a, and then the element (automatically repeat), and then sort it, OK
Import Java.util.hashset;import java.util.scanner;/** * @author_Stone6762 */public class Main {/** * @Title_strArr2Str * @ Describe_ a string array into a string _ two strings interspersed with the specified characters * @Author_Stone6762 * @param arr string array * @param signs the characters you want to insert in the middle @return */public static string arr2string (object[] arr, string signs) {String aim = ""; for (int i = 0; i < arr.length; i++) {if (i = = 0) {aim + = arr[i].tostring (); continue;} Aim + = signs + arr[i];} return aim;} private static void Choosesort (integer[] arr) {for (int i = 0; i < arr.length-1; i++) {int min = i;for (int j = i + 1 ; J < Arr.length; J + +) {if (Arr[j] < arr[min]) {min = j;}} int temp = Arr[i];arr[i] = arr[min];arr[min] = temp;}} public static void Main (string[] args) {Scanner scan = new Scanner (system.in), while (Scan.hasnext ()) {int N=scan.nextint () ; int M=scan.nextint (); Hashset<integer>set=new hashset<integer> (); for (int i = 0; I <m+n; i++) {Set.add (Scan.nextint ());} Integer []arr=set.toarray (new integer[0]); Choosesort (arr); System. Out.println (Arr2string (arr, ""));}}}
The result is really "live up to expectations", timed out , hehe, Java comes with the tool really don't let use ah, then well, continue to analyze
(2) Stupid method
Collection does not allow the collection to be stored, that can only use the array, directly apply for a large array (capacity of a+b), first load the set a, and then assemble B,
each element of set B is loaded with a traversal of set a , if it is already there, do not, if it is not loaded in ,
Just a little trouble.
Import java.util.scanner;/** * @author_Stone6762 */public class Main {private static String intarr2string (int[] arr, int k {String aim = ""; for (int i = 0; I < K; i++) {if (i = = 0) {aim + = Arr[i];continue;} Aim + = "+ Arr[i];} return aim;} private static void Choosesort (int[] arr) {for (int i = 0; i < arr.length-1; i++) {int min = i;for (int j = i + 1; j < Arr.length; J + +) {if (Arr[j] < arr[min]) {min = j;}} int temp = Arr[i];arr[i] = arr[min];arr[min] = temp;}} public static void Main (string[] args) {Scanner scan = new Scanner (system.in), while (Scan.hasnext ()) {int n = scan.nextint (); int m = Scan.nextint (); int arr[] = new Int[m + n];for (int i = 0; i < n; i++) {Arr[i] = Scan.nextint ();} Boolean flag = True;int k = n;for (int i = n; i < arr.length; i++) {int t = scan.nextint (); flag = true;for (int j = 0; J < K; J + +) {if (arr[j] = = t) {flag = False;break;}} if (flag) {arr[k] = t;k++;}} for (int i = k; i < arr.length; i++) {arr[k] = Integer.max_value;} Choosesort (arr); System.out.println (Intarr2string (arr, k));}}}
The result is a timeout , OK, the double loop timeout should also, then come
(3) deformation to come again
Apply two arrays, each with elements from set A and set B, and then sort the collection A and set B, respectively
The current smallest element mina,minb is then selected from set A and set B to compare
① if they are equal, output any one, and then select the second smaller from the two collections to compare
② if mina>minb, then direct output MINB, Then choose a second small one from B to compare, anyway
③ know that an array is empty, Then output all elements of another array
this time it should be OK ...
Import java.util.scanner;/** * @author_Stone6762 */public class Main {private static void Choosesort (int[] arr) {for (int i = 0; i < arr.length-1; i++) {int min = i;for (int j = i + 1; j < Arr.length; J + +) {if (Arr[j] < arr[min]) {min = j;}} int temp = Arr[i];arr[i] = arr[min];arr[min] = temp;}} public static void Main (string[] args) {Scanner scan = new Scanner (system.in), while (Scan.hasnext ()) {int n = scan.nextint (); int m = Scan.nextint (); int arra[] = new Int[n];int arrb[] = new Int[m];for (int i = 0; i < n; i++) {Arra[i] = SCAN.N Extint ();} for (int i = 0; i < m; i++) {Arrb[i] = Scan.nextint ();} Choosesort (ArrA); Choosesort (ARRB); int i = 0, j = 0;while (I < n && J < m) {int a = Arra[i];int B = arrb[j];i F (A = = b) {System.out.print (b + ""); j++;i++;} else if (b > a) {System.out.print (A + ""); i++;} else {System.out.print (b + "); J + +;} if (i >= N) {while (J < m-1) {System.out.print (Arrb[j] + ""); J + +;} System.out.print (Arrb[j]); break;} if (J >= m){while (I < n-1) {System.out.print (Arra[i] + ""); i++;} System.out.print (Arra[i]); break;}} System.out.println ();}}}
The result is directly wrong , is also drunk, is also wasted, for Mao Ah ...
Break it up, remove all the code, and re-analyze it.
(4) Another way of thinking and rebuilding
To do this moment, slowly began to calm, not so play Ah, this has been used for nearly one hours ...
The results of the final analysis:
Direct hard come, apply a number to assemble the elements in both sets, and then nothing is sorted directly.
if the two elements are the same, then it will certainly be next to each other, at the time of the output , equal to only one output
Import Java.util.arrays;import java.util.scanner;/** * @authorStone6762 */public class Main {public static void main (string[] args) { //scanner scan = new Scanner (system.in); while (Scan.hasnext ()) { int n = scan.nextint (); int m = Scan.nextint (); int arr[] = new Int[n + m]; for (int i = 0; I <arr.length; i++) { Arr[i] = Scan.nextint (); } Arrays.sort (arr); System.out.print (Arr[0]); for (int i = 1; i < arr.length; i++) { // if (Arr[i]!=arr[i-1]) { System.out.print ("" +arr[i]); } } System.out.println ();}}}
The result is A, finally a, but spent 1.5 hours a, such a water problem always feel loss, it is a loss .... Anyway, it's already done, so optimize it .
(5) Revise again
Look carefully again.
Time can optimize the place is left only the input and output of the place, and the others do 1:30 will not be able to think of
In
space , you can
use less than one half of a variable .
finally
Import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.printwriter;import Java.io.streamtokenizer;import java.util.arrays;/** * @author_Stone6762 */public class Main {public static void main (S Tring[] args) throws IOException {//streamtokenizer in = new Streamtokenizer (New InputStreamReader ( system.in)); PrintWriter out = new PrintWriter (System.out); while (In.nexttoken ()! = streamtokenizer.tt_eof) {int n = (int) in.nval; In.nexttoken (); int m = (int) in.nval + N; int arr[] = new Int[m]; for (int i = 0; i < m; i++) {//In.nexttoken (); Arr[i] = (int) in.nval; }//arrays.sort (arr); Out.print (Arr[0]); Out.flush (); for (int i = 1; i < m; i++) {if (Arr[i]! = Arr[i-1]) {//Out.print ("" + arr[i]); Out.flush (); }} out.println (); Out.flush (); } }}
The
result did not think unexpectedly is a list first, really do not know whether to cry or should laugh ...
Hoj1412 two sets of and {A} + {B}--java to achieve the best solution of the bitterness path