Advertisement
Guest User

Untitled

a guest
Feb 11th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace VariableLengthCodes
  8. {
  9.     class VariableLengthCodes
  10.     {
  11.         static void Main()
  12.         {
  13.             //new KeyValuePair<char, string>(key, value)
  14.             string[] firstLine = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  15.  
  16.             string encodedText = "";
  17.  
  18.             for (int i = 0; i < firstLine.Length; i++)
  19.             {
  20.                 encodedText += Convert.ToString(int.Parse(firstLine[i]), 2).PadLeft(8, '0');
  21.             }
  22.  
  23.             string[] encodedTextAsArray = encodedText.Split(new char[] { '0' }, StringSplitOptions.RemoveEmptyEntries);
  24.  
  25.             int lines = int.Parse(Console.ReadLine());
  26.  
  27.             var keyPairValue = new List<KeyValuePair<char, string>>();
  28.  
  29.             for (int i = 0; i < lines; i++)
  30.             {
  31.                 string pair = Console.ReadLine();
  32.  
  33.                 var digits = new List<int>();
  34.  
  35.                 for (int j = pair.Length - 1; j >= 0; j--)
  36.                 {
  37.                     if (!char.IsDigit(pair[j]))
  38.                     {
  39.                         break;
  40.                     }
  41.  
  42.                     digits.Add(int.Parse(pair[j].ToString()));
  43.                 }
  44.  
  45.                 digits.Reverse();
  46.                 int numberCode = 0;
  47.  
  48.                 for (int j = 0; j < digits.Count; j++)
  49.                 {
  50.                     numberCode = numberCode * 10 + digits[j];
  51.                 }
  52.  
  53.                 keyPairValue.Add(new KeyValuePair<char, string>(pair[0], new string('1', numberCode)));
  54.             }
  55.  
  56.             StringBuilder result = new StringBuilder();
  57.  
  58.             for (int i = 0; i < encodedTextAsArray.Length; i++)
  59.             {
  60.                 foreach (var pair in keyPairValue)
  61.                 {
  62.                     if (pair.Value == encodedTextAsArray[i])
  63.                     {
  64.                         result.Append(pair.Key);
  65.                     }
  66.                 }
  67.             }
  68.  
  69.             Console.WriteLine(result.ToString());
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement