欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

CCF CSP 2020-06 Markdown 渲染器

程序员文章站 2022-08-03 12:52:55
没想到阴沟里翻船,B题被卡了一手读入...发一下C题的代码吧#includeusing namespace std;#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)typedef long long ll;typedef pair P;struct markdown{ int type; string s;};boo...

没想到阴沟里翻船,B题被卡了一手读入...

发一下C题的代码吧

#include<bits/stdc++.h>

using namespace std;
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
typedef long long ll;
typedef pair <ll, ll> P;
struct markdown
{
    int type;
    string s;
};

bool isspace(string &s)
{
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] != ' ')
            return false;
    }
    return true;
}

string standard(string s)
{
    int pos1 = 0, pos2 = s.length() - 1;
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] != ' ')
        {
            pos1 = i;
            break;
        }
    }
    for (int i = s.length() - 1; i >= 0; i--)
    {
        if (s[i] != ' ')
        {
            pos2 = i;
            break;
        }
    }
    string temp = s.substr(pos1, pos2 - pos1 + 1);
    return temp;
}

vector <markdown> v;

int main()
{
    //freopen("C://Users/Administrator/Desktop/4.txt","r",stdin);
    IOS;
    int w;
    cin >> w;
    ll ans = 0;
    string str;
    while (getline(cin, str))
    {
        if (!isspace(str))
            break;
    }
    if (str.size() >= 2 && str[1] == ' ' && str[0] == '*')
    {
        v.push_back({1, stardand(str.substr(2))});
    }
    else
    {
        v.push_back({2, stardand(str)});
    }
    bool check = false;
    while (getline(cin, str))
    {
        if (isspace(str))
            check = true;
        else
        {
            if (check)
            {
                check = false;
                if (str.size() >= 2 && str[1] == ' ' && str[0] == '*')
                {
                    v.push_back({1, stardand(str.substr(2))});
                }
                else
                {
                    v.push_back({2, stardand(str)});
                }
            }
            else
            {
                markdown &bac = v.back();
                if (bac.type == 1 || bac.type == 3)
                {
                    if (str.length() >= 2 && str[1] == ' ' && str[0] == ' ')
                    {
                        bac.s += " ";
                        bac.s += stardand(str.substr(2));
                    }
                    else if (str.size() >= 2 && str[1] == ' ' && str[0] == '*')
                    {
                        v.push_back({3, stardand(str.substr(2))});
                    }
                    else
                    {
                        v.push_back({2, stardand(str)});
                    }
                }
                else
                {
                    if (str.size() >= 2 && str[1] == ' ' && str[0] == '*')
                    {
                        v.push_back({1, stardand(str.substr(2))});
                    }
                    else
                    {
                        bac.s += " ";
                        bac.s += stardand(str);
                    }
                }
            }
        }
    }
//    for(auto &x:v)
//    {
//        cout<<x.type<<" "<<x.s<<endl;
//    }
    for (int i = 0; i < v.size(); i++)
    {
        string &str = v[i].s;
        if (v[i].type != 3 && i > 0)
        {
            //cout<<"kong"<<endl;
            ans++;
        }
        if (v[i].type != 2)
        {
            if (str.size() == 0)
            {
                //cout<<"kong"<<endl;
                ans++;
            }
            else
                for (int i = 0; i < str.length(); i += (w - 3))
                {
                    while (i < str.length() && str[i] == ' ')
                        i++;
                    //cout<<str[i]<<endl;
                    ans++;
                }
        }
        else
        {
            for (int i = 0; i < str.length(); i += w)
            {
                while (i < str.length() && str[i] == ' ')
                    i++;
                //cout<<str[i]<<endl;
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

 

本文地址:https://blog.csdn.net/qq_43313637/article/details/107110455