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
很有意思的题目, POJ的题目都感觉相当好。
按照排序度的高低来排序,排序的排序?呵呵
因为数据的特殊性,所以本题可以计算逆序数的方法,可以0MS通过的,不过这里我使用的方法是:
1 merge sort来计算排序度
2 继续使用归并排序,排好最终序列
二次归并排序啊,呵呵,最终运行速度是16MS,做不到0MS,但是可以运行在无数据特殊性的情况下,通用性高。
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
struct DNA
{
string s;
int n;
DNA(int i = 0, string ss = "") : n(i), s(ss){}
bool operator<(const DNA &a) const
{
return n < a.n;
}
};
string biMerge(string &L, string &R, int &c)
{
string s;
unsigned i = 0, j = 0;
while ( i < L.size() && j < R.size() )
{
if (L[i] <= R[j]) s.push_back(L[i++]);
else
{
s.push_back(R[j++]);
c += L.size() - i;
}
}
if ( i < L.size() ) s.append(L.substr(i));
else s.append(R.substr(j));
return s;
}
void biSort(string &s, int &c)
{
if (s.size() < 2) return ;
int mid = ((s.size()-1)>>1);//错误:写成s.size()>>1
string L(s.substr(0, mid+1));
biSort(L, c);
string R(s.substr(mid+1));
biSort(R, c);
s = biMerge(L, R, c);
}
void DNASorting()
{
int Len = 0, T = 0, c = 0;
cin>>Len>>T;
vector<DNA> dnas(T);
string t;
for (int i = 0; i < T; i++)
{
cin>>t;
dnas[i].s = t;
c = 0;
biSort(t, c);
dnas[i].n = c;
}
sort(dnas.begin(), dnas.end());
for (int i = 0; i < T; i++)
{
cout<<dnas[i].s<<endl;
}
}
int main()
{
DNASorting();
return 0;
}
分享到:
相关推荐
总结来说,"POJ1007-DNA Sorting"是一个涉及生物信息学和算法设计的编程挑战,通过解这个问题,程序员可以提升处理字符串排序的能力,同时了解如何在实际问题中应用编程技巧。解题报告和代码提供了学习和借鉴的实例...
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 34868 Accepted: 13480 Description One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are ...
【标签】"POJ 1094 Sorting It All Out" 明确了这是POJ平台上编号为1094的一道关于“Sorting”(排序)的题目。在编程竞赛中,标签常用于分类题目,便于参赛者根据自己的专长选择合适的题目。 从题目名和标签我们...
poj-1002源码,没有题解,题解看博客poj-1002源码,没有题解,题解看博客poj-1002源码,没有题解,题解看博客poj-1002源码,没有题解,题解看博客
poj dna sorting 问题,研究的ac coderrrrrrr
【标题】"POJ-1009"是北大在线编程竞赛平台上的一个题目,它涉及到计算机科学中的算法和编程技巧。POJ(Problem Set of Peking University)是中国北京大学主办的一个在线编程竞赛平台,旨在提高学生的算法设计和...
POJ---1456.Supermarket测试数据及答案,题目描述:A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an integral ...
POJ-3299解题 C++源代码 Description Adapted from Wikipedia, the free encyclopedia The humidex is a measurement used by Canadian meteorologists to reflect the combined effect of heat and humidity. It...
【标题】"poj-2528 Mayor's posters 测试数据及答案"涉及的是一个编程竞赛中的问题,这个问题来源于POJ(Programming Online Judge)平台,编号为2528。POJ是一个在线的编程练习系统,它提供了一系列的问题供程序员...
标题“POJ3253-POJ3253-Fence Repair【STL优先队列】”指的是一个在线编程竞赛题目,源自北京大学的在线判题系统POJ(Problem Online Judge)。该题目要求参赛者使用C++编程语言解决特定的问题,并且在解决方案中...
### ACM新手刷题攻略之POJ ... - 推荐题目:[poj1068](https://vjudge.net/problem/POJ-1068)、[poj2632](https://vjudge.net/problem/POJ-2632)、[poj1573](https://vjudge.net/problem/POJ-1573)、[poj2993]...
【标题】"poj-1028-Web-Navigation.zip_poj" 指向的是一个编程竞赛问题,POJ(Programming Online Judge)平台上的第1028题,名为“Web Navigation”。该问题通常涉及到算法设计和实现,可能是为了考察参赛者的编程...
【标题】"POJ-2151.rar_poj"是一个与编程竞赛相关的压缩文件,主要涉及的问题是“检查问题的难度”,并且是为动态规划的实战练习而设计的。这个题目来自于著名的在线编程竞赛平台POJ(Programming Online Judge)。 ...
标题中的“POJ-1170.rar_poj”指的是编程竞赛网站POJ(Programming Online Judge)上的一个问题,编号为1170。POJ是一个在线的编程练习平台,主要面向学习和提升算法与编程技能的用户。这个问题的解决方案可能包含在...