Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace _03.Jedi_Code_X
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- StringBuilder text = new StringBuilder();
- for (int i = 0; i < n; i++)
- {
- var input = Console.ReadLine();
- text.Append(input);
- }
- var finalText = text.ToString();
- var firstPattern = Console.ReadLine().Trim();
- var secondPattern = Console.ReadLine().Trim();
- MatchCollection namesMatchCollection =
- Regex.Matches(finalText, firstPattern + "([A-Za-z]{" + firstPattern.Length + "})[^a-zA-Z]*");
- MatchCollection messagesMatchCollection =
- Regex.Matches(finalText, secondPattern + "([A-Za-z0-9]{" + secondPattern.Length + "})[^a-zA-Z0-9]*");
- Dictionary<string, string> jediAndMessages = new Dictionary<string, string>();
- Assign(jediAndMessages, namesMatchCollection, messagesMatchCollection);
- foreach (var pair in jediAndMessages)
- {
- Console.WriteLine($"{pair.Key} - {pair.Value}");
- }
- }
- public static void Assign(Dictionary<string, string> dict, MatchCollection names, MatchCollection messages)
- {
- var indexes = Console.ReadLine().Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
- Queue<int> indexesQueue = new Queue<int>();
- Queue<string> namesQueue = new Queue<string>();
- for (int i = 0; i < indexes.Length; i++)
- {
- indexesQueue.Enqueue(int.Parse(indexes[i]));
- }
- for (int i = 0; i < names.Count; i++)
- {
- namesQueue.Enqueue(names[i].Groups[1].Value);
- }
- while (true)
- {
- var name = namesQueue.Dequeue();
- if (indexesQueue.Count == 0)
- {
- dict.Add(name, string.Empty);
- }
- else
- {
- while (true)
- {
- if (indexesQueue.Count == 0)
- {
- dict.Add(name, string.Empty);
- break;
- }
- int index = indexesQueue.Dequeue();
- if (IsInRange(messages, index))
- {
- dict.Add(name, messages[index - 1].Groups[1].Value);
- break;
- }
- else
- {
- continue;
- }
- }
- }
- if (dict.Count == names.Count)
- {
- break;
- }
- }
- }
- public static bool IsInRange(MatchCollection mess, int index)
- {
- if (index - 1 >= 0 && index - 1 < mess.Count)
- {
- bool range = true;
- return range;
- }
- else
- {
- bool range = false;
- return range;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement