Nyoj 905 card game

Source: Internet
Author: User
Card Game time limit: 1000 MS | memory limit: 65535 kb difficulty: 1
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
AC code:
#include<stdio.h>int main(){char str[105],ch[110],s;int T,i,j,left,right;scanf("%d",&T);while(T--){scanf("%s",str);s='9';for(i=0;str[i]!='\0';i++){if((str[i]!='0')&&str[i]<=s){s=str[i];j=i;}}left=53;right=52;ch[left]='9';for(i=0;str[i]!='\0';i++){if(i>=j)break;if(str[i]<=ch[left]){left--;ch[left]=str[i];}else{right++;ch[right]=str[i];}}printf("%c",s);for(i=left;i<=right;i++)printf("%c",ch[i]);for(i=j+1;str[i]!='\0';i++)printf("%c",str[i]);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.