CSP Markdown渲染器 40分

#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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值