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[text.Length];
- //The ugliness of one word is the maximum line width minus the length of that word.
- ugliness[i][i] = lineWidth - text[i].Length;
- //Compute the ugliness of multiple words after each other.
- for (int j = i + 1; j < text.Length + 1; j++)
- {
- if (j != text.Length)
- {
- //The ugliness of multiple words is the uglines of the all the words without the last word
- //minus the space of length 1 and the length of the last word.
- ugliness[i][j] = ugliness[i][j - 1] - 1 - text[j].Length;
- }
- //The ugliness has to be equal or greater than zero because otherwise the sequence of
- //words is to large and the ugliness has to be set to a very large value to indicate that.
- if (ugliness[i][j - 1] >= 0)
- {
- //If there is a word with a '.' , '! 'or '?' than the uggliness of that line is set to 0
- if (text[j - 1][text[j - 1].Length - 1] == '.' || text[j - 1][text[j - 1].Length - 1] == '!' ||
- text[j - 1][text[j - 1].Length - 1] == '?')
- {
- ugliness[i][j - 1] = 0;
- }
- //Otherwise the uggliness of the line is the ugliness to the power of 2 because of
- //the formula in the assignment.
- else
- {
- ugliness[i][j - 1] = ugliness[i][j - 1] * ugliness[i][j - 1];
- }
- }
- else
- {
- ugliness[i][j - 1] = int.MaxValue;
- }
- }
- }
- 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.
- for (int i = text.Length - 1; i >= 0; i--)
- {
- totalUgliness[i] = ugliness[i][text.Length - 1];
- resultIndices[i] = text.Length;
- for (int j = text.Length - 1; j > i; j--)
- {
- //If the total ugliness from i to the end is higher than the total ugliness
- //splitted, we can better split the line at index j.
- if (ugliness[i][j - 1] != int.MaxValue && totalUgliness[i] > ugliness[i][j - 1] + totalUgliness[j])
- {
- totalUgliness[i] = totalUgliness[j] + ugliness[i][ j - 1];
- resultIndices[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