标签:poj 1007
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
分析:
问题的关键在于如何求解一个序列的无序度,最差肯定是O(n2)的,这里我们可以从归纳的角度分析问题,如果已知前n-1个字母序列的无序度,那么对于第n个字母,我们需要和前n-1个字母比较来更新,问题似乎没变,还是一样的复杂度,但是这里我们只有ACGT四个字母,我们想如何用更强的归纳来减少问题的规模,对于第n个字母,我们不需要显式的和前面n-1个字母比较,只要知道前面有几个A,几个C,几个G,几个T,我们就可以直接更新无序度了,如果为A,则新的无序度需要加上前n-1个字母的CGT个数,使用更强的归纳解决问题!
#include <iostream> using namespace std; int find_sortedness(char *seq,int length) { int numA=0,numC=0,numG=0,numT=0; int unsort=0; for(int i=0;i<length;i++) { if(seq[i]==‘A‘) { unsort+=(numC+numG+numT); numA++; } else if(seq[i]==‘C‘) { unsort+=(numG+numT); numC++; } else if(seq[i]==‘G‘) { unsort+=numT; numG++; } else { numT++; } } return unsort; } int main() { int length=0; int nums=0; cin>>length>>nums; char** a=new char *[nums]; int* results=new int[nums]; int* print=new int[nums]; for(int i=0;i<nums;i++) { a[i]=new char[length]; for(int j=0;j<length;j++) { cin>>a[i][j]; } results[i]=find_sortedness(a[i],length); print[i]=i; } for(int i=0;i<nums;i++) for(int j=nums-1;j>i;j--) { if(results[j]<results[j-1]) { int tmp=results[j]; results[j]=results[j-1]; results[j-1]=tmp; tmp=print[j]; print[j]=print[j-1]; print[j-1]=tmp; } } for(int i=0;i<nums;i++) { for(int j=0;j<length;j++) { cout<<a[print[i]][j]; } cout<<endl; } return 0; }
本文出自 “这与那” 博客,请务必保留此出处http://hereandthere.blog.51cto.com/7561107/1605819
标签:poj 1007
原文地址:http://hereandthere.blog.51cto.com/7561107/1605819