[滴水石穿]poj 1007-DNA Sorting 结题报告【1】
2012-05-19 18:05
549 查看
原题描述:
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
Sample Output
题目理解比较简单,从第一个字符开始,统计后面比它小的字符个数,如52341,比5小的有4个,比2小的1个,同理,3---1,4---1,总和为4+1+1+1 = 7;
计算逆序和相对比较简单,一个二维循环即可,关键是排序,我这里比较偷懒,没有涉及字符串排序,而是用一个标志数组保存每个串的逆序和,然后每次输出前遍历这个数组,求出最小逆序和的位置,进而输出,并把该逆序和调为最大,以免影响后面输出结果。
通过的关键有两点:1. N要为51,M为101,具体原因不知,理论上说50,100就可以;
2. 最大值要设的足够大,应大于所有逆序和的最大值,开始时设为65535,没有通过,后来改为655350000,就行了;当然为了保险起见,可以单独求出标志数组中最大值max,这样前面说的最大值可以设为max+1;
GCC 实现代码如下:
#include <stdio.h>
#define N 51
#define M 101
int test(char *,int );
int min(int *,int );
int main()
{
char seq[M]
;
int flag[M];
int n,m,i;
int t;
scanf("%d %d",&n,&m);
for(i = 0;i < m;i++){
scanf("%s",seq[i]);
flag[i] = test(seq[i],n);
// printf("%d\n",flag[i]);
}
for(i = 0;i < m;i++){
t = min(flag,m);
flag[t] = 655350000;
printf("%s\n",seq[t]);
}
return 0;
}
int test(char *s,int m){//
int sum = 0;
int i,j;
for(i = 0;i < m - 1; i++){
for(j = i + 1;j < m;j++){
if(s[i] > s[j])
sum ++;
}
}
return sum;
}
int min(int *num,int m){//返回最小值的位置
int i;
int min = num[0],place = 0;
for(i = 0;i < m;i++){
if(num[i] < min){
min = num[i];
place = i;
}
}
return place;
}
To myself:趁做这道题,好好复习各种排序算法,包括字符串排序。
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
题目理解比较简单,从第一个字符开始,统计后面比它小的字符个数,如52341,比5小的有4个,比2小的1个,同理,3---1,4---1,总和为4+1+1+1 = 7;
计算逆序和相对比较简单,一个二维循环即可,关键是排序,我这里比较偷懒,没有涉及字符串排序,而是用一个标志数组保存每个串的逆序和,然后每次输出前遍历这个数组,求出最小逆序和的位置,进而输出,并把该逆序和调为最大,以免影响后面输出结果。
通过的关键有两点:1. N要为51,M为101,具体原因不知,理论上说50,100就可以;
2. 最大值要设的足够大,应大于所有逆序和的最大值,开始时设为65535,没有通过,后来改为655350000,就行了;当然为了保险起见,可以单独求出标志数组中最大值max,这样前面说的最大值可以设为max+1;
GCC 实现代码如下:
#include <stdio.h>
#define N 51
#define M 101
int test(char *,int );
int min(int *,int );
int main()
{
char seq[M]
;
int flag[M];
int n,m,i;
int t;
scanf("%d %d",&n,&m);
for(i = 0;i < m;i++){
scanf("%s",seq[i]);
flag[i] = test(seq[i],n);
// printf("%d\n",flag[i]);
}
for(i = 0;i < m;i++){
t = min(flag,m);
flag[t] = 655350000;
printf("%s\n",seq[t]);
}
return 0;
}
int test(char *s,int m){//
int sum = 0;
int i,j;
for(i = 0;i < m - 1; i++){
for(j = i + 1;j < m;j++){
if(s[i] > s[j])
sum ++;
}
}
return sum;
}
int min(int *num,int m){//返回最小值的位置
int i;
int min = num[0],place = 0;
for(i = 0;i < m;i++){
if(num[i] < min){
min = num[i];
place = i;
}
}
return place;
}
To myself:趁做这道题,好好复习各种排序算法,包括字符串排序。
相关文章推荐
- poj 1007 DNA Sorting 解题报告
- poj 1007 DNA Sorting 解题报告
- poj 1007 DNA Sorting 解题报告
- 【原】 POJ 1007 DNA Sorting 逆序数 解题报告
- POJ 1007 DNA Sorting [解题报告] Java
- poj 1007 DNA Sorting
- POJ 1007 DNA Sorting
- POJ 1007 DNA Sorting
- POJ 1007 解题报告 DNA Sorting
- poj 1007 DNA Sorting 【水题】
- POJ 1007 DNA Sorting
- POJ 1007 DNA Sorting
- POJ 1007 DNA Sorting
- POJ 1007 - DNA Sorting
- POJ 1007 DNA Sorting (简单)
- POJ 1007 DNA Sorting
- POJ 1007 DNA Sorting
- POJ 1007 DNA Sorting
- POJ 1007 DNA Sorting
- POJ 1007 DNA Sorting(简单排序)