洛谷 P1012 拼数
真——手写字符串的排序比较函数
这个题目,因为之前没有把更长的字符串后面剩余的字符与另一个字符串的首字母进行比较,所以最后一个点WA了
#include <bits/stdc++.h>
using namespace std;
int n;
struct op{
char st[100001];
};
op a[21];
bool cmp(op x,op y){
int l1=strlen(x.st),l2=strlen(y.st);
int tp=0;
while(tp<min(l1,l2)){
if(x.st[tp]>y.st[tp])
return 1;
else if(x.st[tp]<y.st[tp])
return 0;
++tp;
}//比较两个字符串长度重合部分的字符大小
if(l1<l2){
while(tp<l2){
if(x.st[0]>y.st[tp])
return 1;
else if(x.st[0]<y.st[tp])
return 0;
++tp;
}
}
else if(l1>l2){
while(tp<l1){
if(y.st[0]<x.st[tp])
return 1;
else if(y.st[0]>x.st[tp])
return 0;
++tp;
}
}//分别讨论两个字符串更长的情况,并逐个比较字符串
return 1;
}//手写排序函数
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%s",a[i].st);
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++)
printf("%s",a[i].st);
return 0;
}