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 ...
随机推荐
- Mac系统在终端中查看CPU信息的命令
在mac os x的终端中以命令行的形式查看本机cpu信息: sysctl -n machdep.cpu.brand_string E.G. lis-mbp:Home jenkins$ sysctl ...
- POJ 1113 凸包模板题
上模板. #include <cstdio> #include <cstring> #include <iostream> #include <algorit ...
- IIS Web负载均衡的几种方式
Web负载均衡的几种实现方式 摘要:负载均衡(Load Balance)是集群技术(Cluster)的一种应用.负载均衡可以将工作任务分摊到多个处理单元,从而提高并发处理能力.目前最常见的负载均衡应用 ...
- 手动配置gradle
最近从github倒入项目,运行的特别慢gradle配置有问题,解决方法: 1.C:\android\demo\hellocharts-android-master\gradle\wrapper 目录 ...
- Deferred的那些知识
在移动开发中的各种中,我们一定会遇到异步回调的问题,比如: 1:Css3执行动画完毕, 回调 2:Jquery Animate动画的执行完毕, 回调 3:Ajax的执行(并行.串行),回调 等等 ...
- php服务器安装memcache
https://pecl.php.net/get/memcache-3.0.8.tgz wget https://pecl.php.net/get/memcache-3.0.8.tgzgzip -d ...
- AJAX在GBK编码页面中传中文参数乱码的问题
---恢复内容开始--- 页面编码是GBK的情况下传递中文有乱码,解决方法如下: 在ajax传递前用若是Array,JSON,等其它对象,可用JSON.stringfy字符串序列化后,赋值给ajax传 ...
- Java [Leetcode 88]CMerge Sorted Array
题目描述: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. N ...
- B树索引和位图索引的区别!
B树索引主键和唯一性约束字段的B树索引,效率几乎和海量数据没有关系. 键值重复率低的字段比较适合使用B树索引. 位图索引键值重复率高的字段比较适合使用位图索引.count.and.or.in这些特定的 ...
- MySQL基础之第4章 MySQL数据类型
4.1.整数类型 tinyint(4)smallint(6)mediumint(9)int(11)bigint(20) 注意:后面的是默认显示宽度,以int为例,占用的存储字节数是4个,即4*8=32 ...