hdoj1379DNA Sorting

来源:互联网 发布:dota2 mac怎么全屏 编辑:程序博客网 时间:2024/06/10 00:12


Problem 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. 


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
110 6AACATGAAGGTTTTGGCCAATTTGGCCAAAGATCAGATTTCCCGGGGGGAATCGATGCAT
 

Sample Output
CCCGGGGGGAAACATGAAGGGATCAGATTTATCGATGCATTTTTGGCCAATTTGGCCAAA
代码:
#include<algorithm>#include<iostream>#include<string.h>using namespace std;struct stu{char a[100];int b;}st[100];bool cmp(stu x ,stu y){if(x.b!=y.b)return x.b<y.b;elsereturn strcmp(x.a,y.a)<0;}int main(){int n;scanf("%d",&n);while(n--){int q,w,i,j,k,sum=0;scanf("%d%d",&q,&w);for(i=1;i<=w;i++){scanf("%s",st[i].a);}for(i=1;i<=w;i++){for(j=0;j<q;j++){for(k=0;k<=j;k++){if(st[i].a[j]<st[i].a[k])st[i].b++;}}}sort(st+1,st+w+1,cmp);for(i=1;i<=w;i++){printf("%s\n",st[i].a);}}return 0;}
思路:用sort快排和结构体解答。这道题做的时候不会将结构体中的两个变量捆绑起来一起变化,所以请教师傅来着,虽然现在还是有点不太明白,但是会用了。
Problem 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. 


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
110 6AACATGAAGGTTTTGGCCAATTTGGCCAAAGATCAGATTTCCCGGGGGGAATCGATGCAT
 

Sample Output
CCCGGGGGGAAACATGAAGGGATCAGATTTATCGATGCATTTTTGGCCAATTTGGCCAAA
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 横屏游戏变竖屏怎么办 三星w2016购买后怎么办 电脑账户删不了怎么办 手机副版出问题怎么办 越狱证书过期了怎么办 iphone7不停重启怎么办 手机被病毒入侵怎么办 苹果手机不息屏怎么办 苹果手机散屏怎么办 苹果手机屏幕死机了怎么办 苹果手机屏死机怎么办 微信号账号异常怎么办 手机设置加密了怎么办 华为手机触屏坏了怎么办 安卓启动器停止怎么办 手机显示停止运行怎么办 手机来电被拦截怎么办 手机被加黑名单怎么办 手机短信被屏蔽了怎么办 手机信息被拦截怎么办 手机有感染病毒怎么办 华为p10速度慢怎么办 华为手机清理慢 怎么办 华为手机有回音怎么办 手机没有返回键怎么办 手机总显示内存不足怎么办 扩音器耳麦接触不良怎么办 音响插头接触不良怎么办 华为手机耳机声音小怎么办 苹果耳机孔变形怎么办 耳机孔松了怎么办 荣耀9青春版费电怎么办 华为双清了怎么办 华为开不开机怎么办 华为手机黑屏打不开怎么办 荣耀v8指纹消失怎么办 耳机话筒进水了怎么办 beats耳机进水了怎么办 音量孔进水了怎么办 苹果手机屏幕进水了怎么办 耳机孔进水了怎么办