A DNA sequence consists of an array of a/c/g/t four letters. The ratio of G to C (defined as Gc-ratio) is the total number of occurrences of the G and C two letters in the sequence divided by the total letter count (that is, the sequence length). In genetic engineering, this proportion is very important. Because high Gc-ratio may be the starting point of the gene.
Given a long DNA sequence, and the required length of the least-boy sequence, researchers often need to find the highest-gc-ratio subsequence in it.
Input |
Enter a string gene sequence, and the length of the int type substring |
Output |
Find the most GC-proportional string |
Sample input |
AACTGTGCACGACCTGA 5 |
Sample output |
Gcacg |
ImportJava.util.Scanner; Public classmain{ Public Static voidMain (string[] args) {Scanner Scanner=NewScanner (system.in); String DNA=Scanner.next (); intn=Scanner.nextint (); System.out.println (Count (dna,n)); Scanner.close (); } Public StaticString count (String DNA,intN) {intlen=dna.length (); floatmaxratio=0.0f; String maxstring=NULL; for(inti=0;i<len-n;i++) {String s=dna.substring (i,i+N); floatR=ratio (s); if(r>maxratio) {Maxratio=R; Maxstring=s; } } returnmaxstring; } Public Static floatratio (String s) {intlen=s.length (); intCnt=0; for(inti=0;i<len;i++) { CharC=S.charat (i); if(c== ' G ' | | | c== ' C ') CNT++; } returncnt*1.0f/Len; }}
Huawei Machine Test-dna-string