HDU 4550 card game (Greedy)

Source: Internet
Author: User
Card games

Time Limit: 3000/1000 MS (Java/others) memory limit: 65535/32768 K (Java/Others)
Total submission (s): 376 accepted submission (s): 115

Problem description James was bored at home recently, so he invented an interesting game with N stacked cards with numbers on each card, the number range is 0 ~ 9. The game rules are as follows:
First, take the top card and put it on the table. Then, take the top card each time and put it to the rightmost or leftmost of the existing card sequence on the table. When N cards are all placed on the table, N cards on the table constitute a number. This number cannot have a leading 0, that is, the number on the leftmost card cannot be 0. The goal of the game is to minimize this number.
Now your task is to help James write segments Program To find the minimum number.

 

The first line of input is a number t, indicating that there are T groups of test data;
Then there are t rows below, each row contains only 0 ~ The string of 9 indicates n cards stacked together, And the leftmost number indicates the top card.

[Technical Specification]
T <= 1000
1 <= n <= 100

 

Output: for each group of test data, output the minimum number in one row.

 

Sample input3
565
9876543210
9876105432

 

Sample output556
1234567890
1678905432

 

Source2013 Jinshan xishanju creative game program challenge-Preliminary Round (2)

 

Recommendliuyiding adopts greedy methods. Find the smallest value on the right. The minimum value found for the first time cannot be 0. Use sta1 and sta2 to store the first and last characters respectively.
# Include <stdio. h> # Include <Algorithm> # Include < String . H> # Include <Iostream> Using  Namespace  STD;  Const   Int Maxn = 110  ;  Char  STR [maxn];  Char  Sta1 [maxn], sta2 [maxn];  Int  Main (){  Int  T; scanf (  "  % D  " ,& T );  While (T -- ) {Scanf (  "  % S  " ,& Str );  Int N = Strlen (STR );  Int T1 = 0 , T2 = 0  ;  Int T = n- 1 ;  While (T> 0 & STR [T] = '  0  ' ) T -- ;  For ( Int I = T; I> = 0 ; I -- )  If (STR [I]! = '  0  ' & STR [I] < STR [T]) T = I; sta1 [T1 ++] = STR [T];  For ( Int I = N- 1 ; I> T; I --) sta2 [T2 ++] = STR [I];  While (T1 + t2 <n & T> 0  ){  Int TMP = T- 1  ; For ( Int J = TMP- 1 ; J> = 0 ; J -- )  If (STR [J] < STR [TMP]) TMP = J; sta1 [T1 ++] = STR [TMP];  For ( Int I = T- 1 ; I> TMP; I --) sta2 [T2 ++] =STR [I]; t = TMP ;}  For ( Int I = 0 ; I <t1; I ++) printf ( "  % C  "  , Sta1 [I]);  For ( Int I = t2- 1 ; I> = 0 ; I --) printf ( " % C  "  , Sta2 [I]); printf (  "  \ N  "  );}} 

 

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.