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.Threading.Tasks;
- using System.Text.RegularExpressions;
- using System.IO;
- namespace GenomeDecoder
- {
- class GenomeDecoder
- {
- static void Main(string[] args)
- {
- string[] input = Console.ReadLine().Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries);
- int n = int.Parse(input[0]);
- int m = int.Parse(input[1]);
- string genome = Console.ReadLine();
- StringBuilder result = new StringBuilder();
- string pattern = @"(?<number>[0-9]*)(?<letter>[A-Z]{1})";
- Match match = Regex.Match(genome, pattern);
- while (match.Success)
- {
- int number = 1; // initially one sequence of every letter
- if (char.IsDigit(match.ToString()[0])) // if match begins with digit, then we matched the group "number" and parse it
- {
- number = int.Parse(match.Groups["number"].ToString());
- }
- char[] ch = match.Groups["letter"].ToString().ToCharArray();
- result.Append(new string(ch[0],number));
- match = match.NextMatch();
- }
- int numberOfRows = result.Length / n; // if we have reminder then add 1 (e.g. 9.77 means 10 rows)
- if (result.Length%n!=0)
- {
- numberOfRows++;
- }
- //find padding
- int padding = 0;
- string num = numberOfRows.ToString();
- for (int k = 0; k < num.Length; k++)
- {
- padding++;
- }
- int i = 1;
- while(result.Length!=0)
- {
- string line = GetLineReady(result,n,m);
- Console.Write(i.ToString().PadLeft(padding));
- Console.Write(' ');
- Console.WriteLine(line);
- i++;
- }
- }
- private static string GetLineReady(StringBuilder result, int n, int m)
- {
- StringBuilder line = new StringBuilder(result.Length);
- for (int i = 0; i < n; i++)
- {
- if (i>=result.Length)
- {
- result.Clear();
- return line.ToString();
- }
- if (i!=0 && i%m == 0)
- {
- line.Append(' ');
- }
- line.Append(result[i]);
- if (i==n-1)
- {
- line = new StringBuilder(line.ToString().TrimStart().TrimEnd());
- }
- }
- result.Remove(0, n);
- return line.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement