您的位置:首页 > 其它

zoj 1188 DNA Sorting

2017-03-09 15:20 381 查看
DNA SortingTime Limit: 2 Seconds Memory Limit: 65536 KB
One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to its right and E is greater than one letter to its right. This measure is called the number of inversions in the sequence. The sequence ``AACEDGG'' has only one inversion (E and D)--it is nearly sorted--while the sequence ``ZWQM'' has 6 inversions (it is as unsorted as can be--exactly the reverse of sorted).

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.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Input

The first line contains two integers: a positive integer n (0 < n <= 50) giving the length of the strings; and a positive integer m (1 < m <= 100) giving the number of strings. These are followed by m lines, each containing a string of length n.

Output

Output the list of input strings, arranged from ``most sorted'' to ``least sorted''. If two or more strings are equally sorted, list them in the same order they are in the input file.

Sample Input

1

10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT

Sample Output

CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA

注意输出格式,输出的每一个块后需要空一行,最后一个块后不空行

多组测试用例,容器每次要清零

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(const string &s1, const string &s2){
int i, j;
int l1 = s1.length(), l2 = s2.length();
int c1 = 0, c2 = 0;
for(i = 0; i < l1 - 1; i++){
for(j = i + 1; j < l1; j++){
if(s1[i] > s1[j])
c1++;
}
}
for(i = 0; i < l2 - 1; i++){
for(j = i + 1; j < l2; j++){
if(s2[i] > s2[j])
c2++;
}
}
//如果逆序数相同,那么会按原先固有的位置排序
return c1 < c2;
}

int main(){
int t;
int n, m;
string str;
vector<string> v;
cin >> t;
while(t--){
cin >> n >> m;
v.clear();
for(int i = 0; i < m; i++){
cin >> str;
v.push_back(str);
}
sort(v.begin(), v.end(), cmp);
for(vector<string>::iterator it = v.begin(); it != v.end(); it++){
cout << *it << endl;
}
if(t != 0)
cout << endl;
}
//system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: