我實現了一個使用字典與元組鍵的算法,算法的工作原理,但它非常慢。我有一組字符串。我試圖實現一個關聯矩陣,其中A["abc","bcde"]
= 2,這兩個字符串的重疊量。 L中的元組是A中的鍵。L是一個排序後的數組=> A [L [i]] < A [L [i + 1]] 我合併了集合中具有最大重疊的兩個字符串, 「矩陣」和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();
}
詞典是驚人的快,不會是你的問題。 – Dessus
你真的應該發佈[mcve]。 – Enigmativity