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.

Input

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

Output minimal width of the ad.

Examples

Input
4
garage for sa-le
Output
7
Input
4
Edu-ca-tion-al Ro-unds are so fun
Output
10

Note

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 题目大意:有一组字符串,用连字符‘-’以及空格‘ ’进行分割,要求分割不超过k段,求分割后的最小宽度(宽度就是分割后最长的字符串的长度)
思路:博主是个菜鸡,一开始没啥思路,学姐讲题的时候才想到用二分。。。。。。。。
  经过不断地啃博客(正经脸),算是明白一点如何从 二分 入手这道题。
  把每一段字符串 按照一个长度 x 进行分割,得到的字符串长度不能超过 x ,看最后得到的行数是否小于k;
  如果按长度x进行分割得到的行数小于k 那么按长度 x+1 进行分割所得到的行数必定小于k。
  注意 最小宽度 定义,这个时候我们就可以用二分,不断地去逼近这个最小宽度,就可以得到答案。
  最后注意的就是二分的上下界,上界就是初始字符串的长度s.size(),下界可以是s.size()/k(这个博主就不解释了)。
AC代码:
 #include<bits/stdc++.h>
using namespace std;
vector<int>v;
string s;
int k;
bool check(int x){
int line = ;// 记录以x切割时可以得到的行数
int len = ;
for(int i = ; i < v.size(); i++){
if(v[i] > x){
return ;// 如果v[i] > x 说明存在v[i]让x无法表示(说明x取小了)
}
if(v[i] + len > x){
line++;
len = v[i];
}else if(v[i] + len == x){
line++;
len = ;
}else if(v[i] + len < x){
len += v[i];
}// 将字符串 以不大于x 的长度分割
// line 记录进行分割后得到的总行数
}
if(len > ) line++;// 注意分割最后的一小段字符串是否忽略
return line <= k;// 根据x分割所得到的总行数不能大于k
} int main(){
while(~scanf("%d",&k)){
v.clear();
getchar();
getline(cin,s);
int j = ;
for(int i = ; i < s.size(); i++){
if(s[i] == ' ' || s[i] == '-'){
v.push_back(j + );
j = ;
}else j++;
}
v.push_back(j);// 不要忘记末尾的字符串长度
int l = s.size()/k, r = s.size();
while(r - l > ){
int mid = (r + l)/ ;
if(check(mid)){
r = mid;
}else{
l = mid + ;
}
}
printf("%d\n",l);
}
return ;
}

Magazine Ad

一个从很久以前就在做的梦。

 

最新文章

  1. v14.0\AspNet\Microsoft.Web.AspNet.Props 找不到
  2. 分页控件layui的使用
  3. 在Ubuntu上安装有道词典
  4. SQL删除约束
  5. Unity中那些事半功倍的好插件
  6. 个性二维码开源专题&lt;基础篇&gt;
  7. unity3d 截屏
  8. Matlab mex编程
  9. 简单的FIRST集演示程序
  10. 新手笔记-tftp与yum
  11. 【Machine Learning in Action --4】朴素贝叶斯过滤网站的恶意留言
  12. Alamofire源码解读系列(五)之结果封装(Result)
  13. XJOIWinterCampPrecontest1-P2队列
  14. MP4文件格式的解析
  15. centos----------centos下如何安装phpstorm
  16. mongodb与mysql命令详细对比
  17. underscore utility
  18. RECONSUME_LATER
  19. 傻瓜式搭建php+nginx+mysql服务器环境
  20. 考研编程练习----m叉树先序和后序所包含的情况

热门文章

  1. Android蓝牙自动配对Demo,亲测好使!!!
  2. SQL Server中建立自定义函数
  3. 广告点击率预测(CTR) —— 在线学习算法FTRL的应用
  4. Web API 2 入门——Web API 2(C#)入门(谷歌翻译)
  5. JS 获取指定日期的前几天,后几天
  6. python 爬虫网络图片中遇到的问题总结
  7. CountDownLatch的简单使用
  8. mysql 查询大量数据报错
  9. Windows下设置Ubuntu引导项
  10. sysctl.conf学习和调优