CCF 认证(CSP) 练题笔记

202006-3 Markdown渲染器

定义 \(pos\) 存储下一次要填的位置, 特别 \(pos=w\) 的含义是即将换行但是还没换. 注意 * 的空行也占一行.

代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inc(i, l, r) for (int i = l; i <= r; i++)

const int maxn = 1e6 + 5;

int w, res;
vector<string> s, t;
string tmp;

bool empty(string ss) {
    for (auto e : ss)
        if (e != ' ') return false;
    return true;
}

string fix(string ss) {
    if (empty(ss)) return "";
    int i, j;
    for (i = 0; i < ss.size(); i++) {
        if (ss[i] != ' ') break;
    }
    for (j = ss.size() - 1; j >= 0; j--) {
        if (ss[j] != ' ') break;
    }
    return ss.substr(i, j - i + 1);
}

void solve() {
    res++;
    string tot;
    int status = 0, pos = w;
    for (int i = 0; i < t.size(); i++) {
        auto qko = [&]() {
            string now = " " + fix(t[i].substr(2));
            for (int j = 0; j < now.size(); j++) {
                if (now[j] == ' ') {
                    if (pos > 0 && pos < w - 3) pos++;
                } else {
                    if (pos == w - 3) res++, pos = 0;
                    pos++;
                }
            }
        };
        if (t[i].size() >= 2 && t[i][0] == '*' && t[i][1] == ' ') {
            if (status == 1) {
                pos = 0;
                res += 2;
            } else {
                pos = 0;
                res++;
            }
            status = 2;
            qko();
        } else if (t[i].size() >= 2 && t[i][0] == ' ' && t[i][1] == ' ' &&
                   status == 2) {
            qko();
        } else {
            if (status == 2) {
                pos = 0;
                res += 2;
            }
            status = 1;
            string now = " " + fix(t[i]);
            for (int j = 0; j < now.size(); j++) {
                if (now[j] == ' ') {
                    if (pos > 0 && pos < w) pos++;
                } else {
                    if (pos == w) res++, pos = 0;
                    pos++;
                }
            }
        }
    }
}

int main() {
    freopen("2020006-3.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> w;
    while (getline(cin, tmp)) s.push_back(tmp);
    for (int i = 0; i < s.size(); i++) {
        if (empty(s[i])) {
            if (t.size()) {
                solve();
                t.clear();
            }
        } else {
            t.push_back(s[i]);
        }
    }
    if (t.size()) solve();
    res--;
    cout << res << "\n";
}

上一篇:CCF认证201812-2小明放学


下一篇:CCF 最大的矩形