计算出每一列,每个字母出现的次数,放在一个N*4的count矩阵的每一行中,选择出现次数最多的
如何保证字典序最小?每一行count矩阵从右到左扫描,只要>=max就赋值给max和index(存下标)
import java. util. Scanner;
public class Main {
public static void main ( String[ ] args ) {
Scanner sc = new Scanner ( System. in) ;
int T = sc. nextInt ( ) ;
while ( T -- > 0 ) {
int m= sc. nextInt ( ) ;
int n= sc. nextInt ( ) ;
char[ ] [ ] a= new char [ m] [ n] ;
char[ ] [ ] count= new char [ n] [ 4 ] ;
for ( int i = 0 ; i < a. length; i++ ) {
a[ i] = sc. next ( ) . toCharArray ( ) ;
for ( int j = 0 ; j < a[ 0 ] . length; j++ ) {
if ( a[ i] [ j] == 'A' ) {
count[ j] [ 0 ] ++ ;
} else if ( a[ i] [ j] == 'C' ) {
count[ j] [ 1 ] ++ ;
} else if ( a[ i] [ j] == 'G' ) {
count[ j] [ 2 ] ++ ;
} else if ( a[ i] [ j] == 'T' ) {
count[ j] [ 3 ] ++ ;
}
}
}
int sum= 0 ;
for ( int i = 0 ; i < n; i++ ) {
int max= - 1 ;
int index= 999 ;
for ( int j = 3 ; j >= 0 ; j-- ) {
if ( count[ i] [ j] >= max) {
max= count[ i] [ j] ;
index= j;
}
}
if ( index== 0 ) {
System. out. print ( 'A' ) ;
} else if ( index== 1 ) {
System. out. print ( 'C' ) ;
} else if ( index== 2 ) {
System. out. print ( 'G' ) ;
} else if ( index== 3 ) {
System. out. print ( 'T' ) ;
}
sum+= m- count[ i] [ index] ;
}
System. out. println ( ) ;
System. out. println ( sum) ;
}
}
}