题目直接暴力,但是考察了稳定排序和非稳定排序,使用sort直接WA了(不稳定排序),而使用stable_sort就过了(稳定排序)
- #include<iostream>
- #include<algorithm>
- #include<cstring>
- #include<cstdio>
- usingnamespacestd;
- constintmaxn=1005;
- intn;
- structnode{
- charstr[maxn];
- intval;
- booloperator<(constnode&a)const{
- returnval<a.val;
- }
- }edge[maxn];
- voidDeal(intk){
- //cout<<len<<endl;
- intsum=0;
- //cout<<edge[k].str<<endl;
- //cout<<n<<endl;
- for(inti=0;i<n;++i){
- for(intj=i+1;j<n;++j){
- if(edge[k].str[i]>edge[k].str[j]){
- sum++;
- //cout<<"aaaaaaaaaaaaaaaa"<<endl;
- }
- }
- }
- edge[k].val=sum;
- }
- intmain(){
- intm;
- while(scanf("%d%d",&n,&m)!=EOF){
- for(inti=0;i<m;++i){
- for(intj=0;j<n;++j){
- cin>>edge[i].str[j];
- }
- //getchar();
- edge[i].val=0;
- Deal(i);
- //cout<<edge[i].val<<endl;
- }
- stable_sort(edge,edge+m);
- for(inti=0;i<m;++i)
- cout<<edge[i].str<<endl;
- puts("********************");
- }
- }