Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
朴素算法O(NN)的找逆叙述#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; string serial; int len; class node { private: string _serial; int unsortedness; public: node() {}; node(const string &serial); int getUnsortedness() const; string getSerial() const; }; vector<node> L; void input(); void solve(); int compare(const node &n1,const node &n2); void output(); int countUnsortedness(); int main() { int n; cin>>len>>n; for (int i=0;i<n;i++) { input(); node tmp(serial); //cout<<serial<<' '<<tmp.getUnsortedness()<<endl; L.push_back(tmp); } solve(); output(); return 0; } void input() { cin>>serial; } int countUnsortedness() { int res=0; for (int i=0;i<len;i++) { for (int j=0;j<len-i;j++) { if (serial[i]>serial[j+i]) res++; } } return res; } node :: node (const string &serial) { _serial=serial; unsortedness=countUnsortedness(); } int node :: getUnsortedness () const { return unsortedness; } string node :: getSerial () const { return _serial; } int compare (const node &n1,const node &n2){ return (n1.getUnsortedness()<n2.getUnsortedness()) ; } void solve() { sort(L.begin(),L.end(),compare); } void output() { for (int i=0;i<L.size();i++) { cout<<L[i].getSerial()<<endl; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator