zoj 1188
View Code
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 #include <string> 6 7 const int N = 110; 8 string str[N]; 9 struct info{ 10 int strid,invers; 11 }inversions[N]; 12 13 14 void solve(int n,int m); 15 int get(char c) 16 { 17 int ret = 3; 18 if(c == 'A')ret = 0; 19 if(c == 'C')ret = 1; 20 if(c == 'G')ret = 2; 21 return ret; 22 } 23 int cmp(const info &a,const info &b) 24 { 25 if( a.invers < b.invers)return 1; 26 if( a.invers == b.invers){ 27 if(a.strid < b.strid)return 1; 28 else return 0; 29 } 30 return 0; 31 } 32 33 int main() 34 { 35 int test,cas = 0; 36 cin >> test; 37 while(cas++ < test){ 38 int n,m; 39 cin >> m >> n; 40 for(int i = 0; i < n; ++i ){ 41 cin >> str[i]; 42 } 43 solve(n,m); 44 for(int b = 0; b < n; ++b)cout << str[inversions[b].strid] << endl; 45 if(cas < test)cout << endl; 46 } 47 return 0; 48 } 49 50 void solve(int n,int m) 51 { 52 int i,j,k,add; 53 for(i = 0; i < n; ++i){ 54 int counts[N][5]={0}; 55 add = get(str[i][0]); 56 ++counts[0][add]; 57 for(j = 1; j < m; ++j){ 58 for(k = 0; k < 4; ++k){ 59 counts[j][k] += counts[j-1][k]; 60 } 61 add = get(str[i][j]); 62 ++counts[j][add]; 63 } 64 int sum = 0; 65 for(j = 1; j < m; ++j){ 66 if(str[i][j] < 'C'){ 67 sum += counts[j-1][1]; 68 } 69 if(str[i][j] < 'G'){ 70 sum += counts[j-1][2]; 71 } 72 if(str[i][j] < 'T'){ 73 sum += counts[j-1][3]; 74 } 75 } 76 inversions[i].strid = i; 77 inversions[i].invers = sum; 78 } 79 sort(inversions,inversions+n,cmp); 80 }
View Code2
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 #include <string> 6 7 const int N = 110; 8 struct info{ 9 string str; 10 int strid,invers; 11 }node[N]; 12 13 void solve(int n,int m); 14 int get(char c) 15 { 16 int ret = 3; 17 if(c == 'A')ret = 0; 18 if(c == 'C')ret = 1; 19 if(c == 'G')ret = 2; 20 return ret; 21 } 22 int cmp(const info &a,const info &b) 23 { 24 if( a.invers < b.invers)return 1; 25 if( a.invers == b.invers){ 26 if(a.strid < b.strid)return 1; 27 else return 0; 28 } 29 return 0; 30 } 31 32 int main() 33 { 34 int test,cas = 0; 35 cin >> test; 36 while(cas++ < test){ 37 int n,m; 38 cin >> m >> n; 39 for(int i = 0; i < n; ++i )cin >> node[i].str; 40 solve(n,m); 41 for(int b = 0; b < n; ++b)cout << node[b].str << endl; 42 if(cas < test)cout << endl; 43 } 44 return 0; 45 } 46 47 void solve(int n,int m) 48 { 49 int i,j,k,add; 50 string tmp; 51 for(i = 0; i < n; ++i){ 52 tmp = node[i].str; 53 int counts[N][5]={0}; 54 add = get(tmp[0]); 55 ++counts[0][add]; 56 for(j = 1; j < m; ++j){ 57 for(k = 0; k < 4; ++k){ 58 counts[j][k] += counts[j-1][k]; 59 } 60 add = get(tmp[j]); 61 ++counts[j][add]; 62 } 63 int sum = 0; 64 for(j = 1; j < m; ++j){ 65 if(tmp[j] < 'C')sum += counts[j-1][1]; 66 if(tmp[j] < 'G')sum += counts[j-1][2]; 67 if(tmp[j] < 'T')sum += counts[j-1][3]; 68 } 69 node[i].strid = i; 70 node[i].invers = sum; 71 } 72 sort(node,node+n,cmp); 73 }
Code2 用时10ms,结构元素稍复杂。