[Huawei machine trial exercise] 13. Enter the train station, Huawei enter the train station

Source: Internet
Author: User

[Huawei machine trial exercise] 13. Enter the train station, Huawei enter the train station

Question

Description: a positive integer N is given to indicate the number of trains. The value is 0 <N <10. Next, enter the train arrival sequence. There are N trains in total. Each train is numbered 1-9. The serial number of the train exit must be output in Lexicographic Order. Question type: Stack difficulty: Advanced running time limit: 10Sec memory limit: 128MByte stage: pre-entry exercise input: multiple groups of test cases, enter a positive integer N (0 <N <10) in the first row of each group. The second row contains N positive integers ranging from 1 to 9. Output: the output train's outbound serial number in Lexicographic Order. Each serial number is separated by a space. Each output sequence has a line break. For details, see sample. Sample input: 31 2 3 sample output: 1 2 31 3 22 1 32 3 13 2 1

Ideas

1. If n = 1, it is an arrangement. 2. When n> 1, first obtain the order of n-1's outbound stack, and then separately insert n into n-1, N-1 and every number after N-1!

Code

/* ------------------------------------- * Date: 2015-06-30 * Author: SJF0115 * Title: Enter the train station * Source: huawei machine --------------------------------------- */# include <iostream> # include <string> # include <algorithm> # include <vector> using namespace std; void helper (string & inTrain, vector <string> & outTrain, int index) {if (index = inTrain. size () {return;} // if (index = 0) {string outNum (""); outNum + = inTrain [index]; outTrain. push_back (outNum);} // if else {vector <string> newOutTrain; // output stack sequence int size = outTrain. size (); // index the train to stack for (int I = 0; I <size; ++ I) {// the I-th output stack sequence int count = outTrain [I]. size (); // search for the previous train subscript int targetIndex; for (int j = 0; j <count; ++ j) {if (inTrain [index-1] = outTrain [I] [j]) {targetIndex = j; break ;} // if} // for string tmp (outTrain [I]); for (int j = targetIndex; j <= count; ++ j) {tmp. insert (tmp. begin () + j, inTrain [index]); newOutTrain. push_back (tmp); tmp. erase (tmp. begin () + j);} // for swap (outTrain, newOutTrain);} // else helper (inTrain, outTrain, index + 1 );} vector <string> TrainLeft (string inTrain) {vector <string> result; int size = inTrain. size (); if (size <= 0) {result;} // if helper (inTrain, result, 0); sort (result. begin (), result. end (); return result;} int main () {int n; // freopen ("C: \ Users \ Administrator \ Desktop \ c00000000.txt ", "r", stdin); while (cin> n) {string train (""); int num; for (int I = 1; I <= n; ++ I) {cin> num; train + = num + '0';} // for vector <string> trainNum = TrainLeft (train ); // output int size = trainNum. size (); for (int I = 0; I <size; ++ I) {int count = trainNum [I]. size (); for (int j = 0; j <count; ++ j) {if (j = 0) {cout <trainNum [I] [j];} // if else {cout <"" <trainNum [I] [j];} // else} // for cout <endl ;} // for} // while return 0 ;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.