数据范围很小,直接O(n2)统计一下每个字符串逆序对数目,排个序就好了,有点惊讶,用了string,cin竟然还是0ms过的
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define PII pair<int,int> const int N_MAX=50+10,M_MAX=100+10; int n,m;string str[M_MAX];PII cnt[M_MAX]; int main(){ scanf("%d%d", &n, &m); for (int i=1; i<=m; ++i){ cin>>str[i]; cnt[i].first=0; cnt[i].second=i; for (string::iterator it_i=str[i].begin(); it_i!=str[i].end(); ++it_i) for (string::iterator it_j=it_i+1; it_j!=str[i].end(); ++it_j) if (*it_j<*it_i) ++cnt[i].first; } sort(cnt+1,cnt+m+1); for (int i=1; i<=m; ++i) cout<<str[cnt[i].second]<<endl; return 0; }
时间: 2024-12-15 10:28:31