ZOJ Problem Set–1188 DNA Sorting

Time Limit: 2 Seconds      Memory Limit: 65536 KB


 

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

该题目中仅需要改变一下string的排序方式,然后使用STL容器就可以了。不过有一点,题目中说,如果按照规定的比较方法比较的结果是一样大的话,就按照输入的顺序输出,这一点为了节省了很多麻烦。

代码如下:

 
 
#include<iostream>
#include<set>
#include<string>
#include<algorithm>
#include<list>
using namespace std;
class DNA
{
public:
  string actg;
  DNA(string& s):actg(s){}
  bool operator<(const DNA& dna) const
  {
    return inversionNum() < dna.inversionNum();
  }
  int inversionNum() const
  {
    int bigToRight = 0;
    for(int i = 0; i < actg.length(); i++)
    {
      for(int j = i + 1; j < actg.length(); j++)
      {
        if(actg[i] > actg[j])
          bigToRight++;;
      }
    }
    return bigToRight;
  }
};
int main()
{
  int blocks;cin>>blocks;
  for(int block = 0; block < blocks; block++)
  {
    string state;
    if(block == 0)//仅在第一次的时候有如下两个空行
    {
      getline(cin,state);//用getline 吃掉输入blocks之后的回车
      getline(cin,state);//题目要求的空行
    }
    
    int n,m;
    cin>>n>>m;
    multiset<DNA> se;
    list<string> strList;
    for(int i = 0; i < m; i++)
    {
      string actg;cin>>actg;
      se.insert(DNA(actg));
    }
    for(multiset<DNA>::iterator it = se.begin(); it != se.end(); it++)
    {
      cout<<(*it).actg<<endl;
    }
 
 
    if(block != blocks - 1)//最后一次输出没有空行
    {
      cout<<endl;
    }
  }
  return 0;
}

 

完成之后,我就在想,如果按照题意中的比较方法比较的结果是相等的话,就按string默认的比较方式来排序的话,<重载的方法应该如下所示:

 
 
bool operator<(const DNA& dna) const
{
  return inversionNum() < dna.inversionNum() ? true:actg<dna.actg;
}
但是真的这样做的话,程序是会出错的,老是说我的<重载有错误,这我就不知道错误在哪里了,这个有待研究一下,到底我这样写回导致怎么样的行为,如果要实现我的想法的话,<的重载又该怎么写呢?

转载于:https://www.cnblogs.com/malloc/archive/2012/03/16/2400774.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园建设方案旨在通过先进的信息技术,为师生提供一个全面智能的感知环境和综合信息服务平台。该方案正处在从信息化第二阶段向第三阶段过渡的关键时期,致力于实现校园服务和管理的全面智能化。 方案的核心目标是构建一个集成的校园地理信息服务平台,通过该平台实现资产管理、房产信息管理、基础设施管理、绿化管理和能源监测管理等功能。同时,该平台将提供校园漫游、信息服务、指引服务、活动通知、用房服务和客流统计等多样化服务,以促进校园的和谐、绿色、平安和便捷。 在技术层面,智慧校园建设方案强调系统集成能力、数据统一分析能力、系统资源共享能力以及大数据集成处理能力。通过这些能力,可以构建统一的校园地理信息平台,提供综合的应用支撑和管理能力,实现系统平滑演进。 应用方向上,智慧校园建设方案围绕和谐校园、绿色校园、平安校园和掌上校园四个维度展开。和谐校园侧重于提供校园漫游、信息服务、指引服务等,增强师生的校园体验。绿色校园则关注资产管理和能源监测,推动校园的可持续发展。平安校园通过视频监控、数字巡更等手段,确保校园安全。掌上校园则利用移动设备,实现校园服务的随时随地访问。 最终,智慧校园建设方案将通过三维虚拟校史馆、720度成像技术等创新应用,提供身临其境的校园漫游体验,同时通过可视化管理和数据分析,优化校园资源配置和运营效率,实现校园管理的智能化和现代化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值