Advertisement
d_brezoev

GenomeDecoder80

Jan 12th, 2014
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Text.RegularExpressions;
  7. using System.IO;
  8.  
  9. namespace GenomeDecoder
  10. {
  11.     class GenomeDecoder
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             string[] input = Console.ReadLine().Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries);
  16.             int n = int.Parse(input[0]);
  17.             int m = int.Parse(input[1]);
  18.             string genome = Console.ReadLine();
  19.             StringBuilder result = new StringBuilder();
  20.             string pattern = @"(?<number>[0-9]*)(?<letter>[A-Z]{1})";
  21.             Match match = Regex.Match(genome, pattern);
  22.             while (match.Success)
  23.             {
  24.                 int number = 1; // initially one sequence of every letter
  25.                 if (char.IsDigit(match.ToString()[0])) // if match begins with digit, then we matched the group "number" and parse it
  26.                 {
  27.                     number = int.Parse(match.Groups["number"].ToString());
  28.                 }
  29.                 char[] ch = match.Groups["letter"].ToString().ToCharArray();
  30.                 result.Append(new string(ch[0],number));
  31.                 match = match.NextMatch();
  32.             }
  33.             int numberOfRows = result.Length / n; // if we have reminder then add 1 (e.g. 9.77 means 10 rows)
  34.             if (result.Length%n!=0)
  35.             {
  36.                 numberOfRows++;
  37.             }
  38.  
  39.             //find padding
  40.             int padding = 0;
  41.             string num = numberOfRows.ToString();
  42.             for (int k = 0; k < num.Length; k++)
  43.             {
  44.                 padding++;
  45.             }
  46.             int i = 1;            
  47.             while(result.Length!=0)
  48.             {
  49.                 string line = GetLineReady(result,n,m);
  50.                 Console.Write(i.ToString().PadLeft(padding));              
  51.                 Console.Write(' ');
  52.                 Console.WriteLine(line);              
  53.                 i++;
  54.             }        
  55.         }
  56.  
  57.         private static string GetLineReady(StringBuilder result, int n, int m)
  58.         {
  59.             StringBuilder line = new StringBuilder(result.Length);
  60.             for (int i = 0; i < n; i++)
  61.             {
  62.                 if (i>=result.Length)
  63.                 {
  64.                     result.Clear();
  65.                     return line.ToString();                    
  66.                 }  
  67.                 if (i!=0 && i%m == 0)
  68.                 {
  69.                     line.Append(' ');
  70.                 }
  71.                 line.Append(result[i]);
  72.                 if (i==n-1)
  73.                 {
  74.                     line = new StringBuilder(line.ToString().TrimStart().TrimEnd());
  75.                 }                
  76.             }
  77.             result.Remove(0, n);
  78.             return line.ToString();
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement