POJ 4086:dna Sort
Topic
Describe
Now there are a number of identical DNA strings (consisting only of ACGT four letters), please sort them in reverse order. The inverse pair refers to the two characters in the string A a[i], A[j], with the properties of I < J and A[i] > A[j]. such as the string "ATCG", T and C are an inverse pair, t and G is another inverse pair, the inverse logarithm of this string is 2.
Input
Line 1th: two integers n and m,n (0<n<=50) represent the length of the string, and M (0<m<=100) indicates the number of strings
Line 2nd to m+1: Each line is a string of length n
Output
In
reverse order the logarithm from less to multiple output strings, reverse the logarithm of as many strings as the input sequence output.
Sample input
Ten 6AACATGAAGGTTTTGGCCAATTTGGCCAAAGATCAGATTTCCCGGGGGGAATCGATGCAT
Sample output
Cccggggggaaacatgaagggatcagatttatcgatgcatttttggccaatttggccaaa
The main problem that we solve is to find out the inverse of a string for this I design the data structure
DNA hashtable each element records the number of occurrences of key in Value-key in the map, which is constantly updated
All right, my algorithmic thinking is presented in code.
#include <iostream> #include <map> #include <fstream> #include <algorithm> #include <string >using namespace Std;class elem{public:int num;int order;string DNA;}; BOOL Compare (Elem A,elem b) {if (A.num < b.num) return True;else if (a.num = = B.num && a.order < B.order) re Turn True;else return false;} map<char,int> dnamap;const int dna_length = 4;void main_solution (); void Read_data (string* & Data,int &m); int inversion (string & DNA); void Initialize_map (map<char,int> &dnamap); int main () {main_solution (); System ("pause"); return 0;} void Initialize_map (map<char,int> &dnamap) {Dnamap.insert (Make_pair (' A ', 0));d Namap.insert (Make_pair (' C ') , 1));d Namap.insert (Make_pair (' G ', 2));d Namap.insert (Make_pair (' T ', 3));} Find out the reverse of the DNA string to int inversion (string & DNA) {int num = 0;int Now;int * dnahash = new int[dna_length];for (int i=0;i&l t;dna_length;i++) {Dnahash[i] = 0;} For (int. i=0;i<dna.length (); i++) {now = Dnamap.find(Dna[i])->second;dnahash[now] ++;for (int j = now+1; j<dna_length; j + +) {num + = Dnahash[j];}} return num;} void Read_data (string* & Data,int &m) {ifstream reader;reader.open ("Data.txt");reader>>m;reader> >m;data = new String[m];for (int i =0; i<m; i++) {reader>>data[i];} Reader.close ();} void Main_solution () {String * Data;int m;read_data (data,m); Initialize_map (DNAMAP); Elem * MySet = new elem[m];for (int i=0;i<m;i++) {Myset[i].dna = Data[i];myset[i].order = I;myset[i].num = Inversion (data [i]);} Sort (myset,myset+m,compare); for (int i=0;i<m;i++) Cout<<myset[i].dna<<endl;}
POJ 4086:dna Sort