public long Compute(string sequence) { // OLD SOLUTION //long total = 0; //callCompute++; //if (sequence.Length == 0) //{ // return 1; //} //if (cache.ContainsKey(sequence)) //{ // callCache++; // return cache[sequence]; //} //foreach (var word in Dico) //{ // callStartWith++; // if (sequence.StartsWith(word)) // { // checked // { // callRecursive++; // total += Compute(sequence.Substring(word.Length)); // } // } //} var sequenceLength = sequence.Length; var totaux = new long[sequenceLength + 1]; totaux[0] = 1; for (var i = 1; i < sequenceLength + 1; i++) { for (var j = Math.Max(0, i - MaxSize); j < i; j++) { var subSequence = sequence.Substring(j, i - j); if (Dico.ContainsKey(subSequence)) { totaux[i] += totaux[j] * Dico[subSequence]; } } } return(totaux[sequenceLength]); }
protected static object find( string[] names, int index, Dico current) { if (names.Length <= index) { throw new NotFoundException(); } string currentName = names[index]; if (!current.ContainsKey(currentName)) { throw new NotFoundException(); } object obj = current[currentName]; ++index; if (names.Length == index) { return(obj); } currentName = names[index]; Dico submap; if (currentName.StartsWith("<") && currentName.EndsWith(">")) { currentName = currentName.Substring(1, currentName.Length - 2); ListDico list = obj as ListDico; if (currentName.Contains("=")) { bool caseInsensitive = currentName.StartsWith("|") && currentName.EndsWith("|"); if (caseInsensitive) { currentName = currentName.Substring(1, currentName.Length - 2); } bool multiple = currentName.StartsWith("$") && currentName.EndsWith("$"); if (multiple) { currentName = currentName.Substring(1, currentName.Length - 2); } string[] sp = currentName.Split(new char[] { '=' }, 2); string key = sp[0]; string value = sp[1]; Func <string, string, bool> comparator; if (caseInsensitive) { comparator = (a, b) => a.ToLower() == b.ToLower(); } else { comparator = (a, b) => a == b; } try { if (multiple) { ++index; if (names.Length == index) { return(list.Where(e => comparator(e[key] as string, value)).ToList()); } else { return(list.Where(e => comparator(e[key] as string, value)) .Select(e => find(names, index, e)) .ToList()); } } else { submap = list .Where(e => comparator(e[key] as string, value)) .First(); } } catch { throw new NotFoundException(); } } else { if (currentName == "*") { ++index; if (names.Length == index) { return(list.ToList()); } else { return(list .Select(e => find(names, index, e)) .ToList()); } } else { int id = int.Parse(currentName); submap = list[id]; } } ++index; if (names.Length == index) { return(submap); } } else { submap = obj as Dico; if (submap == null) { throw new NotFoundException(); } } return(find(names, index, submap)); }