Book Borders (Gym - 101480B)解题报告

本文探讨了一种用于书籍排版的模拟算法,该算法通过设置最大行长度来填充每行文字,确保每行不超过预设字符数。文章详细介绍了如何计算不同最大行长度下,每行首词构成的引导句子的总长度,包括模拟过程中的关键步骤和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目全文:

Problem B: Book Borders Time limit: 2 s Memory limit: 512 MiB A book is being typeset using a fixed width font and a simple greedy algorithm to fill each line. The book contents is just a sequence of words, where each word contains one or more characters. Before typesetting, we choose a maximum line length and denote this value with m. Each line can be at most m characters long including the space characters between the words. The typesetting algorithm simply processes words one by one and prints each word with exactly one space character between two consecutive words on the same line. If printing the word on the current line would exceed the maximum line length m, a new line is started instead. |its.a.long...| |way.to.the...| |top.if.you...| |wanna.rock.n.| |roll.........| |its.a.long.way| |to.the.top.if.| |you.wanna.rock| |n.roll........| Text from the example input with maximum line lengths 13 and 14 You are given a text to be typeset and are experimenting with different values of the maximum line length m. For a fixed m, the leading sentence is a sentence (a sequence of words separated with a single space character) formed by the first words of lines top to bottom. In the example above, when the sample text is typeset with the maximum line length 14, the leading sentence is “its to you n”. Given a text and two integers a and b, find the length of the leading sentence for every candidate maximum line length between a and b inclusive. The length of a sentence is the total number of characters it contains including the space characters. Input The first line contains the text to be typeset – a sequence of words separated by exactly one space character. Each word is a string consisting of one or more lowercase letters from the English alphabet. The second line contains two integers a and b – the edges of the interval we are interested in, as described above. It is guaranteed that 1 ≤ w ≤ a ≤ b ≤ z ≤ 500 000, where w is the length of the longest word in the text and z is the total number of characters in the text including the space characters. Output Output b − a + 1 lines – the k-th of those lines should contain a single integer – the total length of the leading sentence when the maximum line length is equal to a − 1 + k. Example input its a long way to the top if you wanna rock n roll 13 16 output 22 12 12 15

题目大意:

给你一个字符串,问每一行的第一个单词的长度和是多少,并且每行之间都要加一个空格数。

解题思路:

咳咳其实这道题是开始就GG了,因为没都明白题,标红部分是后面才理解到的,题目不是很难,模拟法便可以做出,但是需要格外的注意每次标记位置的更新到底是更新为多少,这种题一定要多挣扎才会明白,下面代码已给出必要的注释与中间值

模拟法:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn=500000+10;
string s;
int l,r;
int sta[maxn];
int ed[maxn];

int main()
{
    memset(sta,0,sizeof(sta));
    memset(ed,0,sizeof(ed));

    getline(cin,s);
    scanf("%d %d",&l,&r);

    int len=s.size();
    int pos=1;

    //处理开始位置以及结束位置,sta为ed提供了具体的位置信息
    for(int i=0;i<len;i++)
    {
        if(s[i]!=' ')
        {
            sta[i+1]=pos;
        }
        else
        {
            ed[pos]=i+1;
            pos=i+2;
        }
    }
    ed[pos]=len+1;

    //开始讨论本行的末尾是在单词的前中后三种位置
    for(int i=l;i<=r;i++)
    {
        int sum=ed[sta[1]]-sta[1];//第一行的首个单词
        for(int j=i;j<len;)//模拟每一行的划分。
        {
            //cout<<"输出中间值   "<<sta[j]<<"  "<<sta[j+1]<<"   "<<j<<"   "<<s[j-1]<<"   "<<sum<<endl;
            if(sta[j+1]==0)//指在尾部
            {
                sum+=ed[sta[j+2]]-sta[j+2];
                j=j+i+1;
            }
            else if(sta[j]==0)//指在前面(空格处)
            {
                sum+=ed[sta[j+1]]-sta[j+1];
                j=j+i;
            }
            else if(sta[j]<=j&&ed[sta[j]]>j)//指在中间
            {
                sum+=ed[sta[j]]-sta[j];
                j=sta[j]+i-1;
            }
            sum++;
        }
        printf("%d\n",sum);
    }

    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值