Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

朴素算法O(NN)的找逆叙述

Posted by shenyi0828 at 2009-03-04 17:02:39 on Problem 1007
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator