poj1007 Presentation Error

8 篇文章 0 订阅

通过率:39%          难度:较难

DNA Sorting

Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 46899

 

Accepted: 18311

Description

One measure of``unsortedness'' in a sequence is the number of pairs of entries that are outof 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 itsright and E is greater than one letter to its right. This measure is called thenumber of inversions in the sequence. The sequence ``AACEDGG'' has only oneinversion (E and D)---it is nearly sorted---while the sequence ``ZWQM'' has 6inversions (it is as unsorted as can be---exactly the reverse of sorted).

You are responsible for cataloguing a sequence of DNA strings (sequencescontaining only the four letters A, C, G, and T). However, you want to catalogthem, 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 linecontains two integers: a positive integer n (0 < n <= 50) giving thelength of the strings; and a positive integer m (0 < m <= 100) giving thenumber of strings. These are followed by m lines, each containing a string oflength n.

Output

Output the list ofinput strings, arranged from ``most sorted'' to ``least sorted''. Since twostrings 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

这道题的题意是输入 N 个字符串,按照字符串的逆序数由最少到最大开始输出。解题思路是输入一个字符串后马上计算其逆序数,然后用一个数组 a[i] 存储逆序数,再把 a[i] 的值乘以 1000 ,再加上 i 。这样做的目的是排序的时候可以直接用 a[i] 进行快排,而不影响其对应的字符串(因为 m 最大为 100 ,乘以 1000 后排序可以直接得到排序结果,输出的时候按照 a[i]%1000 输出对应的数组序号)。即可得到结果。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char a[101][51];
int n[101];
//快排函数
int cmp(const void * a, const void * b)
{
    return((*(int*)a-*(int*)b));
}
int main()
{
    int i,r,c;

    scanf("%d%d",&r,&c);
    for(i=0;i<c;i++)
    {    int j,m;
    scanf("%s",a[i]);   //输入字符串
    for(j=0;j<r;j++)   //计算字符串的逆序数
        for(m=j+1;m<r;m++)
            if(a[i][j]>a[i][m])
                n[i]++;
    n[i]=n[i]*1000+i;  //逆序数x1000+原序号储存,快排时不影响原序号
    }
    qsort(n,c,sizeof(n[0]),cmp);  //快排
    for(i=0;i<c;i++) //输出,n[i]%1000,取余1000后得到的为该逆序数对应的原序列的下标。
        printf("%s\n",a[n[i]%1000]);
    return 0;

}

Presentation Error:呈现错误(结果是正确的,但是多输出空格或回车)

参考:http://zhidao.baidu.com/question/86523631.html

ps:自己写的代码比较大而慢,特百度下学习好代码,上面代码出自:

http://wenku.baidu.com/view/a96624d226fff705cc170a42.html

成绩:

1    2367318    chenda91    4K    0MS    Pascal    847B     2007-07-21 15:48:11
3    2390755(5)    tianjie    8K    0MS    C    426B     2007-07-26 04:15:47

3029    11336237(3)    dragoo1    172K    0MS    C++     475B     2013-03-11 09:34:47


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值