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. Zookeeper、HBase的伪分布

    1.Zookeeper伪分布的部署(3个节点) 所谓的“伪分布式集群”就是在一台服务器中,启动多个Zookeeper实例.“完全分布式集群”是每台服务器,启动一个Zookeeper实例. 1.1.解压 ...

  2. ProgressBar 示例及自定义样式

    在layout中使用ProgerssBar,其中使用了自定义的样式 <ProgressBar android:id="@+id/footer_refresh_prgs" st ...

  3. JAVA使用原始HttpURLConnection发送POST数据

    package com.newflypig.demo; /** * 使用jdk自带的HttpURLConnection向URL发送POST请求并输出响应结果 * 参数使用流传递,并且硬编码为字符串&q ...

  4. Mysql的 时间戳转换 和 c# 的时间戳转换 (以秒来进行转换,非毫秒,主要是mysql不能存毫秒)

    Mysql 时间戳函数 => 从时间 转成 时间戳   UNIX_TIMESTAMP() 获取当前服务器时间的时间戳 UNIX_TIMESTAMP('2013-01-01 12:33:19') ...

  5. Nginx & AWStats 安装、配置、使用

    —— 参考IBM文章:THIS , 不一样的指导顺序 —— 1. awstats分析nginx - access.log,网上资料大部分都是下载,然后配置.官网下载地址: http://awstats ...

  6. 最简单的视音频播放示例9:SDL2播放PCM

    本文记录SDL播放音频的技术.在这里使用的版本是SDL2.实际上SDL本身并不提供视音频播放的功能,它只是封装了视音频播放的底层API.在Windows平台下,SDL封装了Direct3D这类的API ...

  7. FFmpeg在Android使用3

    android 移植ffmpeg后so库的使用   只需要将我们编译好的ffmpeg的so包(在/obj/local/armeabi/libffmpeg.so)copy到所在ndk下的\platfor ...

  8. 将Temporary文件夹里的Logo文件转移到Logo文件夹

    /// <summary> /// 将Temporary文件夹里的Logo文件转移到Logo文件夹 /// </summary> /// <param name=&quo ...

  9. 请不要用SECONDS_BEHIND_MASTER来衡量MYSQL主备的延迟时间

    链接:http://www.woqutech.com/?p=1116 MySQL 本身通过 show slave status 提供了 Seconds_Behind_Master ,用于衡量主备之间的 ...

  10. asp.net MVC 应用程序的生命周期(上)

    首先我们知道http是一种无状态的请求,他的生命周期就是从客户端浏览器发出请求开始,到得到响应结束.那么MVC应用程序从发出请求到获得响应,都做了些什么呢? 本文我们会详细讨论MVC应用程序一个请求的 ...