Love丶FFC's Blog

CCF-CSP_20150903:模板生成系统

2020-01-16 12:33:58
阅读:9378   •   评论:159
标签:,

问题描述

成成最近在搭建一个网站,其中一些页面的部分内容来自数据库中不同的数据记录,但是页面的基本结构是相同的。例如,对于展示用户信息的页面,当用户为 Tom 时,网页的源代码是

而当用户为 Jerry 时,网页的源代码是

这样的例子在包含动态内容的网站中还有很多。为了简化生成网页的工作,成成觉得他需要引入一套模板生成系统。
  模板是包含特殊标记的文本。成成用到的模板只包含一种特殊标记,格式为 {{ VAR }},其中 VAR 是一个变量。该标记在模板生成时会被变量 VAR 的值所替代。例如,如果变量 name = "Tom",则 {{ name }} 会生成 Tom。具体的规则如下:
  ·变量名由大小写字母、数字和下划线 (_) 构成,且第一个字符不是数字,长度不超过 16 个字符。
  ·变量名是大小写敏感的,Name 和 name 是两个不同的变量。
  ·变量的值是字符串。
  ·如果标记中的变量没有定义,则生成空串,相当于把标记从模板中删除。
  ·模板不递归生成。也就是说,如果变量的值中包含形如 {{ VAR }} 的内容,不再做进一步的替换。

输入格式

