public TAnswer BF(TSample Sample) { sherlockAndAnagramsSample sample = Sample as sherlockAndAnagramsSample; String s = sample.s; //HashSet<String> Subs = new HashSet<string>(); Dictionary <String, Int32> Subs = new Dictionary <String, Int32>(); Int32 result = 0; for (int i = 0; i <= s.Length - 1; i++) { for (int j = i + 1; j <= s.Length; j++) { char[] ss = s.Substring(i, j - i).ToArray(); Array.Sort(ss); String SortedSubString = new string(ss); if (Subs.ContainsKey(SortedSubString)) { Subs[SortedSubString]++; result += Subs[SortedSubString]; } else { Subs.Add(SortedSubString, 0); } } } return(new sherlockAndAnagramsAnswer() { result = result }); }
public TAnswer DP(TSample Sample) { sherlockAndAnagramsSample sample = Sample as sherlockAndAnagramsSample; String s = sample.s; Int32 result = 0; Dictionary <Char, Int32> Counts = new Dictionary <Char, Int32>(); Int32[] Stats = new Int32[s.Length + 1]; //Int32 MaxLen = s.Length / 2; Int32 MaxLen = s.Length - 1; Int32[] F = new Int32[s.Length]; F[0] = 1; F[1] = 1; Int32 Counter = 0; foreach (var c in s) { if (Counts.ContainsKey(c)) { Counts[c]++; } else { Counts.Add(c, 1); } Stats[Counts[c]]++; if (Counter > 1) { F[Counter] = F[Counter - 1] + Counter; } Counter++; } Int32[,] DP = new Int32[Counts.Count + 1, MaxLen + 1]; Counter = 1; foreach (var c in Counts.Keys) { DP[Counter, 1] = (Counts[c] - 1) * Counts[c] / 2; result += DP[Counter, 1]; Counter++; } Counter = 1; foreach (var c in Counts.Keys) { for (int i = 2; i <= MaxLen; i++) { DP[Counter, i] = DP[Counter - 1, i]; Int32 SCI = Counts[c] - i + 1; if (SCI > 1) { Int32 ACount = F[SCI - 1]; DP[Counter, i] += ACount; } for (int j = 1; j <= i - 1; j++) { DP[Counter, i] += DP[Counter, j] * DP[Counter - 1, i - j]; } } Counter++; } for (int i = 2; i <= MaxLen; i++) { result += DP[Counter - 1, i]; } return(new sherlockAndAnagramsAnswer() { result = result }); }