http://acm.hdu.edu.cn/showproblem.php?pid=4550
贪心
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <string> 5 #include <iostream> 6 #include <algorithm> 7 #define maxn 200 8 using namespace std; 9 10 char str[200],s2[220]; 11 int t; 12 string s1; 13 14 int main() 15 { 16 scanf("%d",&t); 17 while(t--) 18 { 19 scanf("%s",str); 20 s1=str[0]; 21 int j=0; 22 char ch='9'; 23 for(int i=0; i<(int)strlen(str); i++) 24 { 25 if(str[i]!='0'&&str[i]<=ch) 26 { 27 j=i; 28 ch=str[i]; 29 } 30 } 31 for(int i=1; i<(int)strlen(str); i++) 32 { 33 if(i==j) s1=str[i]+s1; 34 else if(i>j) 35 { 36 s1+=str[i]; 37 } 38 else if(i<j) 39 { 40 if(str[i]<=s1[0]) 41 { 42 s1=str[i]+s1; 43 } 44 else 45 { 46 s1+=str[i]; 47 } 48 } 49 } 50 cout<<s1<<endl; 51 } 52 return 0; 53 }