A计划--POJ1007 DNA Sorting
Posted 光光-Leo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了A计划--POJ1007 DNA Sorting相关的知识,希望对你有一定的参考价值。
A题不分早晚!
思路是首先把每行的DNA串使用归并排序计算出逆序数对的个数,然后在对每组产生的逆序数对数对原始数据进行排序
import java.util.Scanner;
public class Main
static String dnas[];
static String dataStr[];
static int data[];
static int step;
static int steps[];
static int number = 0;
static String tempStr[];
public static void sort(int left,int right,int temp[])
if(left<right)
int mid = (left+right)/2;
sort(left,mid,temp);
sort(mid+1,right,temp);
merge(left,mid,right,temp);
public static void merge(int left,int mid,int right,int temp[])
int i = left;
int j = mid+1;
int t = 0;
while(i<=mid&&j<=right)
if(data[i]<=data[j])
temp[t++] = data[i++];
else
temp[t++] = data[j++];
step+=mid-i+1;
while(i<=mid)
temp[t++] = data[i++];
while(j<=right)
temp[t++] = data[j++];
t = 0;
while(left<=right)
data[left++] = temp[t++];
public static void sort2(int left,int right,int temp[])
if(left<right)
int mid = (left+right)>>1;
sort2(left,mid,temp);
sort2(mid+1,right,temp);
merge2(left,mid,right,temp);
public static void merge2(int left,int mid,int right,int temp[])
int i = left;
int j = mid+1;
int t = 0;
while(i<=mid&&j<=right)
if(steps[i]<=steps[j])
temp[t] = steps[i];
tempStr[t] = dnas[i];
t++;
i++;
else
temp[t] = steps[j];
tempStr[t] = dnas[j];
t++;
j++;
while(i<=mid)
temp[t] = steps[i];
tempStr[t] = dnas[i];
t++;
i++;
while(j<=right)
temp[t] = steps[j];
tempStr[t] = dnas[j];
t++;
j++;
t = 0;
while(left<=right)
steps[left] = temp[t] ;
dnas[left]= tempStr[t] ;
left++;
t++;
public static void main(String[] args)
Scanner sc = new Scanner(System.in);
int length = sc.nextInt();
int num = sc.nextInt();
dnas = new String[num];
steps = new int[num];
sc.nextLine();
while(number<num)
String dna = sc.next();
dnas[number] = dna;
dataStr = new String[length];
dataStr = dna.split("");
int temp[] = new int[dataStr.length-1];
data = new int[dataStr.length-1];
for(int j=0;j<data.length;j++)
if("A".equals(dataStr[j+1]))
data[j] = 1;
else if("G".equals(dataStr[j+1]))
data[j] = 3;
else if("C".equals(dataStr[j+1]))
data[j] = 2;
else if("T".equals(dataStr[j+1]))
data[j] = 4;
step = 0;
Main.sort(0, data.length-1, temp);
steps[number] = step;
sc.nextLine();
number++;
int temp[] = new int[steps.length];
tempStr = new String[steps.length];
Main.sort2( 0, steps.length-1,temp);
for(String s:dnas)
System.out.println(s);
以上是关于A计划--POJ1007 DNA Sorting的主要内容,如果未能解决你的问题,请参考以下文章