タプルキーで辞書を使ってアルゴリズムを実装しましたが、アルゴリズムは動作しますが、非常に遅いです。私は弦のセットを持っています。私はA["abc","bcde"]
= 2という2つの文字列の重なりの量を表す連想行列を実装しようとしていました。 LのタプルはAのキーです。Lはソートされた配列です。> A [L [i]] < A [L [i + 1]] 2つの文字列をセット内で最も重複してマージしてから更新します「マトリクス」とLリスト。セットが1つだけの要素になるまで私はループでそれを行います。私の問題は、辞書のアルゴリズムが遅すぎるということです。これを行うより効率的な方法はありますか?ここに私のコードです:C#の連想マトリックスの方が効率的ですか?
List<string> words = new List<string>(wordsFromFile);
Dictionary<Tuple<string, string>, int> A = new Dictionary<Tuple<string, string>, int>();
List<Tuple<string, string>> L = new List<Tuple<string,string>>();
(私は行列をリフレッシュその後L.を作るためのソートを数える使用し、リストは非常に時間がかかる:)
while (words.Count > 1)
{
string LastItem1 = L.Last().Item1;
string LastItem2 = L.Last().Item2;
words.Remove(LastItem1);
words.Remove(LastItem2);
string newElement = merge(LastItem1, LastItem2);
words.Add(newElement);
for (int i = 0; i < words.Count; ++i)
{
if (words[i] == newElement)
{
Tuple<string, string> tmp = new Tuple<string, string>(newElement, newElement);
A[tmp] = 0;
}
else
{
Tuple<string, string> tmp = new Tuple<string, string>(newElement, words[i]);
A[tmp] = A[new Tuple<string, string>(LastItem2, words[i])];
tmp = new Tuple<string, string>(words[i], newElement);
A[tmp] = A[new Tuple<string, string>(words[i], LastItem1)];
}
}
var itemsToRemove = A.Where(f => f.Key.Item1 == LastItem1 || f.Key.Item1 == LastItem2 || f.Key.Item2 == LastItem1 || f.Key.Item2 == LastItem2).ToArray();
foreach (var item in itemsToRemove)
A.Remove(item.Key);
L.Remove(L.Last());
for (int i = 0; i < L.Count(); ++i)
{
if (L[i].Item1 == LastItem2 && L[i].Item2 != LastItem1 && L[i].Item2 != newElement && L[i].Item2 != LastItem2) L[i] = new Tuple<string, string>(newElement, L[i].Item2);
else if (L[i].Item2 == LastItem1 && L[i].Item1 != LastItem1 && L[i].Item1 != newElement && L[i].Item1 != LastItem2) L[i] = new Tuple<string, string>(L[i].Item1, newElement);
}
var listitemsToRemove = L.Where(f => f.Item1 == LastItem1 || f.Item2 == LastItem2 || f.Item1 == LastItem2 || f.Item2 == LastItem1).ToArray();
foreach (var item in listitemsToRemove) L.Remove(item);
listitemsToRemove = L.Where(f => f.Item2 == LastItem2).ToArray();
}
辞書は驚くほど速いです、それをアップになります[I](非効率的にforeachループと比較して)3つのルックアップを行っています、。 – Dessus
本当に[mcve]を投稿してください。 – Enigmativity