Guest User

03. Jedi Code-X

a guest
Jun 22nd, 2017
84
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _03.Jedi_Code_X
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. int n = int.Parse(Console.ReadLine());
  15. StringBuilder text = new StringBuilder();
  16.  
  17. for (int i = 0; i < n; i++)
  18. {
  19. var input = Console.ReadLine();
  20. text.Append(input);
  21. }
  22.  
  23. var finalText = text.ToString();
  24.  
  25. var firstPattern = Console.ReadLine().Trim();
  26. var secondPattern = Console.ReadLine().Trim();
  27.  
  28. MatchCollection namesMatchCollection =
  29. Regex.Matches(finalText, firstPattern + "([A-Za-z]{" + firstPattern.Length + "})[^a-zA-Z]*");
  30. MatchCollection messagesMatchCollection =
  31. Regex.Matches(finalText, secondPattern + "([A-Za-z0-9]{" + secondPattern.Length + "})[^a-zA-Z0-9]*");
  32.  
  33.  
  34. Dictionary<string, string> jediAndMessages = new Dictionary<string, string>();
  35. Assign(jediAndMessages, namesMatchCollection, messagesMatchCollection);
  36.  
  37. foreach (var pair in jediAndMessages)
  38. {
  39. Console.WriteLine($"{pair.Key} - {pair.Value}");
  40. }
  41.  
  42. }
  43.  
  44. public static void Assign(Dictionary<string, string> dict, MatchCollection names, MatchCollection messages)
  45. {
  46. var indexes = Console.ReadLine().Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
  47. Queue<int> indexesQueue = new Queue<int>();
  48. Queue<string> namesQueue = new Queue<string>();
  49. for (int i = 0; i < indexes.Length; i++)
  50. {
  51. indexesQueue.Enqueue(int.Parse(indexes[i]));
  52. }
  53. for (int i = 0; i < names.Count; i++)
  54. {
  55. namesQueue.Enqueue(names[i].Groups[1].Value);
  56. }
  57. while (true)
  58. {
  59. var name = namesQueue.Dequeue();
  60.  
  61. if (indexesQueue.Count == 0)
  62. {
  63. dict.Add(name, string.Empty);
  64. }
  65. else
  66. {
  67. while (true)
  68. {
  69. if (indexesQueue.Count == 0)
  70. {
  71. dict.Add(name, string.Empty);
  72. break;
  73. }
  74. int index = indexesQueue.Dequeue();
  75. if (IsInRange(messages, index))
  76. {
  77. dict.Add(name, messages[index - 1].Groups[1].Value);
  78. break;
  79. }
  80. else
  81. {
  82. continue;
  83. }
  84. }
  85.  
  86. }
  87. if (dict.Count == names.Count)
  88. {
  89. break;
  90. }
  91. }
  92. }
  93.  
  94. public static bool IsInRange(MatchCollection mess, int index)
  95. {
  96.  
  97. if (index - 1 >= 0 && index - 1 < mess.Count)
  98. {
  99. bool range = true;
  100. return range;
  101. }
  102. else
  103. {
  104. bool range = false;
  105. return range;
  106. }
  107. }
  108. }
  109. }
RAW Paste Data