HDU-4550 - Greedy

card game

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2782    Accepted Submission(s): 855


Problem Description
  Xiao Ming was bored at home recently, so he invented an interesting game. The game props are N cards stacked together. Each card has a number. The number range is 0~9. The rules of the game are as follows:
  First Take the top card and place it on the table, and then take the top card each time and place it on the far right or far left of the existing card sequence on the table. When all N cards are placed on the table, the N cards on the table form a number. This number cannot have a leading 0, which means that the number on the leftmost card cannot be a 0. The goal of the game is to minimize this number.
  Now your task is to help Xiaoming write a program to find this minimum number.
 

 

Input
The first line is a number T, indicating that there are T groups of test data;
then there are T lines below, each line is a string containing only 0~9, indicating N stacked cards, the leftmost number indicates the top card.

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

 

Output
For each set of test data, print the smallest number you can get on one line.
 

 

Sample Input
3 565 9876543210 9876105432
 

 

Sample Output
556 1234567890 1678905432
 

 

Source
 

      At first, I thought it was simple. There may be data such as "00001" that was not considered. To make this number the smallest, the highest bit should be the smallest. Find the smallest highest bit and the position minn that cannot be 0. For the elements after minn, directly Just connect it at the end in order. For the elements in front of minn as small as possible, you don't need to consider leading zeros because the highest bit has been fixed. Another problem is that if there are multiple smallest and highest bits, it is better to choose the one that is as late as possible, so that the decimals are as close as possible to the front.

   

 1 #include<iostream>
 2 #include<cstring>
 3 #include<queue>
 4 #include<cstdio>
 5 #include<stack>
 6 #include<set>
 7 #include<map>
 8 #include<cmath>
 9 #include<ctime>
10 #include<time.h> 
11 #include<algorithm>
12 using namespace std;
13 #define mp make_pair
14 #define pb push_back
15 #define debug puts("debug")
16 #define LL long long 
17 #define pii pair<int,int>
18 #define eps 1e-10
19 double inf=1e20;
20 int main()
21 {
22     int t,n,m,i,j,k;
23     char s[110];
24     cin>>t;
25     while(t--){
26         string ans="";
27         scanf("%s",s);
28         n=strlen(s);
29         i=n-1;
30         while(s[i]=='0') i--;
31         int minn=i;
32         for(;i>=0;--i) if(s[i]<s[minn]&&s[i]!='0') minn=i; 
33         for(i=0;i<minn;++i){
34             if(ans==""){
35                 ans+=s[i];
36                 continue;
37             }
38             if(s[i]>ans[0]){
39                 ans+=s[i];
40             }
41             else{
42                 string tmp="";
43                 tmp+=s[i];
44                 tmp+=ans;
45                 ans=tmp;
46             }
47         }
48         cout<<s[minn]<<ans<<s+minn+1<<endl;
49     }
50     return 0; 
51 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325286770&siteId=291194637