UVa 1368 - DNA Consensus String

这是连续第8次1Y了,哈哈哈,不过,不过这题看起来挺吓人,读完才知道就是让球一个目标DNA序列,和每个所给序列最相近。不是从里面选,第一次就是这么理解的然后。。。。。是自己用A C G T中组合。如果有多解选字典序最小的。

题目定位 : 字符串水题。 貌似有点贪心的意思。

上Java代码 :

import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
	    int t = scan.nextInt();
	    while(t-- > 0) {
	    	int m = scan.nextInt();
	    	int n = scan.nextInt();
	    	StringBuilder sb = new StringBuilder();
	    	String s;
	    	DNA[] dna = new DNA[m + 1];
	    	for(int i=0; i<m; i++) {
	    		s = scan.next();
	    		dna[i] = new DNA(s, 0);
	    	}
	    	int[] cnt = new int[4];
	    	int dis = 0;
	    	for(int j=0; j<n; j++) {
	    		Arrays.fill(cnt, 0);
	    		for(int i=0; i<m; i++) {
	    			if(dna[i].str.charAt(j) == 'A') {
	    				cnt[0] ++;
	    			}
	    			if(dna[i].str.charAt(j) == 'C') {
	    				cnt[1] ++;
	    			}
	    			if(dna[i].str.charAt(j) == 'G') {
	    				cnt[2] ++;
	    			}
	    			if(dna[i].str.charAt(j) == 'T') {
	    				cnt[3] ++;
	    			}
	    		}
	    		int max = cnt[0];
	    		char ch = 'A';
	    		for(int k=1; k<4; k++) {
	    			if(cnt[k] > max) {
	    				max = cnt[k];
	    				if(k == 1) {
	    					ch = 'C';
	    				}
	    				if(k == 2) {
	    					ch = 'G';
	    				}
	    				if(k == 3) {
	    					ch = 'T';
	    				}
	    			}
	    		}
	    		if(ch == 'A') {
	    			dis += cnt[1] + cnt[2] + cnt[3];
	    		}
	    		if(ch == 'C') {
	    			dis += cnt[0] + cnt[2] + cnt[3];
	    		}
	    		if(ch == 'G') {
	    			dis += cnt[0] + cnt[1] + cnt[3];
	    	    }
	    		if(ch == 'T') {
	    			dis += cnt[1] + cnt[2] + cnt[0];
	    		}
	    		sb.append(ch);
	    		
	    	}
	    	System.out.println(sb);
	    	System.out.println(dis);
	    }
	    

	}

}
class DNA {
	public String str;
	public int cnt;
	public DNA(String str, int cnt) {
		this.str = new String(str);
		this.cnt = cnt;
	}
}


 

 

posted @ 2014-12-25 12:45  Pickle  阅读(353)  评论(0编辑  收藏  举报