特长生模拟 DNA排序(逆序对+快排)

来源:互联网 发布:asp手机考试系统源码 编辑:程序博客网 时间:2024/05/21 08:36

题2、DNA排序

问题描述
逆序对的定义如下:
有一个数列{an},对于任意的ai

var  st:array[0..20000] of string;  nu,a:array[0..20000] of longint;  c:array['A'..'Z'] of longint;  s:string;  i,j,n,l:longint;procedure qsort(l,r:longint);var  i,j,k,k1:longint;begin  if l>=r then exit;  i:=l;j:=r;  k:=a[(l+r) div 2];  k1:=nu[(l+r) div 2];  repeat    while (a[i]<k) or ((a[i]=k) and (nu[i]<k1)) do inc(i);    while (a[j]>k) or ((a[j]=k) and (nu[j]>k1)) do dec(j);    if i<=j then      begin        a[0]:=a[i];a[i]:=a[j];a[j]:=a[0];        st[0]:=st[i];st[i]:=st[j];st[j]:=st[0];        nu[0]:=nu[i];nu[i]:=nu[j];nu[j]:=nu[0];        inc(i);dec(j);      end;  until i>j;  qsort(l,j);qsort(i,r);end;begin  assign(input,'dna.in');reset(input);  assign(output,'dna.out');rewrite(output);  readln(l,n);  for i:=1 to n do    begin      readln(st[i]);      nu[i]:=i;    end;  for i:=1 to n do    begin      s:=st[i];      fillchar(c,sizeof(c),0);      for j:=2 to l do        inc(c[s[j]]);      for j:=1 to l do        begin          if j>1 then dec(c[s[j]]);          case s[j] of            'T':begin                  inc(a[i],c['A']);                  inc(a[i],c['C']);                  inc(a[i],c['G']);                end;            'G':begin                  inc(a[i],c['A']);                  inc(a[i],c['C']);                end;            'C':inc(a[i],c['A']);          end;        end;    end;  qsort(1,n);  for i:=1 to n do    writeln(st[i]);  close(input);close(output);end.