YavorGrancharov

Cubic_Messages(helped)

Sep 1st, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace Cubic_Messages
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string input = Console.ReadLine();
  14.  
  15.             string validsms = @"^([0-9]+)([a-zA-Z]+)([^a-zA-Z]*)$";
  16.  
  17.             Regex validMessage = new Regex(validsms);
  18.  
  19.             while (input != "Over!")
  20.             {
  21.                 int len = int.Parse(Console.ReadLine());
  22.  
  23.                 if (!validMessage.IsMatch(input))
  24.                 {
  25.                     input = Console.ReadLine();
  26.                     continue;
  27.                 }
  28.  
  29.                 Match match = validMessage.Match(input);
  30.                 string smsLen = match.Groups[2].Value;
  31.                 if (smsLen.Length != len)
  32.                 {
  33.                     input = Console.ReadLine();
  34.                     continue;
  35.                 }
  36.  
  37.                 string group1 = match.Groups[1].Value;
  38.                 string group2 = match.Groups[3].Value;
  39.                 string nums = match.Groups[1].Value + match.Groups[3].Value;
  40.  
  41.                 List<int> indexes = new List<int>();
  42.                 for (int i = 0; i < nums.Length; i++)
  43.                 {
  44.                     int num = 0;
  45.                     if (int.TryParse(nums[i].ToString(), out num))
  46.                     {
  47.                         indexes.Add(num);
  48.                     }
  49.                 }
  50.  
  51.                 StringBuilder result = new StringBuilder();
  52.                 foreach (int index in indexes)
  53.                 {
  54.                     if (index < len && index >= 0)
  55.                     {
  56.                         result.Append(smsLen[index]);
  57.                     }
  58.                     else
  59.                     {
  60.                         result.Append(" ");
  61.                     }
  62.                 }
  63.                 Console.WriteLine("{0} == {1}", smsLen, result.ToString());
  64.  
  65.                 input = Console.ReadLine();
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment