传送门:牛客
题目描述
设有n个正整数(n ≤ 20),将它们联接成一排,组成一个最大的多位整数。
例如:n=3时,3个整数13,312,343联接成的最大整数为:34331213
又如:n=4时,4个整数7,13,4,246联接成的最大整数为:7424613
毫无疑问这是一道当年NOIP提高组的一道大水题,看下题目我们显然的会有一个贪心的思想,直接排列字符串这样保证字符串开头尽量最大不就行了吗
然后有以下代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string.h>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
inline ll read() {
ll x=0,w=1;char ch=getchar();
for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') w=-1;
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
return x*w;
}
string a[30];
#define maxn 1000000
bool cmp(string num1,string num2) {
return num1>num2;
}
int main() {
int n;n=read();
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n,cmp);
for(int i=0;i<n;i++) cout<<a[i];
return 0;
}
然后交上去发现75分,心里一声我焯,难不成翻车了,仔细观察代码,感觉没问题,看了很久找到一个hack数据
2
231 23
靠原来我们不能直接地排序字符串,而是要将其前后合并之后判断那个应该在前面,那个应该在后面
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string.h>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
inline ll read() {
ll x=0,w=1;char ch=getchar();
for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') w=-1;
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
return x*w;
}
string a[30];
#define maxn 1000000
bool cmp(string num1,string num2) {
return num1+num2>num2+num1;
}
int main() {
int n;n=read();
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n,cmp);
for(int i=0;i<n;i++) cout<<a[i];
return 0;
}
交上去轻松秒杀!