"Huawei OJ" "072-Train Pit Stop"

Source: Internet
Author: User

"Huawei OJ" "Algorithm Total chapter" "Huawei OJ" "072-Train station" "Project Download" topic description
给定一个正整数N代表火车数量,0<N<10,接下来输入火车入站的序列,一共N辆火车,每辆火车以数字1-9编号。要求以字典序排序输出火车出站的序列号思路:    此处所谓字典序排序的意思是这n辆火车有多少种出站的可能顺序(也就是数据结构中的栈有多少种出栈顺序)。思路为用三个变量分别存储待进站火车,站中火车和已出站火车,其中待进站火车用Queue(队列)存储和站中火车采用stack(栈)存储,已出站火车采用StringBuilder来存储,具体实现是采用递归的方法,递归函数的参数为当前待进站火车、站中火车、已出站火车的值所组成的三元组,递归结束条件是,未进站火车和站中火车均为空,此时输出已出站火车即为所有出站的一种可能,递推关系为对于当前情况有让下一辆火车进站或让站中的一辆火车出站两种可能,对于两种可能分别调用递归函数,即可得出问题的解。
Enter a description
有多组测试用例,每一组第一行输入一个正整数N(0<N<10),第二行包括N个正整数,范围为1到9。
Output description
输出以字典序排序的火车出站序列号,每个编号以空格隔开,每个输出序列换行,具体见sample。
Input example
31 2 3
Output example
1 2 31 3 22 1 32 3 13 2 1
Algorithm implementation
Importjava.util.*;/** * Author: Wang Junshu * date:2016-01-04 16:41 * All rights Reserved!!! */ Public  class Main {     Public Static void Main(string[] args) {//Scanner Scanner = new Scanner (system.in);Scanner Scanner =NewScanner (Main.class.getClassLoader (). getResourceAsStream ("Data.txt")); while(Scanner.hasnext ()) {intn = scanner.nextint (); string[] ss =NewString[n]; for(inti =0; I < n;            i++) {Ss[i] = Scanner.next ();        } System.out.println (Trainout (ss));    } scanner.close (); }/** * Here the so-called dictionary ordering means how many of these n trains have the possible order of outbound (that is, how many stacks in the data structure are in the stack order).  * The idea is to use three variables to store the incoming train, the station train and the outbound train, where the train with queue (queue) storage and station * train with stack (stack) storage, the outbound train uses StringBuilder to store, the implementation is recursive method, recursive function * The current train, station train, the value of the outbound train is composed of ternary group, the recursive end condition is that the train is not in and the station in the fire * vehicles are empty, at this time the output has been outbound train is a possibility of all outbound, the recurrence relationship for the current situation there is to let the next train to stop or let the station * in a train outbound two     Possible, the solution to the problem can be obtained for two possible calls to recursive functions, respectively. * * @param SS * @return  */    Private StaticStringTrainout(string[] ss) {//Arrays.sort (ss);list<list<string>> result =NewArraylist<> (); List<string> out =NewArraylist<> (ss.length); List<string> unout =NewArraylist<> (ss.length); Trainout (0, SS, out, unout, result); Collections.sort (Result,NewComparator<list<string>> () {@Override             Public int Compare(List<string> A, list<string> b) {intmin = A.size () < B.size ()? A.size (): B.size (); for(inti =0; i < min;                    i++) {String as = A.get (i); String bs = B.get (i);if(As.compareto (BS)! =0) {returnAs.compareto (BS); }                }returnA.size ()-b.size ();        }        }); StringBuilder Builder =NewStringBuilder ( the); for(list<string> List:result) {StringBuilder b =NewStringBuilder ( -); for(String s:list) {B.append (s). Append ("'); } B.setcharat (B.length ()-1,' \ n ');        Builder.append (b); }returnBuilder.tostring (); }/** * Train station * * @param I train number * @param SS All trains * @param out Train already outbound sequence * @param unout train not yet outbound sequence * @param result saves all possible results */    Private Static void Trainout(intI, string[] SS, list<string> out, list<string> unout, list<list<string>> result) {//All trains have been in the pit        if(I >= ss.length) {List<string> List =NewArraylist<> (); for(String s:out)            {List.add (s); }//Advanced back out             for(intj = unout.size ()-1; J >=0;            j--) {List.add (Unout.get (j)); } result.add (list);return; }//The first car came in and went out.Out.add (Ss[i]); Trainout (i +1, SS, out, unout, result);//RestoreOut.remove (Out.size ()-1);//No. I car came in without going outUnout.add (Ss[i]); Trainout (i +1, SS, out, unout, result);//RestoreUnout.remove (Unout.size ()-1); }}

"Huawei OJ" "072-Train Pit Stop"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.