Text Justification

题目:

Given an array of words and a length L, format the text such that each line has exactlyL 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.

分析:先计算出每行应该放几个单词,对于只有一个单词的情况比较好处理,对于有好几个单词,得计算每个单词后得放几个空格。

代码如下:

        vector<string> fullJustify(vector<string> &words, int L) {
        vector<string> result;
        for(int i=0;i<words.size();)
        {
            int tmpl=0,j=i;//templ表示当前字符串长度
            for(;j<words.size();j++)
            {
                tmpl+=words[j].length();
                if(tmpl<L)
                {
                    tmpl+=1;
                }
                else
                {
                    if(tmpl==L)
                        break;
                    else
                    {
                        tmpl-=words[j].length();
                        tmpl-=1;
                        j--;
                        break;
                    }
                }
            }
            if(j==words.size())
            {
                string tmps;
                int t=i;
                for(;t<j;t++)
                {
                    tmps+=words[t];
                    if(tmps.length()<L)
                    {
                        tmps+=" ";
                    }
                }
                while(tmps.length()<L)
                {
                    tmps+=" ";
                }
                result.push_back(tmps);
                break;
            }
            else
            {
                string tmps;
                if(i==j)
                {
                    tmps+=words[i];
                    while(tmps.length()<L)
                    {
                        tmps+=" ";
                    }
                    result.push_back(tmps);
                }
                else
                {
                    int t=i;
                    int * array=new int [j-t];
                    int len;
                    int mod = (L-tmpl+j-t)%(j-t);
                    int modresult=(L-tmpl+j-t)/(j-t);
                    for(int k=0;k<j-t;k++)
                    {
                        if(mod>0)
                        {
                            array[k]=modresult+1;
                            mod--;
                        }
                        else
                        {
                            array[k]=modresult;
                        }
                    }
                    for(;t<=j;t++)
                    {
                       tmps+=words[t];
                       if(tmps.length()<L)
                       {
                           while(array[t-i]!=0)
                           {
                               tmps+=" ";
                               array[t-i]--;
                           }
                       }
                    }
                    delete []array;
                    result.push_back(tmps);
                }             
            }
            i=j+1;
        }
        return result;
    }

原文地址:https://www.cnblogs.com/javawebsoa/p/3091539.html