贪心,假如任意给出一个序列,如果两两交换了以后会变大,那么就交换,直到不能交换为止。
#include<bits/stdc++.h> using namespace std; const int maxn = 51; string s[maxn]; int rk[maxn]; bool cmp(int x,int y) { int i = 0, j = 0, n1 = s[x].size(), n2 = s[y].size(); string *s1 = &s[x], *s2 = &s[y]; int ct = n1+n2; while(ct--){ if(i == n1) i = 0,s1 = &s[y]; if(j == n2) j = 0,s2 = &s[x]; if(s1->at(i) != s2->at(j)) return s1->at(i) < s2->at(j); i++;j++; } return false; } int main() { //freopen("in.txt","r",stdin); ios_base::sync_with_stdio(false); int n; while(cin>>n&&n){ for(int i = 0; i < n; i++) cin>>s[i], rk[i] = i; sort(rk,rk+n,cmp); for(int i = n-1; i>=0; i--) printf("%s",s[rk[i]].c_str()); puts(""); } return 0; }