2016-10-17 2 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は、しかし、それは唯一の4の部分文字列で検索され、6を返します。サイズ4の配列を検索した後、文字列自体のサイズに達するまでサイズを増やすようにコードを書く方法はありますか?

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

答えて

1

は、概念的には、あなたが何をしたいのか、このコードの一部を取ることです。 tempTsubstrLen - 1を初期化します。

次に、substrLenの多くの異なる値の関数を呼び出します。

+0

これは、サブ文字列のサイズを増やすでしょうか?私は、部分文字列のサイズを増やすことができれば、最下位のループの3分の1を取り除くことができますが、難しいのは難しいことです。 –

+0

部分文字列の長さに依存するコードの部分を見つけ出すだけで済みます。代わりに 'substrLen'パラメータに依存します。これがプログラミングの本質です(IMHO)。それは間違いなく動作しますが、すべてのソフトウェアはこの種の抽象化に依存しています:-) –

+0

私はあなたが何を意味するのか分かりませんが、しばらく置いてみました。 !whileTopT

関連する問題