*【 POJ - 1007 】DNA Sorting(枚举,类似三元组找第二元问题)
題干:
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 ATCGATGCATSample Output
CCCGGGGGGA AACATGAAGG GATCAGATTT ATCGATGCAT TTTTGGCCAA TTTGGCCAAA題目大意:
輸入n個(gè)長(zhǎng)度為m的DNA序列,把他們按照逆序數(shù)從小到大穩(wěn)定排序輸出。定義“穩(wěn)定排序”就是當(dāng)序列中出現(xiàn)A1==A2時(shí),排序前后A1與A2的相對(duì)位置不發(fā)生改變。(n<=50 ,?m<=100)
解題報(bào)告:
? ? ?跟這題差不多。【HihoCoder - 1550】順序三元組。就是找到枚舉每一個(gè)關(guān)鍵元素然后求和就行。
(好像正解是分治?)
AC代碼:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; char maze[105][55]; int num[105]; int a[105],c[105],g[105],t[105]; struct Node {int val,pos;Node(){}Node(int val,int pos):val(val),pos(pos){} } node[105]; bool cmp(const Node & a, const Node & b) {return a.val < b.val; } int main() {int n,m;cin>>m>>n;//n行m列for(int i = 1; i<=n; i++) {scanf("%s",maze[i]+1);} for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(maze[i][j] == 'A') {a[i]++;num[i] += c[i] + g[i] + t[i];}else if(maze[i][j] == 'C') {c[i]++;num[i] += g[i] + t[i];}else if(maze[i][j] == 'G') {g[i]++;num[i] += t[i];}else t[i]++;}} // for(int i = 1; i<=n; i++) printf("%d\n",num[i]);for(int i = 1; i<=n; i++) {node[i] = Node(num[i],i);}sort(node+1,node+n+1,cmp); // for(int i = 1; i<=n; i++) { // printf("%d %d\n",node[i].pos,node[i].val); // }for(int i = 1; i<=n; i++) {printf("%s\n",maze[node[i].pos] + 1);}return 0 ; }這題用逆序數(shù)也可以搞一發(fā)?回頭試試。類(lèi)似HDU - 5775和OpenJ_Bailian - 2299
總結(jié)
以上是生活随笔為你收集整理的*【 POJ - 1007 】DNA Sorting(枚举,类似三元组找第二元问题)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: 第一次办信用卡怎么还 第一次办信用卡怎么
- 下一篇: Tarjan 算法 常用模板
