private static int GetTotalHealth(TreeChar tree, string[] genes, int[] health, int first, int last, string d) { List <StringSearchResult> ahoCorasickMatching = tree.FindAll(d); Dictionary <string, List <int> > ahoCorasickMatchingDict = ahoCorasickMatching.GroupBy(a => a.Keyword).ToDictionary(a => a.Key, a => a.Select(b => b.Index).ToList()); int result = 0; for (int i = first; i <= last; i++) { if (ahoCorasickMatchingDict.ContainsKey(genes[i])) { foreach (int index in ahoCorasickMatchingDict[genes[i]]) { if (index + genes[i].Length < last) { result += health[index]; } } } } return(result); }