You are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of ``sortedness‘‘, from ``most sorted‘‘ to ``least sorted‘‘. All the strings are of the same length.
The first line contains two integers: a positive integer n (0 < n <= 50) giving the length of the strings; and a positive integer m (0 < m <= 100) giving the number of strings. These are followed by m lines, each containing a string of length n.
TTTGGCCAAA
附上AC代码:
#include <iostream> #include <cstdio> #include <iomanip> #include <string> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const double PI = acos(-1.0); const int MAX = 100; struct DNA { string str; int count; } dna[MAX]; bool cmp(DNA a, DNA b); int main() { ios::sync_with_stdio(false); int n, m; while (cin >> n >> m) { for (int i=0; i<m; i++) { cin >> dna[i].str; dna[i].count = 0; } for (int i=0; i<m; i++) { for (int j=0; j<n; j++) for (int k=j+1; k<n; k++) if (dna[i].str[j] > dna[i].str[k]) dna[i].count++; } sort(dna, dna+m, cmp); for (int i=0; i<m; i++) cout << dna[i].str << endl; } return 0; } bool cmp(DNA a, DNA b) { return a.count < b.count; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/silenceneo/article/details/47721221