zoukankan      html  css  js  c++  java
  • 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;
        }

  • 相关阅读:
    可持久化+Trie || BZOJ 3261最大异或和 || Luogu P4735 最大异或和
    费用流+SPFA ||Luogu P3381【模板】最小费用最大流
    费用流+SPFA ||【模板】最小费用最大流
    Dinic二分图匹配 || Luogu P3386
    Dinic最大流 || Luogu P3376 【模板】网络最大流
    fhq_treap || BZOJ1861: [Zjoi2006]Book 书架 || Luogu P2596 [ZJOI2006]书架
    fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)
    fhq_treap || BZOJ 3224: Tyvj 1728 普通平衡树 || Luogu P3369 【模板】普通平衡树
    Manacher || BZOJ 2342: [Shoi2011]双倍回文 || Luogu P4287 [SHOI2011]双倍回文
    Manacher || P4555 [国家集训队]最长双回文串 || BZOJ 2565: 最长双回文串
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3091539.html
Copyright © 2011-2022 走看看