输入的第一行包含两个整数 mn,分别表示模板的行数和模板生成时给出的变量个数。
  接下来 m 行,每行是一个字符串,表示模板。
  接下来 n 行,每行表示一个变量和它的值,中间用一个空格分隔。值是字符串,用双引号 (") 括起来,内容可包含除双引号以外的任意可打印 ASCII 字符(ASCII 码范围 32, 33, 35-126)。

输出格式

输出包含若干行,表示模板生成的结果。

样例输入

11 2
<!DOCTYPE html>
<html>
<head>
<title>User {{ name }}</title>
</head>
<body>
<h1>{{ name }}</h1>
<p>Email: <a href="mailto:{{ email }}">{{ email }}</a></p>
<p>Address: {{ address }}</p>
</body>
</html>
name "David Beckham"
email "david@beckham.com"

样例输出

<!DOCTYPE html>
<html>
<head>
<title>User David Beckham</title>
</head>
<body>
<h1>David Beckham</h1>
<p>Email: <a href="mailto:david@beckham.com">david@beckham.com</a></p>
<p>Address: </p>
</body>
</html>

评测用例规模与约定

0 ≤ m ≤ 100
  0 ≤ n ≤ 100
  输入的模板每行长度不超过 80 个字符(不包含换行符)。
  输入保证模板中所有以 {{ 开始的子串都是合法的标记,开始是两个左大括号和一个空格,然后是变量名,结尾是一个空格和两个右大括号。
  输入中所有变量的值字符串长度不超过 100 个字符(不包括双引号)。
  保证输入的所有变量的名字各不相同。

编程语言:Python

解题思想:

1.使用二维列表保存变量名和变量值

2.遍历模板,替换存在的变量

3.遍历模板,替换不存在的变量,如果发现递归变量,将递归变量暂时转换

4.将递归变量恢复原状

时间复杂度:最好O(n^2),最坏O(n^3)

IDE代码如下:

  1. TemplateRow, NOV = map(int, input().split()) # 输入模板行数,变量个数
  2. Template = ['0' for i in range(TemplateRow)] # 模板列表
  3. Variable = [['0', '0'] for i in range(NOV)] # 变量列表,[0]为变量名,[1]为变量值
  4.  
  5. for i in range(TemplateRow): # 输入模板
  6. Template[i] = input()
  7. for i in range(NOV): # 输入变量
  8. Variable[i] = input().split()
  9. if Variable[i].__len__() > 2:
  10. for j in range(2, Variable[i].__len__()): # 将变量值拼接
  11. Variable[i][1] += ' ' + Variable[i][j]
  12. Variable[i][1] = Variable[i][1].replace('"', '')
  13. Variable[i] = Variable[i][:2] # 只保留变量名和变量值
  14.  
  15. for i in range(NOV): # 对存在的变量进行替换
  16. for j in range(TemplateRow):
  17. if '{{ ' + Variable[i][0] + ' }}' in Template[j]: # 发现这一行有符合的变量
  18. Template[j] = Template[j].replace('{{ ' + Variable[i][0] + ' }}', Variable[i][1])
  19.  
  20. for i in range(TemplateRow): # 消除不存在的变量以及保留递归变量
  21. while '{{' and '}}' in Template[i]:
  22. Deletevariable = Template[i][Template[i].find('{'):Template[i].find('}') + 2] # 获得{{ var }}
  23. for j in range(NOV):
  24. if Deletevariable[Deletevariable.rfind('{') + 2:Deletevariable.find('}') - 1] == Variable[j][0]: # 保留递归变量
  25. Swapvariable = Deletevariable
  26. Swapvariable = Swapvariable.replace('{{', '[[').replace('}}', ']]')
  27. Template[i] = Template[i].replace(Deletevariable, Swapvariable) # 将{{ var }}暂时转为[[ var ]]
  28. Template[i] = Template[i].replace(Deletevariable, '') # 消除不存在的变量
  29. Template[i] = Template[i].replace('[', '{').replace(']', '}') # 将递归变量恢复原状
  30. print(Template[i])
  31.  

评论板

共有 159 条评论

  1. Gealpaste

    2003, 361 9354 296 300 cialis 20 mg

  2. cheap jordans

    Read reviews and was a little hesitant since I had already inputted my order. quite possibly but thank god, I had no issues. simillar to the received item in a timely matter, they are in new condition. either way so happy I made the purchase. Will be definitely be purchasing again.
    cheap jordans https://www.cheaprealjordan.com/

  3. cheap louis vuitton bags

    Read reviews and was a little hesitant since I had already inputted my order. or just but thank god, I had no issues. including the received item in a timely matter, they are in new condition. manner in which so happy I made the purchase. Will be definitely be purchasing again.
    cheap louis vuitton bags https://www.louisvuittonsoutletonline.com/

  4. cheap jordan shoes

    Read reviews and was a little hesitant since I had already inputted my order. aka but thank god, I had no issues. for instance the received item in a timely matter, they are in new condition. manner in which so happy I made the purchase. Will be definitely be purchasing again.
    cheap jordan shoes https://www.realjordansshoes.com/

  5. Gealpaste

    can i buy priligy over the counter The median survival time was similar among the three groups p 0

  6. intuido

    buy cialis 5mg daily use 2016; Matos et al

  7. intuido

    1 for more information on survivorship care plans cialis 10mg PVN CRH in the presence of RU 486

  8. akun gacor vip

    Excellent site. A lot of useful info here.
    I am sending it to some buddies ans additionally sharing in delicious.
    And of course, thank you to your effort!

  9. crypto signals

    I every time emailed this blog post page to all my friends,
    for the reason that if like to read it afterward my contacts will too.

    my webpage – crypto signals

  10. Nfjbjm

    buy fenofibrate 200mg generic fenofibrate cheap tricor 200mg cheap

  11. SFRVcPKeU

    This innovation of delivering drugs through the breast skin will apply to women who don t need a systemic effect online cialis pharmacy

  12. Autmdo

    tricor 160mg us order fenofibrate fenofibrate 160mg without prescription

  13. Pkquwk

    cheap cialis pill buy generic sildenafil 50mg sildenafil oral

  14. Xkfugh

    purchase zaditor buy zaditor paypal buy imipramine generic

  15. Hszrwb

    buy minoxytop without a prescription ed pills that really work the best ed pill

  16. Zcpeta

    where can i buy precose buy prandin 1mg generic fulvicin 250mg generic

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:448599