Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.53 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.                     else
  37.                     {
  38.                         ugliness[i][j] = ugliness[i][j - 1] - 1 - text[jInTextArray].Length;
  39.                     }
  40.                 }
  41.             }
  42.  
  43.             for (int i = 0; i < text.Length; i++)
  44.             {
  45.                 for (int j = 0; j < lineWidth && i + j < text.Length; j++)
  46.                 {
  47.                     //The index of last word of the line in the text array
  48.                     int jInTextArray = i + j;
  49.  
  50.                     if (ugliness[i][j] < 0)
  51.                     {
  52.                         ugliness[i][j] = int.MaxValue;
  53.                     }
  54.                     //If there is a word with a '.' , '! 'or '?' than the uggliness of that line is set to 0
  55.                     else if (text[jInTextArray][text[jInTextArray].Length - 1] == '.' || text[jInTextArray][text[jInTextArray].Length - 1] == '!' ||
  56.                         text[jInTextArray][text[jInTextArray].Length - 1] == '?')
  57.                     {
  58.                         ugliness[i][j] = 0;
  59.                     }
  60.                     else
  61.                     {
  62.                         ugliness[i][j] = (ugliness[i][j]) * (ugliness[i][j]);
  63.                     }
  64.  
  65.                 }
  66.             }
  67.  
  68.             int[] totalUgliness = new int[text.Length];
  69.             int[] resultIndices = new int[text.Length];
  70.  
  71.             //Compute the totalUgliness and the resultIndices bottum-up,
  72.             //this also means that the total ugliness of the text will be
  73.             //put on index 0 in the totalUgliness array.
  74.             totalUgliness[0] = ugliness[0][0];
  75.             for (int i = 1; i < text.Length; i++)
  76.             {
  77.                 totalUgliness[i] = ugliness[i][0] + totalUgliness[i - 1];
  78.                 int length = 0;
  79.                 for (int j = i; j > 0 && i - j < lineWidth; j--)
  80.                 {
  81.                     int nWords = i - j;
  82.                     length += text[j].Length + 1;
  83.                     if (length <= lineWidth && totalUgliness[j - 1] + ugliness[i - nWords][nWords] < totalUgliness[i])
  84.                     {
  85.                         totalUgliness[i] = totalUgliness[j - 1] + ugliness[i - nWords][nWords];
  86.                     }
  87.                 }
  88.             }
  89.  
  90.             /*for (int i = 0; i < 10; i++)
  91.             {
  92.                 Console.WriteLine(" ");
  93.                 for (int j = 0; j < 10; j++)
  94.                 {
  95.                     Console.Write(ugliness[i][j] + " ");
  96.                 }
  97.             }*/
  98.  
  99.             //If the modus is 1 the string will be splitted at the indices which
  100.             //are stored in the resultIndices array.
  101.             if (modus == 1)
  102.             {
  103.                 int begin = 0;
  104.                 int end = 0;
  105.  
  106.                 while (end < text.Length)
  107.                 {
  108.                     end = resultIndices[begin];
  109.                     for (int k = begin; k < end; k++)
  110.                     {
  111.                         Console.Write(text[k] + " ");
  112.                     }
  113.                     Console.Write('\n');
  114.                     begin = end;
  115.                 }
  116.             }
  117.             //If the modus is 2 only the total ugliness of the text will be printed.
  118.             else
  119.             {
  120.                 Console.WriteLine(totalUgliness[0]);
  121.             }
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement