2016-10-17 44 views
0

這是我最後一次發佈有關此項目的信息,我幾乎完成了該項目,但我堅持要增加被搜索的子字符串的大小。這是該計劃的提示。OCAML增量搜索子字符大小

Description: 
You are given a DNA sequence: 
a string that contains only characters 'A', 'C', 'G', and 'T'. 
Your task is to calculate the number of substrings of sequence, 
in which each of the symbols appears the same number of times. 

Example 1: 
For sequence = "ACGTACGT", the output should be 6 
All substrings of length 4 contain each symbol exactly once (+5), 
and the whole sequence contains each symbol twice (+1). 

Example 2: 
For sequence = "AAACCGGTTT", the output should be 1 
Only substring "AACCGGTT" satisfies the criterion above: it contains each symbol twice. 


Input: String, a sequence that consists only of symbols 'A', 'C', 'G', and 'T'. 
Length constraint: 0 < sequence.length < 100000. 

這是我code`

let countA = ref 0 
let countC = ref 0 
let countG = ref 0 
let countT = ref 0 
let subStricount = ref 0 
let tempH = ref 0 
let tempT = ref 3 

let countChar x = 
    match x with 
     'A'-> countA := !countA +1; 
    | 'C' -> countC := !countC +1; 
    | 'T' -> countT := !countT +1; 
    | 'G' -> countG := !countG +1; 
;; 
let demoStri = read_line() in 
let striL = String.length demoStri in 
for i = 0 to striL -1 do 
    if !tempT < striL then 
     for j = !tempH to !tempT do 
      countChar demoStri.[j]; 
      if (countA = countC) && (countC = countG) && (countG = countT) then subStricount := !subStricount +1; 
     done; 
     countA := 0; 
     countC := 0; 
     countG := 0; 
     countT := 0; 
     tempH := !tempH +1; 
     tempT := !tempT +1; 
done; 
if String.length demoStri > 4 then 
    for i = 0 to String.length demoStri - 1 do 
     countChar demoStri.[i]; 
    done; 
if (!countA > 0) && (countA = countC) && (countC = countG) && (countG = countT) then subStricount := !subStricount + 1; 


print_int !subStricount; print_string "\n"; 

` 此代碼運行正常計數字符串的輸入,例如ACGTACGT將返回6,但它僅在4子檢索,有沒有一種方法來編碼它,以便在它搜索一個大小爲4的數組後,它會增加大小,直到它達到字符串本身的大小?

回答

1

從概念上講,你想做的事就是以這部分代碼:

let tempH = ref 0 
let tempT = ref 3 

let striL = String.length demoStri in 
. . . 
if (!countA > 0) && (countA = countC) && 
    (countC = countG) && (countG = countT) then 
    subStricount := !subStricount + 1; 

並使其成爲兩個參數的函數:demoStrisubstrLen。初始化tempTsubstrLen - 1

然後調用substrLen許多不同值的函數。

+0

會增加子字符串的大小嗎?我知道,如果我可以得到子字符串的大小增加,我可以擺脫第三個循環底部,但得到它們是困難的 –

+0

你只需要找到代碼的部分取決於子字符串的長度,並使它們取決於參數substrLen。這是編程的根本(恕我直言)。它肯定會工作,所有的軟件都取決於這種抽象:-) –

+0

我不是很確定我明白你的意思,但是,我試着把它放在一邊,現在它給了我錯誤的答案,我只是把整個東西放在一個while循環中,而!tempT