按照字符串的逆序排序。
/*Accepted 100K 16MS C++ 863B 2012-08-03 08:30:48*/ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; const int MAXN = 55, MAXM = 110; struct str { char s[MAXN]; int r; }t[MAXM]; int n, m; bool cmp(str a, str b) { return a.r < b.r; } int main() { int i, j, k; while(scanf("%d%d", &n, &m) == 2) { memset(t, 0, sizeof t); for(i = 0; i < m; i ++) scanf("%s", t[i].s); for(i = 0; i < m; i ++) { t[i].r = 0; for(j = 0; t[i].s[j]; j ++) { for(k = j + 1; t[i].s[k]; k ++) { if(t[i].s[j] > t[i].s[k]) t[i].r ++; } } } sort(t, t + m, cmp); for(i = 0; i < m; i ++) printf("%s\n", t[i].s); } return 0; }