UVALive 7325 Book Borders (模拟)
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 (模拟)的更多相关文章
- UVALive 7325 Book Borders
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive - 6269 Digital Clock 模拟
UVALive - 6269 Digital Clock 题意:时钟坏了,给你一段连续的时间,问你现在可能的时间是多少. 思路:直接模拟,他妈的居然这场就跪在了这题,卧槽,他妈的就在111行,居然多打 ...
- UVALive - 7139(差分+模拟)
题目链接 参考 题意 N*M的网格,一辆车沿着网格线按给定路线走,每个网格里有一个人,人的视线始终看着车,问这些人净转圈数的平方和. 分析 由于车的起点和终点都为左上角,且每个格子里的人永远面对着车, ...
- UVALive 7464 Robots(模拟)
7464Robots Write a program to collect data from robots. We are given two sets of robotsX=fX1;:::;Xmg ...
- 【Bit String Reordering UVALive - 6832 】【模拟】
题意分析 题目讲的主要是给你一个01串,然后给你要变成的01串格式,问你要转换成这一格式最少需要移动的步数. 题目不难,但当时并没有AC,3个小时的个人赛1道没AC,归根到底是没有逼自己去想,又想的太 ...
- 【Miscalculation UVALive - 6833 】【模拟】
题目分析 题目讲的是给你一个串,里面是加法.乘法混合运算(个人赛中误看成是加减乘除混合运算),有两种算法,一种是乘法优先运算,另一种是依次从左向右运算(不管它是否乘在前还是加在前). 个人赛中试着模拟 ...
- UVaLive 6809 Spokes Wheel (模拟)
题意:给定两个16进制数,问你把它转成二进制后,把第一个向左或者向右旋转最少的次数同,使得第一个变成第二个. 析:也是比较水的,按照要求做就好,注意0的情况,可能会忘记. #pragma commen ...
- UVALive 6858 Frame (模拟)
Frame 题目链接: http://acm.hust.edu.cn/vjudge/contest/130303#problem/D Description http://7xjob4.com1.z0 ...
- 模拟/字符串处理 UVALive 6833 Miscalculatio
题目传送门 /* 模拟/字符串处理:主要是对*的处理,先把乘的预处理后再用加法,比如说是:1+2*3+4 = 1+..6+4 = 11 */ #include <cstdio> #incl ...
随机推荐
- Uboot 2014.07 makefile分析 - 其他Cortex系列
uboot的官网可以通过谷歌搜索得到,显示结果第一个链接就是. 官网:: http://www.denx.de/wiki/U-Boot ftp下载: ftp://ftp.denx.de/pub/u-b ...
- 自定义View(1)简单流程及示例模板
1,继承View , ViewGroup,或TextView等等 2,绘制相关的api, canvas 画布, paint 画笔 2,重写重要的函数(注意这个顺序) onMeasure 属于View的 ...
- [HDOJ3635]Dragon Balls(并查集,路径压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意:有n个龙珠,n个城市.初始状态第i个龙珠在第i个城市里.接下来有两个操作: T A B:把 ...
- git push default
今天使用git push的时候出现了如下提示: warning: push.default is unset; its implicit value is changing in Git 2.0 fr ...
- 1242. Werewolf(dfs)
1242 简单dfs 往孩子方向搜一遍 父母方向搜一遍 输入还搞什么字符串.. #include <iostream> #include<cstdio> #include< ...
- JAVA调用易信接口向指定好友推送消息(一)背景需求
众所周知,中国电信内部一直使用易信群进行交流 各种工作交流都在易信群里面沟通 包括投诉处理,障碍报修,拍照上传 最重要的就是每天甚至每个时点的指标完成情况的通报 所以只能用4个字来形容 String ...
- BZOJ2253: [2010 Beijing wc]纸箱堆叠
题解: 其实就是求三维偏序最长链.类似于三维逆序对,我们可以用树状数组套平衡树来实现. DP方程 :f[i]=max(f[j]+1) a[j]<a[i] 我们按一维排序,另一位建立树状数组,把第 ...
- ASP.NET MVC Html.ActionLink使用说明
本文整理了该方法的几种重载形式: 1.Html.ActionLink("linkText","actionName")该重载的第一个参数是该链接要显示的文字,第 ...
- IE6、IE7、IE8中overflow:hidden无效问题
在做图片无缝滚动效果时遇到了这个兼容问题 div宽1000px高250px超出隐藏. 但在Firefox中正常,超出部分隐藏,但是在IE6.IE7.IE8.Sogou高速下都显示了出来.做了这么多年的 ...
- 试图从数据库 ‘UFData_001_2003' 中提取的逻辑页 (1:10720) 属于对象 '0',而非对象 'syscolumns'
数据库可以使用,可以备份,但对备份进行恢复时报错,使用sp_attach_db对两个物理文件进行连接时,报同样错误: 服务器: 消息 605,级别 21,状态 1,行 1 试图从数据库 ‘UFData ...