Book Borders

题目链接:

http://acm.hust.edu.cn/vjudge/contest/127407#problem/B

Description


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...| |its.a.long.way|
|way.to.the...| |to.the.top.if.|
|top.if.you...| |you.wanna.rock|
|wanna.rock.n.| |n.roll........|
|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 input file contains several test cases, each of them as described below.
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 ≤ 500000, 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


For each test case, 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.

Sample Input


its a long way to the top if you wanna rock n roll
13 16

Sample Output


22
12
12
15


##题意:

把一个长字符串分割成给定行宽的字串(不能使得单词断开)
输出每次分割后,所有字串的第一个单词组成的句子长度.


##题解:

直接暴力模拟即可.
记录 每个单词的长度 和 离当前位置最近的上一个结束位置.
关于时间复杂度:
在暴力枚举过程中,行宽越大,枚举的次数越少.
那么整体时间复杂度为 n(1/1 + 1/2 + 1/3 + ... + 1/n) = nlnn 不会超出时限.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 1010000
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

char str[maxn];

int pre[maxn];

int len[maxn];

int main(int argc, char const *argv[])

{

//IN;

int n;
while(gets(str) != NULL)
{
int sz = strlen(str); str[sz] = ' '; int flag = 0, last = 0, cnt = 0;
memset(pre, 0, sizeof(pre));
for(int i=0; i<=sz; i++, cnt++) {
if(str[i+1] == ' ') pre[i] = i;
else pre[i] = i==0? 0:pre[i-1]; if(!flag && str[i] != ' ') {
flag = 1;
last = i;
cnt = 0;
} if(flag && str[i] == ' ') {
flag = 0;
len[last] = cnt;
}
} int a,b; scanf("%d %d\n", &a,&b);
for(int i=a; i<=b; i++) {
int cur = 0, ans = 0;
while(cur < sz) {
ans += len[cur] + 1;
cur = pre[min(cur+i-1, sz-1)] + 2;
}
printf("%d\n", ans-1);
}
} return 0;

}

UVALive 7325 Book Borders (模拟)的更多相关文章

  1. UVALive 7325 Book Borders

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  2. UVALive - 6269 Digital Clock 模拟

    UVALive - 6269 Digital Clock 题意:时钟坏了,给你一段连续的时间,问你现在可能的时间是多少. 思路:直接模拟,他妈的居然这场就跪在了这题,卧槽,他妈的就在111行,居然多打 ...

  3. UVALive - 7139(差分+模拟)

    题目链接 参考 题意 N*M的网格,一辆车沿着网格线按给定路线走,每个网格里有一个人,人的视线始终看着车,问这些人净转圈数的平方和. 分析 由于车的起点和终点都为左上角,且每个格子里的人永远面对着车, ...

  4. UVALive 7464 Robots(模拟)

    7464Robots Write a program to collect data from robots. We are given two sets of robotsX=fX1;:::;Xmg ...

  5. 【Bit String Reordering UVALive - 6832 】【模拟】

    题意分析 题目讲的主要是给你一个01串,然后给你要变成的01串格式,问你要转换成这一格式最少需要移动的步数. 题目不难,但当时并没有AC,3个小时的个人赛1道没AC,归根到底是没有逼自己去想,又想的太 ...

  6. 【Miscalculation UVALive - 6833 】【模拟】

    题目分析 题目讲的是给你一个串,里面是加法.乘法混合运算(个人赛中误看成是加减乘除混合运算),有两种算法,一种是乘法优先运算,另一种是依次从左向右运算(不管它是否乘在前还是加在前). 个人赛中试着模拟 ...

  7. UVaLive 6809 Spokes Wheel (模拟)

    题意:给定两个16进制数,问你把它转成二进制后,把第一个向左或者向右旋转最少的次数同,使得第一个变成第二个. 析:也是比较水的,按照要求做就好,注意0的情况,可能会忘记. #pragma commen ...

  8. UVALive 6858 Frame (模拟)

    Frame 题目链接: http://acm.hust.edu.cn/vjudge/contest/130303#problem/D Description http://7xjob4.com1.z0 ...

  9. 模拟/字符串处理 UVALive 6833 Miscalculatio

    题目传送门 /* 模拟/字符串处理:主要是对*的处理,先把乘的预处理后再用加法,比如说是:1+2*3+4 = 1+..6+4 = 11 */ #include <cstdio> #incl ...

随机推荐

  1. html5 touch事件实现触屏页面上下滑动(二)

    五一小长假哪都没去,睡了三天,今天晕晕沉沉的投入工作了,但还是做出了一点点效果 上周用html5的touch事件把简单的滑动做出来了,实现了持续页面上下滑动的效果,参考之前 的文章及代码html5 t ...

  2. Cookie的具体使用之来存储对象

    1.创建一个新的cookie,并赋值. HttpCookie cookie;       cookie=new HttpCookie("user");       cookie.D ...

  3. oracle db shutdown immediate–multi Instance

    [oracle@redhat4 ~]$ sqlplus / as sysdba@orcl SQL*Plus: Release 11.2.0.1.0 Production on Tue Oct 6 21 ...

  4. C++ STL之set的基本操作

    set是集合,虽然也存在键值和实值,不过两者根本就是同一个值,键值的设置完全就是为了满足红黑树的底层结构,set操作与map很像不过也有些不同. 1. set迭代器与map的不同: (1)set使用接 ...

  5. Effective C++学习笔记 条款02:尽量以const,enum,inline替换 #define

    尽量使用const替换 #define定义常量的原因: #define 不被视为语言的一部分 宏定义的常量,预处理器只是盲目的将宏名称替换为其的常量值,导致目标码中出现多分对应的常量,而const定义 ...

  6. Jqgrid入门-Jqgrid列数据拖动(七)

    上一章提到在Jqgrid中如何设置二级表头,这一章节主要探讨Jqgrid表格里面的数据如果实现拖动功能,比如你想把第一行的数据拖到当前页的最后一行,或者其他位置.     Jqgrid表格插件自己没有 ...

  7. 用I/O口模拟IIC总线协议遇到的一些问题

    最近做的一个项目,是基于IIC总线通信的传感器系统.由于另外一个传感器使用的是类IIC协议,而不是标准IIC,所以MCU不能与其通信,最后没有办法,只有通过I/O口模拟的方式实现IIC的总线通信.具体 ...

  8. ionic安装拍照选照片插件

    1.安装插件,也可以用ionic plugin add .... phonegap local plugin add https://git-wip-us.apache.org/repos/asf/c ...

  9. c++实现输入法窗口自定义的代码

    #pragma once #include <Windows.h> #include <imm.h> #include <string> #pragma comme ...

  10. Math.trunc