CCF 202006-3 Markdown渲染器(Python100分)

CCF历届真题 部分题解 ,点击进入

问题描述

试题编号:202006-3
试题名称:Markdown渲染器
时间限制:1.0s
内存限制:512.0MB
问题描述:

 

 

 

 

import sys
text=[]
for i in sys.stdin.readlines():
    text.append(i)
w=int(text[0])
text=text[1:]
text1=[]
project=1
i=0
while i < len(text):
    #判断为项目
    if text[i][:2]=='* ':
        if project and text1:
            if text1[-1]!='\n':
                    text1.append('\n')
        s=text[i][2:].strip()
        i+=1
        while i < len(text):
            if text[i].split() and text[i][:2]=='  ':
                s+=' '+text[i][2:].strip()
            else:
                break
            i+=1
        #处理
        if len(s)+3<=w:
            text1.append(' . '+s)
        else:
            s=s.lstrip(' ')
            text1.append(' . '+s[:w-3])
            s=s[w-3:].lstrip(' ')
            while len(s)+3>w:
                text1.append('   '+s[:w-3])
                s=s[w-3:].lstrip(' ')
            text1.append('   '+s)
        if i< len(text):
            if text[i][:2]!='* ':
                text1.append('\n')
        project=0
    else:
        project=1
        if not text[i].split():
            if text1:
                if text1[-1]!='\n':
                    text1.append('\n')
            i+=1
        else:
            s=text[i].strip()
            i+=1
            while i < len(text):
                #判断下一行是否为段落
                if text[i].split() and text[i][:2]!='* ':
                    s+=' '+text[i].strip()
                else:
                    break
                i+=1
            #处理
            if len(s)<=w:
                text1.append(s)
            else:
                s=s.lstrip(' ')
                while len(s)>w:
                    text1.append(s[:w])
                    s=s[w:].lstrip(' ')
                text1.append(s)
print(len(text1))
""" 1.判断类型 项目、段落、空行
    2.如果为空行,  1)判断是否为渲染文本前的空行,如果是都不要
                    2)判断前一行是否已经有换行,如果有则不换行,否则换行
    3.段落:把一个段落的文本连接起来,渲染时每一次换行去掉前面的空格
    4.项目:1)首先判断上文不是项目并且已经有渲染文本,如果上一行不为换行则要换行
            2)把一个项目连接起来,注意:   ‘* ’
                                          ‘    come’这种格式
            3)判断是否还有下一行,如果有并且不为项目则换行"""
        

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值