问题描述
试题编号: | 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)判断是否还有下一行,如果有并且不为项目则换行"""