CodeForces 803D Magazine AD

CodeForces 803D Magazine AD

[题目描述]
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:

There are space-separated non-empty words of lowercase and uppercase Latin letters.

There are hyphen characters ‘-’ in some words, their positions set word wrapping points. Word can include more than one hyphen.

It is guaranteed that there are no adjacent spaces and no adjacent hyphens. No hyphen is adjacent to space. There are no spaces and no hyphens before the first word and after the last word.

When the word is wrapped, the part of the word before hyphen and the hyphen itself stay on current line and the next part of the word is put on the next line. You can also put line break between two words, in that case the space stays on current line. Check notes for better understanding.

The ad can occupy no more that k lines and should have minimal width. The width of the ad is the maximal length of string (letters, spaces and hyphens are counted) in it.

You should write a program that will find minimal width of the ad.

[输入]
The first line contains number k (1 ≤ k ≤ 105).

The second line contains the text of the ad — non-empty space-separated words of lowercase and uppercase Latin letters and hyphens. Total length of the ad don’t exceed 106 characters.

[输出]
Output minimal width of the ad.

[样例]
Input
4
garage for sa-le
Output
7

Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10

[样例解释]
Here all spaces are replaced with dots.

In the first example one of possible results after all word wraps looks like this:

garage.
for.
sa-
le
The second example:

Edu-ca-
tion-al.
Ro-unds.
are.so.fun

[解题思路]
这道题可以分为两部分进行处理,首先是将输入的字符串按照短横和空格分割得到每个单词(可以是分割以后的单词的一部分)的长度。然后查找能够写下所有单词的最小宽度,这里使用了check函数,从宽度为1开始增加,模拟能不能在k行之内写下,能写下则为最小的宽度,输出即可。

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int w[1000005],k,cnt=0;
bool check(int x)
{
	int line=1,temp=0;
	for(int i=0;i<cnt;i++)
	{
		temp+=w[i];
		if(i==cnt-1&&line<=k&&temp<=x)
			return true;
		if(line>k)
			return false;
		if(temp==x)
		{
			line++;
			temp=0;
		}
		if(temp>x)
		{
			line++;
			i--;
			temp=0;
		}
	}
}//检查当前宽度能否写下所有内容 
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	string s;
	cin>>k;
	cin.ignore();//注意在cin后使用getline,前面加上此句 
	getline(cin,s);
	int len=s.length(),pos=-1;
	for(int i=0;i<len;i++)
	{
		if(s[i]==' '||s[i]=='-'||i==len-1)
		{
			w[cnt++]=i-pos;
			pos=i;
		}
	}//用w数组存取所有分词长度 
	for(int i=1;;i++)
	{
		if(check(i))
		{
			cout<<i<<endl;
			break;
		}
	}//从宽度1开始依次检查是否符合 
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

球王武磊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值