半个小时, 一遍AC,真高兴啊
下文为源代码,一些注释是在测试时方便观察和比较用的,大家酌情看吧
/*
DNA Sorting
Time Limit:1000MS Memory Limit:10000K
Total Submit:9694 Accepted:3892
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
*/
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
//////////////////////////////////////////////
//#include<time.h>
//////////////////////////////////////////////
const int MAX_LENGTH = 51;
const int MAX_TIMES = 100;
int times = 0;
int length = 0;
//int tempSortRate[MAX_LENGTH];
struct sorting
{
int index;
int sortrate;
};
inline int cmp(const void* a, const void* b)
{
return ( (sorting*)a )->sortrate - ( (sorting*)b )->sortrate;
}
//getSortRate:在以head为首的字符中查找从第n个开始的排序混乱度
//n从0开始
//由于每次都要搜索n之后的所有字符,因此用DP
//信息保存在tempSortRate中,
//又:所有的A都是不产生排序混乱度的
/*
int getSortRate(char* head, const int n)
{
int SR = 0;
char comparation = *(head + n);
switch(comparation)
{
case 'A':
tempSortRate[n] = getSortRate(head, n+1);
return tempSortRate[n];
break;
case 0:
tempSortRate[n] = 0;
return 0;
break;
default:
break;
}
if(tempSortRate[n] != -1)
{
return tempSortRate[n];
}
SR = getSortRate(head, n + 1);
if( *(head + n + 1) < comparation && *(head + n + 1) != 0)
{
SR++;
}
tempSortRate[n] = SR;
return SR;
}
void initTSR(void)
{
memset(tempSortRate, -1, sizeof(tempSortRate) );
}
*/
//对于每个不同的字符串,都要清空一次tempSortRate
int getSortRate(char* head, const int n)
{
int i = 0;
int j = 0;
int SR = 0;
for(i = 0; i < length - n; i++)
{
for(j = i; j < length - n; j++)
{
if( *(head + n + j) < *(head + n + i) )
{
SR++;
}
}
}
return SR;
}
int main()
{
int i = 0;
char buffer[MAX_TIMES][MAX_LENGTH];
sorting sorted[MAX_TIMES];
memset(buffer, 0, sizeof(buffer) );
memset(sorted, 0, sizeof(sorted) );
scanf("%d %d", &length, ×);
//////////////////////////////////////////
//clock_t start, finish;
//start = clock();
//////////////////////////////////////////
for(i = 0; i < times; i++)
{
// initTSR();
scanf("%s", buffer[i]);
sorted[i].sortrate = getSortRate(buffer[i], 0);
sorted[i].index = i;
}
//for(i = 0; i < times; i++)
qsort(sorted, times, sizeof(sorting), cmp);
for(i = 0; i < times; i++)
{
printf("%s/n", buffer[ sorted[i].index ]);
//printf("%s %d/n", buffer[ sorted[i].index ], sorted[i].sortrate);
}
//////////////////////////////////////////
//finish = clock();
//printf("The Program is finished in %dMS(s)./n", finish - start);
//////////////////////////////////////////
//system("pause");
return 0;
}