poj 1007

DNA Sorting

Time Limit: 1000MS
Memory Limit: 10000K

Total Submissions: 52558
Accepted: 20563

Description

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.

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 (0 < 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''. Since two strings can be equally sorted, then output them according to the orginal order.

Sample Input

10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT

Sample Output

CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA
 
 
求逆序对
归并排序
 
View Code
var
num,len,i,j,l,anss:longint;
f:
array['A'..'Z'] of integer;
ff:
array[1..26] of char;
ans:
array[1..100] of longint;
a,temp1,temp2:
array[0..50] of longint;
b:
array[1..100,0..50] of longint;
ch:char;
procedure merge(l,r:longint);
var
i,j,mid,l1,l2,k:longint;
begin
mid:
=(l+r)>>1;
if l<mid then merge(l,mid);
if mid+1<r then merge(mid+1,r);
l1:
=mid-l+1;
l2:
=r-mid;
for i:=1 to l1 do
temp1[i]:
=a[l+i-1];
for i:=1 to l2 do
temp2[i]:
=a[mid+i];
temp1[l1
+1]:=maxlongint;
temp2[l2
+1]:=maxlongint;
i:
=1; j:=1;
for k:=l to r do
begin
if temp1[i]<=temp2[j] then
begin
a[k]:
=temp1[i];
inc(i);
end
else
begin
a[k]:
=temp2[j];
inc(j);
inc(anss,l1
-i+1);
end;
end;
end;
procedure qs(r,l:longint);
var
i,j,x,y:longint;
temp:
array[0..50] of longint;
begin
i:
=r; j:=l;
x:
=ans[(i+j) div 2];
repeat
while ans[i]<x do inc(i);
while ans[j]>x do dec(j);
if i<=j then
begin
y:
=ans[i];
ans[i]:
=ans[j];
ans[j]:
=y;
temp:
=b[i];
b[i]:
=b[j];
b[j]:
=temp;
inc(i);
dec(j);
end;
until i>j;
if i<l then qs(i,l);
if j>r then qs(r,j);
end;
begin
assign(input,
'input.in');
assign(output,
'output.out');
reset(input);
rewrite(output);
readln(len,num);
i:
=1;
for ch:='A' to 'Z' do
begin
f[ch]:
=i; inc(i);
end;
ff[
1]:='A';
ff[
7]:='G';
ff[
3]:='C';
ff[
20]:='T';
for i:=1 to num do
begin
a[
0]:=0;
for j:=1 to len do
begin
read(ch);
inc(a[
0]);
a[a[
0]]:=f[ch];
end;
readln;
b[i]:
=a;
anss:
=0;
merge(
1,len);
ans[i]:
=anss;
end;
qs(
1,num);
for i:=1 to num do
begin
for j:=1 to len do
write(ff[b[i,j]]);
writeln;
end;
close(input);
close(output);
end.
posted on 2011-03-01 11:53  leve  阅读(142)  评论(0编辑  收藏  举报