URAL 1881. Long problem statement

16 篇文章 0 订阅

1881. Long problem statement

Time limit: 0.5 second
Memory limit: 64 MB
While Fedya was writing the statement of the problem  GOV Chronicles, he realized that there might be not enough paper to print the statements. He also discovered that his text editor didn't have the feature of calculating the number of pages in a text. Then Fedya decided to write a program that would calculate the number of pages for any given text.
Fedya knew that there were  h lines on each page and  w symbols in each line. Any two neighboring words in a line were separated by exactly one space. If there was no place for a word in a line, Fedya didn't hyphen it but put the whole word at the beginning of the next line.

Input

The first line contains the integers  hw, and  n, which are the number of lines on a page, the number of symbols in a line, and the number of words in the problem statement, respectively (1 ≤  h, w ≤ 100; 1 ≤  n ≤ 10 000). The statement written by Fedya is given in the following  n lines, one word per line. The words are nonempty and consist of uppercase and lowercase English letters and punctuation marks (period, comma, exclamation mark, and question mark); the length of each word is at most  w. The total length of all the words is at most 10 000.

Output

Output the number of pages in the problem statement.

Sample

input output
3 5 6
To
be
or
not
to
be
2




题意:给n个单词,要打印所有的单词。现已知每页纸有h行,每行的长度不超过w,规定每个单词中间需用一个空格隔开,一个单词不能分开打印在两行里。问最少需要多少张打印纸。

解析:直接模拟即可。



AC代码:

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;

int main(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    #endif //sxk

    int h, w, n, ans, left;
    string s;
    while(scanf("%d%d%d", &h, &w, &n)==3){
        ans = 1;
        left = w;
        for(int i=0; i<n; i++){
            cin>>s;
            int len = s.size();
            if(left < w) left --;
            if(len <= left) left -= len;
            else{
                ans ++;
                left = w - len;
            }
        }
        printf("%d\n", (ans + h - 1)/ h);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值