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 该题 利用数组的技巧 巧算逆序数 如函数f所示
运用结构体将 逆序数 与 字符串联系起来
运用algorithm中的stable_sort函数 排序 相同时,不改变原有序列!!
#include<iostream>
#include<algorithm>
using namespace std;
struct DNA{
char str[];
int num;
}d[];
bool cmp(DNA a,DNA b)
{
return a.num<b.num;
}
int f(char s[],int n)
{
int a[]={,,,},m=;
for(int i=n-;i>=;i--){
switch(s[i]){
case 'A':
a[]++;
a[]++;
a[]++;
break;
case 'C':
a[]++;
a[]++;
m+=a[];
break;
case 'G':
a[]++;
m+=a[];
break;
case 'T':
m+=a[];
break;
}
}
return m;
}
int main() {
int n,m;
cin>>n>>m;
for(int i=;i<m;i++){
for(int j=;j<n;j++)cin>>d[i].str[j];
d[i].num=f(d[i].str,n);
}
stable_sort(d,d+m,cmp);
for(int i=;i<m;i++) {
for(int j=;j<n;j++)cout<<d[i].str[j];
cout<<endl;
}
//system("pause");
return ;
}

E - 归并排序 求逆序数的更多相关文章

  1. poj 2299 Ultra-QuickSort :归并排序求逆序数

    点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 34676   Accepted ...

  2. [CF 351B]Jeff and Furik[归并排序求逆序数]

    题意: 两人游戏, J先走. 给出一个1~n的排列, J选择一对相邻数[题意!!~囧], 交换. F接着走, 扔一硬币, 若正面朝上, 随机选择一对降序排列的相邻数, 交换. 若反面朝上, 随机选择一 ...

  3. POJ2299 Ultra-QuickSort(归并排序求逆序数)

    归并排序求逆序数   Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descri ...

  4. HDU 3743 Frosh Week(归并排序求逆序数)

    归并排序求逆序数 #include <iostream> #include <cstdio> using namespace std; #define maxn 1000005 ...

  5. hiho一下 第三十九周 归并排序求逆序数

    题目链接:http://hihocoder.com/contest/hiho39/problem/1 ,归并排序求逆序数. 其实这道题也是可以用树状数组来做的,不过数据都比较大,所以要离散化预处理一下 ...

  6. poj 2299 Ultra-QuickSort 归并排序求逆序数对

    题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...

  7. POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 39279   Accepted: 14163 ...

  8. poj2299解题报告(归并排序求逆序数)

    POJ 2299,题目链接http://poj.org/problem?id=2299 题意: 给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列. 思路: 其实就 ...

  9. poj 2299 Ultra-QuickSort (归并排序 求逆序数)

    题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...

  10. poj 1804 (nyoj 117)Brainman : 归并排序求逆序数

    点击打开链接 Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7810   Accepted: 4261 D ...

随机推荐

  1. OC中限制UITextView的最大字数的实现

    一.属性 //自定义的textview @property (weak, nonatomic) IBOutlet UITextView *textview; //添加一个bool类型的属性 @prop ...

  2. UVA 11754 Code Feat (枚举,中国剩余定理)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud C Code Feat   The government hackers at C ...

  3. Sumblime Text 2 常用插件以及安装方法

    1.直接安装 安装Sublime text 2插件很方便,可以直接下载安装包解压缩到Packages目录(菜单->preferences->packages). 2.使用Package C ...

  4. export-data.js

    var timeBtnClick = (function() { function _todayClick() { $('.select-time .today').on('click', funct ...

  5. 话说 MAX_FILE_SIZE

    PHP关于文件上传部分,特别提到表单隐藏域:MAX_FILE_SIZE,意思是接收文件的最大尺寸.文档中给出的例子如下: <form enctype="multipart/form-d ...

  6. LFS,编译自己的Linux系统 - 编译临时系统

    编译GCC-4.8.2 PASS 1 解压并重命名 cd /mnt/lfs/sources tar -Jxf ../mpfr-3.1.2.tar.xz mv mpfr-3.1.2 mpfr tar - ...

  7. SQL Server 索引重建的 4 种方法

    解决方法 方法 1. 重建指定索引,这种方法没有性能可谈.重建时表还不可访问. 方法 2. 在线重建索引,只有SQL Server 企业版才支持. 方法 3. 使用填充因子重建,这样做不一定可以减小查 ...

  8. USB匹配电阻

    做过USB的人都或许有一个纠结,那就是D+和D-上到底要串多大的电阻,串在源端还是终端. 我想说:网络上的说法都不完全正确,首先USB有低速.全速和高速之分,在低速和全速模式下是电压驱动的,驱动电压为 ...

  9. Eclipse IDE for Java EE Developers使用和新建工程helloworld

    开发j2ee还是用专门的java ee eclipse,自带了许多开发j2ee的插件,包括: This package includes: Data Tools Platform Eclipse Gi ...

  10. html模块一些方法

    <pre name="code" class="python"> find_by_tag_name: @elements = $h->find ...