6
我發現這段代碼,計算答案和猜測之間Levenshtein's distance:Levenshtein距離C#計數錯誤類型
int CheckErrors(string Answer, string Guess)
{
int[,] d = new int[Answer.Length + 1, Guess.Length + 1];
for (int i = 0; i <= Answer.Length; i++)
d[i, 0] = i;
for (int j = 0; j <= Guess.Length; j++)
d[0, j] = j;
for (int j = 1; j <= Guess.Length; j++)
for (int i = 1; i <= Answer.Length; i++)
if (Answer[i - 1] == Guess[j - 1])
d[i, j] = d[i - 1, j - 1]; //no operation
else
d[i, j] = Math.Min(Math.Min(
d[i - 1, j] + 1, //a deletion
d[i, j - 1] + 1), //an insertion
d[i - 1, j - 1] + 1 //a substitution
);
return d[Answer.Length, Guess.Length];
}
但我需要一個方式做一個計數的時間發生的每個錯誤的數量。有沒有簡單的方法來實現呢?
我用「你好」和一個例子嘗試這種「你好」,它的缺失和插入加起來當它是正確的。這是我需要的東西只是不確定這一點是否正在工作! – user1988332 2013-03-21 23:46:03
當「hello」正確時,刪除和插入兩者都會吐出數字9? – user1988332 2013-03-22 00:30:13
基本問題是我誤解了發生了什麼。在代碼中,當'i == j'時只想增加刪除,插入等。至少,我認爲這就是問題所在。您可能需要嘗試一下。我對我的代碼進行了更改。 – 2013-03-22 01:02:37