protected void Decompress() { char[] chars = CompressedString.ToCharArray(); StringBuilder builder = new StringBuilder(); double count = 1; for (var i = 0; i < chars.Length; i++) { char current = chars[i]; if (!char.IsNumber(current)) { for (var j = 0; j < count; j++) { builder.Append(current); } count = 1; } else { count = char.GetNumericValue(current); } } DecompressedString = builder.ToString(); }
private static Boolean KawigiEdit_RunTest(int testNum, string p0, Boolean hasAnswer, int p1) { Console.Write("Test " + testNum + ": [" + "\"" + p0 + "\""); Console.WriteLine("]"); CompressedString obj; int answer; obj = new CompressedString(); DateTime startTime = DateTime.Now; answer = obj.getLength(p0); DateTime endTime = DateTime.Now; Boolean res; res = true; Console.WriteLine("Time: " + (endTime - startTime).TotalSeconds + " seconds"); if (hasAnswer) { Console.WriteLine("Desired answer:"); Console.WriteLine("\t" + p1); } Console.WriteLine("Your answer:"); Console.WriteLine("\t" + answer); if (hasAnswer) { res = answer == p1; } if (!res) { Console.WriteLine("DOESN'T MATCH!!!!"); } else if ((endTime - startTime).TotalSeconds >= 2) { Console.WriteLine("FAIL the timeout"); res = false; } else if (hasAnswer) { Console.WriteLine("Match :-)"); } else { Console.WriteLine("OK, but is it right?"); } Console.WriteLine(""); return res; }
public Segment(int count, CompressedString value) { Count = count; Value = value; }
public static CompressedString Compress(string s) { var compressed = new CompressedString(); // longest possible repeating substring is half the length of the // string, so try that first and work down to shorter lengths for (int len = s.Length / 2; len > 0; len--) { // look for the substring at each possible index for (int i = 0; i < s.Length - len - 1; i++) { var sub = s.Substring(i, len); int count = 1; // look for repeats of the substring immediately after it. for (int j = i + len; j <= s.Length - len; j += len) { // increase the count of times the substring is found // or stop looking when it doesn't match if (sub == s.Substring(j, len)) { count++; } else { break; } } // if we found repeats then handle the substring before the // repeats, the repeast, and everything after. if (count > 1) { // if anything is before the repeats then add it to the // segments with a count of one and compress it. if (i > 0) { compressed.segments.Add(new Segment(1, Compress(s.Substring(0, i)))); } // Add the repeats to the segments with the found count // and compress it. compressed.segments.Add(new Segment(count, Compress(sub))); // if anything is after the repeats then add it to the // segments with a count of one and compress it. if (s.Length - (count * len) > i) { compressed.segments.Add(new Segment(1, Compress(s.Substring(i + (count * len))))); } // We're done compressing so break this loop and the // outer by setting len to 0. len = 0; break; } } } // If we failed to find any repeating substrings then we just have // a single uncompressible string. if (!compressed.segments.Any()) { compressed.uncompressible = s; } // Reduce the the compression for something like "2(2(ab))" to "4(ab)" compressed.Reduce(); return(compressed); }