Advertisement
Guest User

Untitled

a guest
Apr 13th, 2019
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace Problem03PostOffice
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string[] input = Console.ReadLine().Split('|');
  12.  
  13.             var symbolAndTimes = new Dictionary<char, int>();
  14.  
  15.             string firstPartPattern = @"([#$%*&])[A-Z]+\1";
  16.             string secondPartPattern = @"(?<asciiCode>\d{2}):(?<length>\d{2})";
  17.  
  18.             string firstPart = input[0];
  19.             string secondPart = input[1];
  20.             string thirdPart = input[2];
  21.  
  22.             MatchCollection partOne = Regex.Matches(firstPart, firstPartPattern);
  23.             string capitalLetters = string.Empty;
  24.  
  25.             foreach (Match item in partOne)
  26.             {
  27.                 capitalLetters += item.Value.Remove(0, 1).Remove(item.Value.Length - 2, 1);
  28.             }
  29.  
  30.             MatchCollection partTwo = Regex.Matches(secondPart, secondPartPattern);
  31.  
  32.             foreach (Match item in partTwo)
  33.             {
  34.                 string[] asciiCodeAndLength = item.Value.Split(':');
  35.  
  36.                 int asciiCode = int.Parse(asciiCodeAndLength[0]);
  37.                 int length = int.Parse(asciiCodeAndLength[1]) + 1;
  38.  
  39.                 //Console.WriteLine("ASCII: " + asciiCode);
  40.                 //Console.WriteLine("Length: " + length);
  41.  
  42.                 if (!symbolAndTimes.ContainsKey((char)asciiCode))
  43.                 {
  44.                     symbolAndTimes.Add((char)asciiCode, length);
  45.                 }
  46.             }
  47.  
  48.             var dictionary = new Dictionary<char, int>();
  49.  
  50.             for (int i = 0; i < capitalLetters.Length; i++)
  51.             {
  52.                 foreach (char key in symbolAndTimes.Keys)
  53.                 {
  54.                     if (key == capitalLetters[i])
  55.                     {
  56.                         dictionary.Add(key, symbolAndTimes[key]);
  57.                     }
  58.                 }
  59.             }
  60.  
  61.             string[] words = thirdPart.Split(' ');
  62.  
  63.             for (int i = 0; i < words.Length; i++)
  64.             {
  65.                 if (dictionary.ContainsKey(words[i][0]))
  66.                 {
  67.                     if (words[i].Length == dictionary[words[i][0]])
  68.                     {
  69.                         Console.WriteLine(words[i]);
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement