Text Justification

2014.2.10 02:42

Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.

You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly Lcharacters.

Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.

For the last line of text, it should be left justified and no extra space is inserted between words.

For example,
words["This", "is", "an", "example", "of", "text", "justification."]
L16.

Return the formatted lines as:

[
   "This    is    an",
   "example  of text",
   "justification.  "
]

Note: Each word is guaranteed not to exceed L in length.

click to show corner cases.

Corner Cases:
  • A line other than the last line might contain only one word. What should you do in this case?
    In this case, that line should be left-justified.

Solution:

  The problem is not so difficult, but you have to carefully tackle several corner cases. Refer to the hint and think for yourself.

  Hope you can solve it with one submission only. Pity I was too careless then.

  Total time complexity is O(n), where n is the total number of words. Space complexity is O(total length of all words).

Accepted code:

  1 // 2WA, 1AC, fair enough~
  2 // #define MY_MAIN
  3 // A line other than the last line might contain only one word. What should you do in this case?
  4 // In this case, that line should be left-justified.
  5 #include <cstdio>
  6 #include <string>
  7 #include <vector>
  8 using namespace std;
  9 
 10 class Solution {
 11 public:
 12     vector<string> fullJustify(vector<string> &words, int L) {
 13         int ll, rr;
 14         int i, j;
 15         int wc = (int)words.size();
 16         int total_len;
 17         int space_len1, space_len2;
 18         int space_count1, space_count2;
 19         int len;
 20         
 21         result.clear();
 22         // 1WA here, special case is dealt with here.
 23         if (wc == 1) {
 24             s[0] = 0;
 25             sprintf(s, "%s", words[0].c_str());
 26             len = (int)words[0].length();
 27             while (len < L) {
 28                 s[len++] = ' ';
 29             }
 30             s[len] = 0;
 31             result.push_back(string(s));
 32             return result;
 33         }
 34         
 35         ll = 0;
 36         while (ll < wc) {
 37             rr = ll + 1;
 38             total_len = (int)words[ll].length();
 39             while (rr < wc && total_len + 1 + (int)words[rr].length() <= L) {
 40                 total_len += ((int)words[rr].length() + 1);
 41                 ++rr;
 42             }
 43             
 44             if (rr < wc) {
 45                 // the total length exceeds L, more lines to be expected
 46                 // fully-justified
 47                 total_len -= (rr - ll - 1);
 48                 if (rr - ll > 1) {
 49                     space_count1 = (L - total_len) % (rr - ll - 1);
 50                     space_count2 = (rr - ll - 1) - space_count1;
 51                     space_len1 = (L - total_len) / (rr - ll - 1) + 1;
 52                     space_len2 = (L - total_len) / (rr - ll - 1);
 53                     s[0] = 0;
 54                     sprintf(s, "%s", words[ll].c_str());
 55                     len = (int)words[ll].length();
 56                     for (i = 0; i < space_count1; ++i) {
 57                         for (j = 0; j < space_len1; ++j) {
 58                             s[len++] = ' ';
 59                         }
 60                         s[len] = 0;
 61                         sprintf(s + len, "%s", words[ll + 1 + i].c_str());
 62                         len += words[ll + 1 + i].length();
 63                     }
 64                     for (i = 0; i < space_count2; ++i) {
 65                         for (j = 0; j < space_len2; ++j) {
 66                             s[len++] = ' ';
 67                         }
 68                         s[len] = 0;
 69                         sprintf(s + len, "%s", words[ll + 1 + space_count1 + i].c_str());
 70                         len += words[ll + 1 + space_count1 + i].length();
 71                     }
 72                 } else {
 73                     // only one word on this line, and not the last line
 74                     // left-justified
 75                     s[0] = 0;
 76                     sprintf(s, "%s", words[ll].c_str());
 77                     for (i = (int)words[ll].length(); i < L; ++i) {
 78                         s[i] = ' ';
 79                     }
 80                     s[i] = 0;
 81                 }
 82             } else {
 83                 // the last line is reached
 84                 // left-justified
 85                 s[0] = 0;
 86                 sprintf(s, "%s", words[ll].c_str());
 87                 len = (int)words[ll].length();
 88                 for (i = ll + 1; i < rr; ++i) {
 89                     s[len++] = ' ';
 90                     s[len] = 0;
 91                     sprintf(s + len, "%s", words[i].c_str());
 92                     len += (int)words[i].length();
 93                 }
 94                 // 1WA here, the last line must be filled with spaces.
 95                 while (len < L) {
 96                     s[len++] = ' ';
 97                 }
 98                 s[len] = 0;
 99             }
100             result.push_back(string(s));
101             ll = rr;
102         }
103         
104         return result;
105     }
106 private:
107     char s[10000];
108     vector<string> result;
109 };
110 
111 #ifdef MY_MAIN
112 int main()
113 {
114     int i;
115     int n;
116     vector<string> words;
117     int L;
118     char s[1000];
119     Solution solution;
120     vector<string> result;
121     
122     while (scanf("%d", &n) == 1 && n) {
123         scanf("%d", &L);
124         words.clear();
125         for (i = 0; i < n; ++i) {
126             scanf("%s", s);
127             words.push_back(string(s));
128         }
129         result = solution.fullJustify(words, L);
130         for (i = 0; i < (int)result.size(); ++i) {
131             printf("%d: %s\n", i + 1, result[i].c_str());
132         }
133     }
134     
135     return 0;
136 }
137 #endif

 

 posted on 2014-02-10 02:51  zhuli19901106  阅读(359)  评论(0编辑  收藏  举报