这是我发布这个项目的最后一次,我差不多完成了这个项目,但我仍然坚持要增加被搜索子字符串大小的部分。这是该计划的提示。
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.
这是我的代码`
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的数组后,它会增加大小,直到达到字符串本身的大小?
答案 0 :(得分: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;
并使其成为两个参数的函数:demoStri
和substrLen
。将tempT
初始化为substrLen - 1
。
然后调用函数以获取substrLen
的许多不同值。