#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int N = 20 * 1024 * 1204 + 10;
char str[N];
vector<string> strs;
vector<string> new_strs;
int check_kong(string &s)
{
for(auto s1:s)
{
if(s1 != ' ') return 0;
}
return 1;
}
// std::string& trim(std::string &);
// std::string& trim(std::string &s)
string trim(string &s)
{
if (s.empty())
{
return s;
}
s.erase(0,s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + 1);
return s;
}
int main()
{
int w;
cin >> w;
// getchar(); // 从缓冲区读走一个字符,相当于清除缓冲区
while(fgets(str,N,stdin))
{
strs.push_back(str);
strs.back().pop_back();//删除回车
}
int sum = 0;
string tmp;
for(int i=0;i < strs.size();i++)
{
// cout << strs[i] << endl;
if(check_kong(strs[i]))
{
continue;
}
else // 开始加入
{
tmp += trim(strs[i]);// 去掉strs首位的空格
tmp += ' ';
if(i < strs.size()-1 && check_kong(strs[i+1]) || i == strs.size()-1)
{
new_strs.push_back(tmp);
tmp = "";// 清空tmp
}
}
}
sum = new_strs.size() - 1;
// 假设都是段落
for(auto str2:new_strs)
{
if(str2.size() <= w)
{
sum++;
continue;
}
string s2 = str2.substr(w); // 去掉了前面w个
sum++;
while((int)s2.size() > 0) // 只要非空
{
s2 = trim(s2); // 取掉前面的,后面的基本不会有
// cout << s2 << endl;
if((int)s2.size() >= w) s2 = s2.substr(w),sum++;
else
{
if((int)s2.size() > 0) sum++;
break;
}
}
}
cout << sum;
return 0;
}
// string str;
// getline(cin,str);
// stringstream strs(str);
// string t;
// bool isEOF = strs.eof();
// cout << isEOF << endl;
// // while(getline(strs,t))
// // {
// // cout << t << endl;
// // }
// for(string s1;strs >> s1;cout << s1 << endl);
// cout << isEOF << endl;
CSP Markdown渲染器 40分
最新推荐文章于 2022-10-31 20:04:18 发布