http://acm.pku.edu.cn/JudgeOnline/problem?id=1007 #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; typedef struct { int Unsortedness; string DNAstring; }DNA; class LESS { public: bool operator()( DNA dna1, DNA dna2 ) { return dna1.Unsortedness < dna2.Unsortedness; } }; int Number(string& str); vector<DNA> DNAVector; vector<DNA>::iterator it; int main() { int n,m; DNA dna; string str; cin>>n>>m; for( int i = 0; i < m; ++i ) { cin>>dna.DNAstring; dna.Unsortedness = Number(dna.DNAstring); DNAVector.push_back( dna ); } stable_sort(DNAVector.begin(),DNAVector.end(),LESS() ); for( it = DNAVector.begin(); it != DNAVector.end(); ++it ) { cout<<it->DNAstring<<endl; } return 0; } int Number(string& str) { int count = 0; for (int i = 0; i < str.length() - 1; ++i) { for ( int j = i; j < str.length(); ++j) { if( str.at(i) > str.at(j) ) { ++count; } } } return count; }