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;}