DNA Sorting
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 80832   Accepted: 32533

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

Source

 
  水题,字符串排序
  题意:给你m个长度为n的字符串,要求让你按照字符串的逆序数进行稳定排序。
    所谓逆序数,就是给定一个排列和一个标准次序,如果这个排列中有两个元素与标准次序不同,则称这是一个逆序,这个排列的所有逆序总数称为这个排列的逆序数。
  思路:定义一个结构体存储字符串和字符串的逆序数,在主程序里,输入字符串之后计算每一个字符串的逆序数。然后用sort进行排序,最后输出排序后的字符串。
  代码

 #include <iostream>
#include <algorithm>
using namespace std;
struct Str{
char s[];
int n; //逆序数个数
};
bool cmp(const Str &a,const Str &b)
{
if(a.n<b.n)
return ;
return ;
}
int main()
{
int n,m,i,j;
Str str[];
while(cin>>n>>m){
for(i=;i<=m;i++) //输入
cin>>str[i].s;
for(i=;i<=m;i++){
int num=;
int A=,C=,G=;
for(j=n-;j>=;j--){
switch(str[i].s[j]){ //计算逆序数
case 'A':A++;break;
case 'C':C++;num+=A;break;
case 'G':G++;num+=A;num+=C;break;
case 'T':num+=A;num+=C;num+=G;break;
default:break;
}
}
str[i].n = num;
}
sort(str+,str+m+,cmp);
for(i=;i<=m;i++)
cout<<str[i].s<<endl;
}
return ;
}

Freecode : www.cnblogs.com/yym2013

poj 1007:DNA Sorting(水题,字符串逆序数排序)的更多相关文章

  1. POJ 1007 DNA Sorting(sort函数的使用)

    Description One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are ...

  2. [POJ 1007] DNA Sorting C++解题

        DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 77786   Accepted: 31201 ...

  3. poj 1007 DNA Sorting

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 95437   Accepted: 38399 Des ...

  4. [POJ] #1007# DNA Sorting : 桶排序

    一. 题目 DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 95052   Accepted: 382 ...

  5. poj 1007 DNA sorting (qsort)

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 95209   Accepted: 38311 Des ...

  6. POJ 1007 DNA sorting (关于字符串和排序的水题)

    #include<iostream>//写字符串的题目可以用这种方式:str[i][j] &str[i] using namespace std; int main() {int ...

  7. poj 1007 DNA Sorting 解题报告

    题目链接:http://poj.org/problem?id=1007 本题属于字符串排序问题.思路很简单,把每行的字符串和该行字符串统计出的字母逆序的总和看成一个结构体.最后把全部行按照这个总和从小 ...

  8. POJ 1488 Tex Quotes --- 水题

    POJ 1488 题目大意:给定一篇文章,将它的左引号转成 ``(1的左边),右引号转成 ''(两个 ' ) 解题思路:水题,设置一个bool变量标记是左引号还是右引号即可 /* POJ 1488 T ...

  9. poj 2105 IP Address(水题)

    一.Description Suppose you are reading byte streams from any device, representing IP addresses. Your ...

随机推荐

  1. Java web中为什么要用Service接口和DAO接口?

    面向接口:依赖倒转原理----使用service接口的原因是为了让表示层不依赖于业务层的具体实现,使用dao接口的原理也是如此,而且便于spring ioc容器,当修改dao层,时不需要修改servi ...

  2. url中出现井号("#")的问题

    今天在asp.net mvc网站项目的前台页面里写一段js代码时,想要跳转到某个url,例如 location.href="xxxx?"+"id="+id+&q ...

  3. 升级centos内核到最新版本

    root权限执行: rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org rpm -Uvh http://www.elrepo.org/ ...

  4. Group Anagrams

    Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...

  5. ERROR Cannot determine the location of the VS Common Tools Folder

    参考:ERROR Cannot determine the location of the VS Common Tools Folder   http://blog.csdn.net/m3728975 ...

  6. IOS lib(.a)库冲突解决办法

    在引入第三方lib(.a)库时,经常会由于第三方lib库中又引入同你现有工程相同的开源代码而造成.o冲突,最近在集成汉王名片识别时发生ASIHttp的.o冲突.我想说的是像这种开源的使用率很高的源代码 ...

  7. 多字段 java对象排序

    public class ReflexUtil { static Logger logger = LoggerFactory.getLogger(ReflexUtil.class); //getMet ...

  8. 无IDE时编译和运行Java

    最近 Java subreddit 出现了一篇”在没有IDE的情况下编译Java包” 的帖子,这个帖子抛出了这么一个问题,“是否存在一个命令可以编译一组处于同一文件夹下独立包内的java文件的方法(这 ...

  9. IOS-委托代理(degegate)

    委托代理: 委托代理(degegate)顾名思义,把某个对象要做的事情委托给别的对象去做.那么别的对象就是这个对象的代理,代替它来打理要做的事.反映到程序中, 首先要明确一个对象的委托方是哪个对象,委 ...

  10. php图片防盗链的小测试

    test.php <?php $txt = "http://hiphotos.baidu.com/stupidet/pic/item/4f1b8cfb4c33b7254e4aea69. ...