Advertisement
cap7ainjack

Untitled

Jun 18th, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. namespace _3_JediCodeX
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8.  
  9. class Program
  10. {
  11. static void Main()
  12. {
  13. int n = int.Parse(Console.ReadLine());
  14. StringBuilder builder = new StringBuilder();
  15. IDictionary<int, string> messageDictionary = new Dictionary<int, string>();
  16.  
  17. for (int i = 0; i < n; i++)
  18. {
  19. builder.AppendLine(Console.ReadLine().Trim());
  20. }
  21.  
  22. string namePatternInput = Console.ReadLine().Trim();
  23. string msgPatternInput = Console.ReadLine().Trim();
  24.  
  25. string namePattern =
  26. $"{Regex.Escape(namePatternInput)}([a-zA-Z]{{{namePatternInput.Length}}})(?![a-zA-Z])";
  27.  
  28. Queue<string> namesQueue = new Queue<string>();
  29. Regex regex = new Regex(namePattern);
  30. MatchCollection namesCollection = regex.Matches(builder.ToString());
  31.  
  32. foreach (Match nameMatch in namesCollection)
  33. {
  34. namesQueue.Enqueue(nameMatch.Groups[1].Value);
  35. }
  36.  
  37. GetTheMessages(builder, msgPatternInput, messageDictionary);
  38.  
  39.  
  40. int[] indexes =
  41. Console.ReadLine()
  42. .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  43. .Select(int.Parse)
  44. .ToArray();
  45.  
  46. List<string> result = new List<string>();
  47.  
  48. foreach (var index in indexes)
  49. {
  50. if (messageDictionary.ContainsKey(index) && namesQueue.Count > 0)
  51. {
  52. result.Add(namesQueue.Dequeue() + " - " + messageDictionary[index]);
  53. }
  54. }
  55.  
  56. Console.WriteLine(string.Join(Environment.NewLine, result));
  57. }
  58.  
  59. private static void GetTheMessages(StringBuilder builder, string msgPatternInput, IDictionary<int, string> messageDictionary)
  60. {
  61. string messagePattern = $"{Regex.Escape(msgPatternInput)}([a-zA-Z0-9]{{{msgPatternInput.Length}}})(?![a-zA-Z0-9])";
  62.  
  63. Regex regex = new Regex(messagePattern);
  64. MatchCollection messageCollection = regex.Matches(builder.ToString());
  65. int indexCounter = 1;
  66.  
  67. foreach (Match match in messageCollection)
  68. {
  69. if (!messageDictionary.ContainsKey(indexCounter))
  70. {
  71. messageDictionary[indexCounter] = match.Groups[1].Value;
  72. indexCounter++;
  73. }
  74. }
  75. }
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement