2016-10-17 63 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