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 ...
随机推荐
- 如何使用UIAutomation进行iOS 自动化测试(Part I)
转自:http://www.cnblogs.com/vowei/archive/2012/08/10/2631949.html 写在前面 研究iOS的自动化测试也有些日子了,刚开始的时候,一直苦于找不 ...
- Chosen中选择项的更新
Chosen 选择项的动态修改/更新 如果你需要去动态更新select选择框里的选择项,你需要通知Chosen去响应这个变动,你需要在这个选项框是触发一个"liszt:updated&quo ...
- bzoj2437
会做jsoi那道game,这题就非常简单了吧 我们考虑空格的移动,显然,初始与空格位置距离为奇数的黑棋和距离为偶数的白棋并没有什么用, 空格不会移到那,我们直接把他们当作障碍,其他点我们当作可移动区域 ...
- CodeForces Round #278 (Div.2) (待续)
A 这么简单的题直接贴代码好了. #include <cstdio> #include <cmath> using namespace std; bool islucky(in ...
- OpenSSL再爆多处高危漏洞
OpenSSL团队于北京时间6月5号晚8点左右发布了5个安全补丁,这次的更新涉及多处高危漏洞,连接:http://www.openssl.org/news/ 受影响的版本包括: OpenSSL 1.0 ...
- UVA 4080 Warfare And Logistics 战争与物流 (最短路树,变形)
题意: 给一个无向图,n个点,m条边,可不连通,可重边,可多余边.两个问题,第一问:求任意点对之间最短距离之和.第二问:必须删除一条边,再求第一问,使得结果变得更大. 思路: 其实都是在求最短路的过程 ...
- linux下/etc/passwd和/etc/shadow文件
/etc/passwd文件中保存的是用户的账号信息,而/etc/shadow文件中保存的是用户的口令信息. 一 /etc/passwd 一个用户对应着该文件中一行记录,一行记录由若干个字段组成,字段之 ...
- 给table中某一列的文字右对齐
一般来说,没写过jquery的前端人员,肯定是定义一个class,给每一行的那列加上align_r{text-align:right}.这是很麻烦的. 所以用jquery来写,可以$("ta ...
- 编译boost (windows msvc14)
我的环境 OS: WIN10 (x64) IDE: VS2015 (VC14) http://www.boost.org/ 1. 下载 下载boost包, boost_1_62_0.7z 使用ASIO ...
- hdu 3461 Code Lock(并查集)2010 ACM-ICPC Multi-University Training Contest(3)
想不到这还可以用并查集解,不过后来证明确实可以…… 题意也有些难理解—— 给你一个锁,这个所由n个字母组成,然后这个锁有m个区间,每次可以对一个区间进行操作,并且区间中的所有字母要同时操作.每次操作可 ...