HDU-4550 card games

Source: Internet
Author: User

Description

James was bored at home recently, so he invented an interesting game. Game props are n cards stacked together, each card has a number ranging from 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.

Input

The first row 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

Idea: greedy start from the back to the back, put the big back, and then find the current smallest, from the back can also eliminate the possibility of putting 0 to the first

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 200;char str[MAXN];char head[MAXN],tail[MAXN];int main() {int t;scanf("%d", &t);while (t--) {scanf("%s", str);int n = strlen(str);int a = 0, b = 0;int cnt = n-1;while (str[cnt] == '0' && cnt > 0)cnt--;for (int i = cnt; i >= 0; i--) if (str[cnt] > str[i] && str[i] != '0')cnt = i;head[a++] = str[cnt];for (int i = n-1; i > cnt; i--)tail[b++] = str[i];while (a+b < n && cnt > 0) {int tmp = cnt-1;for (int j = tmp-1; j >= 0; j--)if (str[j] < str[tmp])tmp = j;head[a++] = str[tmp];for (int j = cnt-1; j > tmp; j--)tail[b++] = str[j];cnt = tmp;}for (int i = 0; i < a; i++)printf("%c", head[i]);for (int j = b-1; j >= 0; j--)printf("%c", tail[j]);printf("\n");}return 0;}



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.