Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace PrettyPrinting
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] modusAndLineWidth = (Console.ReadLine()).Split(' ');
- int lineWidth = int.Parse(modusAndLineWidth[0]);
- int modus = int.Parse(modusAndLineWidth[1]);
- string[] text = (Console.ReadLine()).Split(' ');
- PrettyPrinting(text, lineWidth, modus);
- }
- static void PrettyPrinting(string[] text, int lineWidth, int modus)
- {
- int[][] ugliness = new int[text.Length][];
- for (int i = 0; i < text.Length; i++)
- {
- ugliness[i] = new int[lineWidth];
- //Compute the ugliness of multiple words after each other.
- for (int j = 0; j < lineWidth && i + j < text.Length; j++)
- {
- //The index of last word of the line in the text array
- int jInTextArray = i + j;
- if (j == 0)
- {
- //The ugliness of one word is the maximum line width minus the length of that word.
- ugliness[i][j] = lineWidth - text[i].Length;
- }
- else
- {
- ugliness[i][j] = ugliness[i][j - 1] - 1 - text[jInTextArray].Length;
- }
- }
- }
- for (int i = 0; i < text.Length; i++)
- {
- for (int j = 0; j < lineWidth && i + j < text.Length; j++)
- {
- //The index of last word of the line in the text array
- int jInTextArray = i + j;
- if (ugliness[i][j] < 0)
- {
- ugliness[i][j] = int.MaxValue;
- }
- //If there is a word with a '.' , '! 'or '?' than the uggliness of that line is set to 0
- else if (text[jInTextArray][text[jInTextArray].Length - 1] == '.' || text[jInTextArray][text[jInTextArray].Length - 1] == '!' ||
- text[jInTextArray][text[jInTextArray].Length - 1] == '?')
- {
- ugliness[i][j] = 0;
- }
- else
- {
- ugliness[i][j] = (ugliness[i][j]) * (ugliness[i][j]);
- }
- }
- }
- int[] totalUgliness = new int[text.Length];
- int[] resultIndices = new int[text.Length];
- //Compute the totalUgliness and the resultIndices bottum-up,
- //this also means that the total ugliness of the text will be
- //put on index 0 in the totalUgliness array.
- totalUgliness[0] = ugliness[0][0];
- for (int i = 1; i < text.Length; i++)
- {
- totalUgliness[i] = ugliness[i][0] + totalUgliness[i - 1];
- int length = 0;
- for (int j = i; j > 0 && i - j < lineWidth; j--)
- {
- int nWords = i - j;
- length += text[j].Length + 1;
- if (length <= lineWidth && totalUgliness[j - 1] + ugliness[i - nWords][nWords] < totalUgliness[i])
- {
- totalUgliness[i] = totalUgliness[j - 1] + ugliness[i - nWords][nWords];
- }
- }
- }
- /*for (int i = 0; i < 10; i++)
- {
- Console.WriteLine(" ");
- for (int j = 0; j < 10; j++)
- {
- Console.Write(ugliness[i][j] + " ");
- }
- }*/
- //If the modus is 1 the string will be splitted at the indices which
- //are stored in the resultIndices array.
- if (modus == 1)
- {
- int begin = 0;
- int end = 0;
- while (end < text.Length)
- {
- end = resultIndices[begin];
- for (int k = begin; k < end; k++)
- {
- Console.Write(text[k] + " ");
- }
- Console.Write('\n');
- begin = end;
- }
- }
- //If the modus is 2 only the total ugliness of the text will be printed.
- else
- {
- Console.WriteLine(totalUgliness[0]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement