Thursday, April 18, 2013

Text Justification(C++ code)

LeetCode Text Justification, Apr 3 '12
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 L characters.
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."]
L: 16.
Return the formatted lines as:
[
   "This    is    an",
   "example  of text",
   "justification.  "
]
Note: Each word is guaranteed not to exceed L in length.


    
  1. //fill each line  
  2.   
  3. void fill(vector<string> &words, int start, int end, int num, vector<string> &res){  
  4.   
  5.   string line;  
  6.   
  7.   if(end == words.size() -1 || start == end){  
  8.   
  9.     while(start < end){line.append(words[start++]); line.push_back(' ');}  
  10.   
  11.     line.append(words[end]);  
  12.   
  13.     while(num-- > 0) line.push_back(' ');  
  14.   
  15.     res.push_back(line); return;  
  16.   }  
  17.   
  18.  int spacen = num/(end - start) + 1, extra = num%(end - start);  
  19.   
  20.  while(start < end){  
  21.   
  22.    int pad = spacen;  
  23.   
  24.    if(extra-- > 0) pad += 1;  
  25.   
  26.    line.append(words[start]);  
  27.   
  28.    while(pad-- > 0) line.push_back(' ');  
  29.   
  30.    start++;  
  31.   
  32.   }  
  33.   
  34.   line.append(words[end]);  
  35.   
  36.   res.push_back(line);  
  37.   
  38. }  
  39.   
  40. //separate each line  
  41.   
  42. void fullJustify(vector<string> &words, int start, int L, vector<string> &res){  
  43.   
  44.    if(start >= words.size()) return;  
  45.   
  46.    int len = 0, cur = start, end;  
  47.   
  48.    while(cur < words.size()){  
  49.   
  50.       if(len + words[cur].length() == L) {len = L;  end = cur; cur++; break;}  
  51.   
  52.       if(len + words[cur].length() < L){  
  53.   
  54.              if(cur == words.size() - 1){end = words.size() - 1;len += words[cur].length();}  
  55.   
  56.              else len += words[cur].length() + 1;  
  57.   
  58.              cur++;  
  59.   
  60.              }  
  61.   
  62.       else {end = cur - 1; len -= 1; break;}  
  63.   
  64.     }  
  65.   
  66.    fill(words,start, end, L - len, res);  
  67.   
  68.    fullJustify(words, cur, L, res);  
  69.   
  70. }  
  71.   
  72. //main function  
  73.   
  74. vector<string> fullJustify(vector<string> &words, int L) {  
  75.   
  76.         vector<string> res;  
  77.   
  78.         if(words.empty()) {  
  79.   
  80.           string str(L,' ');  
  81.   
  82.           res.push_back(str);  
  83.   
  84.         }  
  85.   
  86.         fullJustify(words, 0, L, res);  
  87.   
  88. return res;  
  89.   
  90. }   

No comments:

Post a Comment