Guest User

code by NightmareZ

a guest
Jun 19th, 2011
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. namespace App
  7. {
  8.     class Program
  9.     {
  10.         static IEnumerable<string> ToLines(IEnumerable<object> list, int spaces = 0)
  11.         {
  12.             foreach (object obj in list)
  13.             {
  14.                 string pre = string.Join(string.Empty, Enumerable.Repeat(" ", spaces));
  15.  
  16.                 if (obj is int)
  17.                     yield return pre + obj;
  18.                 else
  19.                 {
  20.                     var kvp = (KeyValuePair<int, List<object>>)obj;
  21.                     yield return string.Format("{0}repeat {1}:", pre, kvp.Key);
  22.                     foreach (string line in ToLines(kvp.Value, spaces + 4))
  23.                         yield return line;
  24.                 }
  25.             }
  26.         }
  27.  
  28.         static bool Compare(object obj1, object obj2)
  29.         {
  30.             if (obj1 is int && !(obj2 is int)) return false;
  31.             if (obj2 is int && !(obj1 is int)) return false;
  32.  
  33.             if (obj1 is int && obj2 is int)
  34.                 return (int) obj1 == (int) obj2;
  35.  
  36.             var kvp1 = (KeyValuePair<int, List<object>>)obj1;
  37.             var kvp2 = (KeyValuePair<int, List<object>>)obj2;
  38.  
  39.             if (kvp1.Key != kvp2.Key) return false;
  40.             if (kvp1.Value.Count != kvp2.Value.Count) return false;
  41.  
  42.             return !kvp1.Value.Where((t, i) => !Compare(t, kvp2.Value[i])).Any();
  43.         }
  44.  
  45.         static IEnumerable<object> Process(IEnumerable<object> source, int depth)
  46.         {
  47.             var list = new List<object>(source);
  48.             var result = new List<object>(list.Count);
  49.             bool sublistsCreated = false;
  50.  
  51.             for (int startIdx = 0; startIdx < list.Count; ++startIdx)
  52.             {
  53.                 var counts = Enumerable.Repeat(0, depth).ToList();
  54.  
  55.                 for (int rangeLen = 1;
  56.                      rangeLen <= depth && startIdx + rangeLen <= list.Count;
  57.                      ++rangeLen)
  58.                 {
  59.                     int count = 0;
  60.                     bool eq;
  61.  
  62.                     do
  63.                     {
  64.                         ++count;
  65.                         eq = true;
  66.  
  67.                         for (int i = 0; i < rangeLen; ++i)
  68.                         {
  69.                             int idx0 = startIdx + i;
  70.                             int idx1 = startIdx + rangeLen*count + i;
  71.  
  72.                             if (idx1 >= list.Count)
  73.                             {
  74.                                 eq = false;
  75.                                 break;
  76.                             }
  77.  
  78.                             if (!Compare(list[idx0], list[idx1]))
  79.                             {
  80.                                 eq = false;
  81.                                 break;
  82.                             }
  83.                         }
  84.                     } while (eq);
  85.  
  86.                     counts[rangeLen - 1] = count;
  87.                 }
  88.  
  89.                 int max = counts.Max();
  90.                 int maxDepth = counts.IndexOf(max) + 1;
  91.  
  92.                 if (max == 1)
  93.                     result.Add(list[startIdx]);
  94.                 else
  95.                 {
  96.                     var subList = new List<object>(maxDepth);
  97.  
  98.                     for (int i = 0; i < maxDepth; ++i)
  99.                         subList.Add(list[startIdx + i]);
  100.  
  101.                     subList = Process(subList, depth).ToList();
  102.                     result.Add(new KeyValuePair<int, List<object>>(max, subList));
  103.                     sublistsCreated = true;
  104.                     startIdx += maxDepth * max - 1;
  105.                 }
  106.             }
  107.  
  108.             if (sublistsCreated)
  109.                 result = Process(result, depth).ToList();
  110.  
  111.             return result;
  112.         }
  113.  
  114.         static void Main()
  115.         {
  116.             const string inputFileName = @"input.txt";
  117.             const string outputFileName = @"output.txt";
  118.  
  119.             var source = (from line in File.ReadAllLines(inputFileName)
  120.                           select (object)int.Parse(line.Trim())).ToList();
  121.  
  122.             var result = Process(source, source.Count);
  123.             File.WriteAllLines(outputFileName, ToLines(result));
  124.  
  125.             foreach (var line in ToLines(result))
  126.                 Console.WriteLine(line);
  127.             Console.ReadKey();
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment