zoukankan      html  css  js  c++  java
  • 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
    ", &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
    ", ans-1);
        }
    }
    
    return 0;
    

    }

  • 相关阅读:
    学习博客
    file-max与ulimit的关系与差别
    buffer cache chain 图
    计算机体系结构 ---图2
    计算机体系结构-图
    工作于内存和文件之间的页缓存, Page Cache, the Affair Between Memory and Files
    Linux Kernel: buffers和cached的区别
    lnux内核的malloc实现(Oracle的cache buffer影子)
    内存管理概述、内存分配与释放、地址映射机制(mm_struct, vm_area_struct)、malloc/free 的实现
    Linux 内核的文件 Cache 管理机制介绍-ibm
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5759391.html
Copyright © 2011-2022 走看看