您的位置:首页 > 其它

UVA 10905 Children's Game (贪心)

2015-09-15 13:01 344 查看
贪心,假如任意给出一个序列,如果两两交换了以后会变大,那么就交换,直到不能交换为止。

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: