DNA Sorting
2008-02-15 19:19
190 查看
FJNU.1900
PKU.1007
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.
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
Source
East Central North America 1998
My Program
#include<iostream>
#include<string.h>
#define N 500
#define M 1000
using namespace std;
int main()
...{
char str[M]
,temp
;
int d[M],t,p;
int m,n,i,j,k;
cin>>t;
for(p=0;p<t;p++)
...{
cin>>m>>n;
for(i=0;i<n;i++)
...{
cin>>str[i];
d[i]=0;
for(j=0;j<m;j++)
for(k=j+1;k<m;k++)
if(str[i][j]>str[i][k])
d[i]++;
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(d[i]>d[j])
...{
k=d[i];
d[i]=d[j];
d[j]=k;
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
for(i=0;i<n;i++)
cout<<str[i]<<endl;
cout<<endl;
}
return 0;
}
YOYO's Note:
很简单的字符串比较,
当时STL不知道可以写比较函数,
于是自己写了超烂的排序……
额,重点是,它说0 < n <= 50,1 < m <= 100,
但是实际上N开50、M开100是不够的,于是我就在后面加了个0……
还有就是 FJNU里的INPUT比PKU的要多一个输入测试例数,
这里贴的是FJNU里的代码和题目。
PKU.1007
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.
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
Source
East Central North America 1998
My Program
#include<iostream>
#include<string.h>
#define N 500
#define M 1000
using namespace std;
int main()
...{
char str[M]
,temp
;
int d[M],t,p;
int m,n,i,j,k;
cin>>t;
for(p=0;p<t;p++)
...{
cin>>m>>n;
for(i=0;i<n;i++)
...{
cin>>str[i];
d[i]=0;
for(j=0;j<m;j++)
for(k=j+1;k<m;k++)
if(str[i][j]>str[i][k])
d[i]++;
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(d[i]>d[j])
...{
k=d[i];
d[i]=d[j];
d[j]=k;
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
for(i=0;i<n;i++)
cout<<str[i]<<endl;
cout<<endl;
}
return 0;
}
YOYO's Note:
很简单的字符串比较,
当时STL不知道可以写比较函数,
于是自己写了超烂的排序……
额,重点是,它说0 < n <= 50,1 < m <= 100,
但是实际上N开50、M开100是不够的,于是我就在后面加了个0……
还有就是 FJNU里的INPUT比PKU的要多一个输入测试例数,
这里贴的是FJNU里的代码和题目。
相关文章推荐
- poj 1007:DNA Sorting(水题,字符串逆序数排序)
- DNA逆序对的问题: DNA sorting
- poj1007 -- DNA Sorting
- POJ 1007 解题报告 DNA Sorting
- poj 1007 (nyoj 160) DNA Sorting
- DNA Sorting--hdu1379
- poj1007"DNA Sorting"解题报告
- ACM之DNA Sorting
- hdoj 1379 DNA Sorting
- [POJ 1007] DNA Sorting C++解题
- POJ-1007-DNA_Sorting
- POJ1007 DNA Sorting
- POJ 1007 DNA Sorting 水
- DNA Sorting
- poj1007——DNA Sorting
- pku1007 poj 1007 hdu1379 DNA Sorting
- POJ1007 DNA Sorting 水题
- POJ 1007 DNA Sorting
- 杭电1379DNA Sorting
- POJ1007 DNA Sorting 水题