7-118 4001 HTML解析

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <limits>
#include <numeric>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using i32 = std::int32_t;
using i64 = std::int64_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using pii = std::pair<i32, i32>;
using pll = std::pair<i64, i64>;
using vi = std::vector<i32>;
using vll = std::vector<i64>;
using vpii = std::vector<pii>;
using vpll = std::vector<pll>;

void solve();

int main()
{
    std::cin.tie(nullptr)->sync_with_stdio(false);
    solve();
}

template <typename T> std::vector<std::string> split(T &&s, char dot = '.')
{
    std::vector<std::string> res;
    std::string temp;
    for (const auto &item : s)
    {
        if (item == dot)
        {
            if (!temp.empty())
            {
                res.emplace_back(std::move(temp));
                temp.clear();
            }
            continue;
        }
        temp += item;
    }
    if (!temp.empty())
    {
        res.emplace_back(std::move(temp));
    }
    return res;
}

void print_br()
{
    std::cout << '\n';
}

void print_hr()
{
    std::cout << '\n';
    for (i32 i = 1; i <= 80; ++i)
    {
        std::cout << '-';
    }
}

void solve()
{
    std::string data;
    std::string str;
    while (std::getline(std::cin, str))
    {
        data += str + ' ';
    }
    bool endl = false;
    auto &&res = split(data, ' ');
    bool space = false;
    i32 count{0};
    for (u64 i = 0; i < res.size(); ++i)
    {
        if (res[i] == "<br>")
        {
            print_br();
            space = false;
            count = 0;
        }
        else if (res[i] == "<hr>")
        {
            print_hr();
            endl = true;
            space = false;
            count = 0;
        }
        else
        {
            if (endl)
            {
                std::cout << '\n';
                endl = false;
            }
            count += res[i].length();
            if (count >= 80)
            {
                count = 0;
                --i;
                space = false;
                std::cout << '\n';
                continue;
            }
            if (space)
            {
                std::cout << ' ';
                ++count;
            }
            std::cout << res[i];
            space = true;
        }
    }
}
posted @ 2025-10-13 23:14  TPPPP72  阅读(5)  评论(0)    收藏  举报