Sunday, January 5, 2020

POJ.1007 DNA Sorting

Problem:
http://poj.org/problem?id=1007

Idea:
n/a

Source:
 int getInvs(string s)  
 {  
      int ans = 0;  
      int n = s.size();  
      for(int i=0;i<n;i++)  
           for (int j = i + 1; j < n; j++) {  
                if (s[i] > s[j]) ans++;  
           }  
      return ans;  
 }  
 bool comp(string a, string b)  
 {  
      return getInvs(a) < getInvs(b);  
 }  
 int main()  
 {  
      vector<string> sv;  
      int n, m;  
      cin >> m >> n;  
      for (int i = 0; i < n; i++) {  
           string s;  
           cin >> s;  
           sv.push_back(s);  
      }  
      sort(sv.begin(), sv.end(), comp);  
      for (int i = 0; i < n; i++) {  
           cout << sv[i] << endl;  
      }  
      return 0;  
 }  

No comments:

Post a Comment