static void Main(string[] args) { int n = Convert.ToInt32(Console.ReadLine()); string[] genes = Console.ReadLine().Split(' '); int[] health = Array.ConvertAll(Console.ReadLine().Split(' '), healthTemp => Convert.ToInt32(healthTemp)); int s = Convert.ToInt32(Console.ReadLine()); TreeChar root = new TreeChar(genes.ToList()); int unhealthiest = int.MaxValue; int healthiest = int.MinValue; for (int sItr = 0; sItr < s; sItr++) { string[] firstLastd = Console.ReadLine().Split(' '); int first = Convert.ToInt32(firstLastd[0]); int last = Convert.ToInt32(firstLastd[1]); string d = firstLastd[2]; int value = GetTotalHealth(root, genes, health, first, last, d); if (value < unhealthiest) { unhealthiest = value; } if (value > healthiest) { healthiest = value; } } Console.WriteLine($"{unhealthiest} {healthiest}"); }
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); }