Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.47 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[text.Length];
  24.  
  25.                 //The ugliness of one word is the maximum line width minus the length of that word.
  26.                 ugliness[i][i] = lineWidth - text[i].Length;
  27.  
  28.                 //Compute the ugliness of multiple words after each other.
  29.                 for (int j = i + 1; j < text.Length + 1; j++)
  30.                 {
  31.                     if (j != text.Length)
  32.                     {
  33.                         //The ugliness of multiple words is the uglines of the all the words without the last word
  34.                         //minus the space of length 1 and the length of the last word.
  35.                         ugliness[i][j] = ugliness[i][j - 1] - 1 - text[j].Length;
  36.                     }
  37.  
  38.                     //The ugliness has to be equal or greater than zero because otherwise the sequence of
  39.                     //words is to large and the ugliness has to be set to a very large value to indicate that.
  40.                     if (ugliness[i][j - 1] >= 0)
  41.                     {
  42.                         //If there is a word with a '.' , '! 'or '?' than the uggliness of that line is set to 0
  43.                         if (text[j - 1][text[j - 1].Length - 1] == '.' || text[j - 1][text[j - 1].Length - 1] == '!' ||
  44.                             text[j - 1][text[j - 1].Length - 1] == '?')
  45.                         {
  46.                             ugliness[i][j - 1] = 0;
  47.                         }
  48.                         //Otherwise the uggliness of the line is the ugliness to the power of 2 because of
  49.                         //the formula in the assignment.
  50.                         else
  51.                         {
  52.                             ugliness[i][j - 1] = ugliness[i][j - 1] * ugliness[i][j - 1];
  53.                         }
  54.                     }
  55.                     else
  56.                     {
  57.                         ugliness[i][j - 1] = int.MaxValue;
  58.                     }
  59.  
  60.                 }
  61.             }
  62.  
  63.             int[] totalUgliness = new int[text.Length];
  64.             int[] resultIndices = new int[text.Length];
  65.  
  66.             //Compute the totalUgliness and the resultIndices bottum-up,
  67.             //this also means that the total ugliness of the text will be
  68.             //put on index 0 in the totalUgliness array.
  69.             for (int i = text.Length - 1; i >= 0; i--)
  70.             {
  71.                 totalUgliness[i] = ugliness[i][text.Length - 1];
  72.                 resultIndices[i] = text.Length;
  73.  
  74.                 for (int j = text.Length - 1; j > i; j--)
  75.                 {
  76.                     //If the total ugliness from i to the end is higher than the total ugliness
  77.                     //splitted, we can better split the line at index j.
  78.                     if (ugliness[i][j - 1] != int.MaxValue && totalUgliness[i] > ugliness[i][j - 1] + totalUgliness[j])
  79.                     {
  80.                         totalUgliness[i] = totalUgliness[j] + ugliness[i][ j - 1];
  81.                         resultIndices[i] = j;
  82.                     }
  83.                 }
  84.             }
  85.  
  86.             //If the modus is 1 the string will be splitted at the indices which
  87.             //are stored in the resultIndices array.
  88.             if (modus == 1)
  89.             {
  90.                 int begin = 0;
  91.                 int end = 0;
  92.  
  93.                 while (end < text.Length)
  94.                 {
  95.                     end = resultIndices[begin];
  96.                     for (int k = begin; k < end; k++)
  97.                     {
  98.                         Console.Write(text[k] + " ");
  99.                     }
  100.                     Console.Write('\n');
  101.                     begin = end;
  102.                 }
  103.             }
  104.             //If the modus is 2 only the total ugliness of the text will be printed.
  105.             else
  106.             {
  107.                 Console.WriteLine(totalUgliness[0]);
  108.             }
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement