跳到正文

题解

BBCode转换Markdown

159号程序员约 4 分钟
原文
文章信息与版本

文章信息

发布
2020年8月14日
收录
2026年10月17日
更新
2026年11月8日
篇幅
约 4 分钟

版本

  1. v1当前

啊!猪国杀太难了!写一道大模拟吧!

Day1:20200814

C++
#include <bits/stdc++.h>
using namespace std;

int main()
{
    string s;
    while(getline(cin, s))
    {
        bool lh1, lh2, rh1, rh2; //判断是否匹配
        lh1 = lh2 = rh1 = rh2 = 0; //初始化为0
        int len = s.length();
        for(int i = 0; i < len; i++) //先判断是否合法
        {
            if(s[i] == '[' && s[i + 1] == 'h' && s[i + 2] == '1' && s[i + 3] == ']') 
            {
                lh1 = 1; //当看到[h1]字样时,lh1=1
            }
            if(s[i] == '[' && s[i + 1] == 'h' && s[i + 2] == '2' && s[i + 3] == ']') 
            {
                lh2 = 1; //当看到[h2]字样时,lh2=1
            }
            if(s[i] == '[' && s[i + 1] == '/' && s[i + 2] == 'h' && s[i + 3] == '1' && s[i + 4] == ']')
            {
                rh1 = 1; //当看到[/h1]字样时,rh1=1
            }
            if(s[i] == '[' && s[i + 1] == '/' && s[i + 2] == 'h' && s[i + 3] == '2' && s[i + 4] == ']')
            {
                rh2 = 1; //当看到[/h1]字样时,rh1=1
            }
        }
        //printf("\n%d %d %d %d\n", lh1, lh2, rh1, rh2);
        if(lh1 && !rh1 || !lh1 && rh1 || lh2 && !rh2 || !lh2 && rh2) 
        {
            printf("%s", "Unclosed Mark");
            return 0;
        }
        for(int i = 0; i < len; i++) //再输出
        {
            if(s[i] == '[' && s[i + 1] == 'h' && s[i + 2] == '1' && s[i + 3] == ']') 
            {
                printf("%c ", '#');
                for(int j = i + 4; s[j] != '['; j++)
                {
                    printf("%c", s[j]);
                }
            }
            if(s[i] == '[' && s[i + 1] == 'h' && s[i + 2] == '2' && s[i + 3] == ']') 
            {
                printf("%s", "## ");
                for(int j = i + 4; s[j] != '['; j++)
                {
                    printf("%c", s[j]);
                }
            }
            if(s[i] == '[' && s[i + 1] == '/' && s[i + 2] == 'h' && s[i + 3] == '1' && s[i + 4] == ']')
            {
                printf(" %c", '#');
                break;
            }
            if(s[i] == '[' && s[i + 1] == '/' && s[i + 2] == 'h' && s[i + 3] == '2' && s[i + 4] == ']')
            {
                printf("%s", " ##");
                break;
            }
        }
    }
}

OK,20分拿下