Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.99 KB | None | 0 0
  1. using System;
  2.  
  3. namespace PrettyPrinting
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string[] modusAndLineWidth = (Console.ReadLine()).Split(' ');
  10.             int lineWidth = int.Parse(modusAndLineWidth[0]);
  11.             int modus = int.Parse(modusAndLineWidth[1]);
  12.  
  13.             string[] text = (Console.ReadLine()).Split(' ');
  14.             PrettyPrinting(text, lineWidth, modus);
  15.         }
  16.  
  17.         static void PrettyPrinting(string[] text, int lineWidth, int modus)
  18.         {
  19.             int[][] ugliness = new int[text.Length][];
  20.  
  21.             for (int i = 0; i < text.Length; i++)
  22.             {
  23.                 ugliness[i] = new int[lineWidth];
  24.  
  25.                 //Compute the ugliness of multiple words after each other.
  26.                 for (int j = 0; j < lineWidth && i + j < text.Length; j++)
  27.                 {
  28.                     //The index of last word of the line in the text array
  29.                     int jInTextArray = i + j;
  30.  
  31.                     if (j == 0)
  32.                     {
  33.                         //The ugliness of one word is the maximum line width minus the length of that word.
  34.                         ugliness[i][j] = lineWidth - text[i].Length;
  35.                     }
  36.                     //If there is a word with a '.' , '! 'or '?' than the uggliness of that line is set to 0
  37.                     else if (text[jInTextArray][text[jInTextArray].Length - 1] == '.' || text[jInTextArray][text[jInTextArray].Length - 1] == '!' ||
  38.                         text[jInTextArray][text[jInTextArray].Length - 1] == '?')
  39.                     {
  40.                         ugliness[i][j] = 0;
  41.                     }
  42.                     else
  43.                     {
  44.                         ugliness[i][j] = (ugliness[i][j - 1] - 1 - text[jInTextArray].Length) * (ugliness[i][j - 1] - 1 - text[jInTextArray].Length);
  45.                     }
  46.                 }
  47.             }
  48.  
  49.             int[] totalUgliness = new int[text.Length];
  50.             int[] resultIndices = new int[text.Length];
  51.  
  52.             //Compute the totalUgliness and the resultIndices bottum-up,
  53.             //this also means that the total ugliness of the text will be
  54.             //put on index 0 in the totalUgliness array.
  55.             for (int i = text.Length - 1; i >= 0; i--)
  56.             {
  57.                 totalUgliness[i] = ugliness[i][text.Length - 1];
  58.                 resultIndices[i] = text.Length;
  59.                 int length = 0;
  60.  
  61.                 for (int j = text.Length - 1; j >= 0 && i + j < text.Length; j--)
  62.                 {
  63.                     int jInTextArray = i + j;
  64.                     length += text[jInTextArray].Length + 1;
  65.  
  66.                     //If the total ugliness from i to the end is higher than the total ugliness
  67.                     //splitted, we can better split the line at index j.
  68.                     if (length <= lineWidth && totalUgliness[i] > ugliness[i][jInTextArray - 1] + totalUgliness[jInTextArray])
  69.                     {
  70.                         totalUgliness[i] = totalUgliness[j] + ugliness[i][jInTextArray - 1];
  71.                         resultIndices[i] = jInTextArray;
  72.                     }
  73.                 }
  74.             }
  75.  
  76.             //If the modus is 1 the string will be splitted at the indices which
  77.             //are stored in the resultIndices array.
  78.             if (modus == 1)
  79.             {
  80.                 int begin = 0;
  81.                 int end = 0;
  82.  
  83.                 while (end < text.Length)
  84.                 {
  85.                     end = resultIndices[begin];
  86.                     for (int k = begin; k < end; k++)
  87.                     {
  88.                         Console.Write(text[k] + " ");
  89.                     }
  90.                     Console.Write('\n');
  91.                     begin = end;
  92.                 }
  93.             }
  94.             //If the modus is 2 only the total ugliness of the text will be printed.
  95.             else
  96.             {
  97.                 Console.WriteLine(totalUgliness[0]);
  98.             }
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement