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): 841 accepted submission (s): 263


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 a segment program and 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 Input

356598765432109876105432
 


Sample output

55612345678901678905432
Question: Give you an orderly 0 ~ The number of 9 is placed on the table at a time. The number of each pendulum is either at the leftmost or rightmost of the number of rows. Ask you to make up the smallest number. Of course, 0 cannot be used as the pilot. I don't know how to be greedy at first. Then it is retrieved in sequence by bit, but when it comes to 0, it is not sure whether to put it in front. It is possible that there is 1 or something in the back. The idea is confusing, and then we directly go to WA. Later, I wanted to take 0 ~ The number of 9 is directly counted and output. When preparing for writing, we find that the question is not correct and can only be left and rightmost.
Solution: the main idea is greed. We need to find the smallest value that is not 0 but the most backward value. We can only treat it as the first value in the final result, so that it is the smallest. You can savor it carefully. Then, you can start to judge the index, and the index is output directly. For details, refer to the code.
Question address: card games
AC code:
# Include <iostream> # include <cstring> # include <string> # include <cstdio> using namespace STD; char a [102]; char TMP [2]; string res; int main () {int TES, I, Len, index; scanf ("% d", & TES); While (TES --) {char P = '9 '; // initialize scanf ("% s", a); Res = ""; // initialize Len = strlen (a); for (I = 0; I <Len; I ++) // know the index {if (a [I]! = '0' & A [I] <= P) {P = A [I]; Index = I ;}} TMP [0] = A [0], TMP [1] = '\ 0'; Res = res + TMP; for (I = 1; I <index; I ++) // check whether the preceding code is bigger than the first one {TMP [0] = A [I], TMP [1] = '\ 0 '; if (A [I] <= res [0]) RES = TMP + Res; else res = res + TMP;} If (index> 0) {TMP [0] = A [Index], TMP [1] = '\ 0'; // index res = TMP + Res;} for (I = index + 1; I <Len; I ++) // The following can only be inserted at the end of {TMP [0] = A [I], TMP [1] = '\ 0 '; res = res + TMP;} cout <res <Endl;} return 0 ;}

These questions are all three questions in the 2013 Jinshan xishanju creative game program challenge-Preliminary Round (2). I didn't expect shiyuankongbu to do it too.


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.