poj1007 DNA Sorting(求逆序数)
2013-08-19 18:26
218 查看
Description
One measure of``unsortedness'' in a sequence is the number of pairs of entriesthat are out of order with respect to each other. For instance, inthe letter sequence ``DAABEC'', this
measure is 5, since D isgreater than four letters to its right and E is greater than oneletter to its right. This measure is called the number ofinversions in the sequence. The sequence ``AACEDGG'' has only oneinversion (E and D)---it is nearly sorted---while
the sequence``ZWQM'' has 6 inversions (it is as unsorted as can be---exactlythe reverse of sorted).
Youare 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, butrather in order
of ``sortedness'', from ``most sorted'' to ``leastsorted''. 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
解析:
题目大意:给定若干个DNA序列(只包含A,C,G和T四个字母),按各自出现的逆序度由小到大输出DNA序列串,例如DNA序列AGCAGT,则其逆序对有GC,GA,CA,所以该串的逆序度为3。只需求每个串的逆序数,然后排序输出相应串。要注意题目中红色标识的那段文字,如果逆序度一样,则按原来的相对顺序输出串。
代码:
#include
#include
using namespace std;
const int MAXLEN = 55;
typedef struct{
int revseq;
char data[MAXLEN];
}DNA,*PDNA;
bool cmp(DNA a,DNA b){
return a.revseq < b.revseq;
}
void get_revseq(PDNA pdna,int n){
int a[3] = {0}; //a[0] = A; a[1] = AC; a[2] = ACG;
pdna->revseq = 0;
while(n-- > 0){
switch(pdna->data
){
case 'A':
a[0] ++;
a[1] ++;
a[2] ++;
break;
case 'C':
a[1] ++;
a[2] ++;
pdna->revseq += a[0];
break;
case 'G':
a[2]++;
pdna->revseq += a[1];
break;
case 'T':
pdna->revseq += a[2];
break;
}
}
}
int main(){
int n,m;
int c,index = 0;
cin>>n>>m;
c = m;
PDNA arr_dna = new DNA[m];
PDNA ptr_dna = arr_dna;
while(c-- > 0){
//memset(ptr_dna->data,0,sizeof(ptr_dna->data)); //无需全部设为0,只需将字符串末尾添加一个'\0'即可,处于效率的考虑
cin>>ptr_dna->data;
ptr_dna->data
= '\0';
get_revseq(ptr_dna,n);
ptr_dna++;
}
stable_sort(arr_dna,arr_dna+m,cmp); //stable_sort是一个稳定的排序
for(int i = 0;i < m;i++) cout<<(*(arr_dna+i)).data<<endl;
delete[] arr_dna;
return 0;
}
One measure of``unsortedness'' in a sequence is the number of pairs of entriesthat are out of order with respect to each other. For instance, inthe letter sequence ``DAABEC'', this
measure is 5, since D isgreater than four letters to its right and E is greater than oneletter to its right. This measure is called the number ofinversions in the sequence. The sequence ``AACEDGG'' has only oneinversion (E and D)---it is nearly sorted---while
the sequence``ZWQM'' has 6 inversions (it is as unsorted as can be---exactlythe reverse of sorted).
Youare 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, butrather in order
of ``sortedness'', from ``most sorted'' to ``leastsorted''. 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
解析:
题目大意:给定若干个DNA序列(只包含A,C,G和T四个字母),按各自出现的逆序度由小到大输出DNA序列串,例如DNA序列AGCAGT,则其逆序对有GC,GA,CA,所以该串的逆序度为3。只需求每个串的逆序数,然后排序输出相应串。要注意题目中红色标识的那段文字,如果逆序度一样,则按原来的相对顺序输出串。
代码:
#include
#include
using namespace std;
const int MAXLEN = 55;
typedef struct{
int revseq;
char data[MAXLEN];
}DNA,*PDNA;
bool cmp(DNA a,DNA b){
return a.revseq < b.revseq;
}
void get_revseq(PDNA pdna,int n){
int a[3] = {0}; //a[0] = A; a[1] = AC; a[2] = ACG;
pdna->revseq = 0;
while(n-- > 0){
switch(pdna->data
){
case 'A':
a[0] ++;
a[1] ++;
a[2] ++;
break;
case 'C':
a[1] ++;
a[2] ++;
pdna->revseq += a[0];
break;
case 'G':
a[2]++;
pdna->revseq += a[1];
break;
case 'T':
pdna->revseq += a[2];
break;
}
}
}
int main(){
int n,m;
int c,index = 0;
cin>>n>>m;
c = m;
PDNA arr_dna = new DNA[m];
PDNA ptr_dna = arr_dna;
while(c-- > 0){
//memset(ptr_dna->data,0,sizeof(ptr_dna->data)); //无需全部设为0,只需将字符串末尾添加一个'\0'即可,处于效率的考虑
cin>>ptr_dna->data;
ptr_dna->data
= '\0';
get_revseq(ptr_dna,n);
ptr_dna++;
}
stable_sort(arr_dna,arr_dna+m,cmp); //stable_sort是一个稳定的排序
for(int i = 0;i < m;i++) cout<<(*(arr_dna+i)).data<<endl;
delete[] arr_dna;
return 0;
}
相关文章推荐
- POJ1007 DNA Sorting (求逆序数)
- POJ1007 DNA Sorting(求逆序数)
- POJ1007 DNA Sorting 排序 C语言
- HDU 1379 DNA sorting(求逆序数)
- 1007 DNA Sorting(字符串+逆序数)
- 【poj1007】DNA Sorting
- POJ1007-DNA Sorting
- POJ1007:DNA Sorting
- POJ1007 DNA Sorting
- POJ1007-DNA Sorting
- POJ1007(DNA Sorting)
- poj 1007 DNA Sorting (求逆序数)
- POJ1007 DNA Sorting
- poj1007-DNA Sorting(排序)
- POJ1007 DNA Sorting
- POJ1007-DNA Sorting-ACM
- POJ1007 DNA Sorting
- POJ1007 DNA Sorting
- poj1007 DNA Sorting
- POJ1007 DNA Sorting