您的位置:首页 > 其它

CCF 201509-3 模板生成系统

2016-02-02 00:01 351 查看
问题描述

试题编号:201509-3
试题名称:模板生成系统
时间限制:1.0s
内存限制:256.0MB
问题描述:问题描述
  成成最近在搭建一个网站,其中一些页面的部分内容来自数据库中不同的数据记录,但是页面的基本结构是相同的。例如,对于展示用户信息的页面,当用户为 Tom 时,网页的源代码是



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



  这样的例子在包含动态内容的网站中还有很多。为了简化生成网页的工作,成成觉得他需要引入一套模板生成系统。

  模板是包含特殊标记的文本。成成用到的模板只包含一种特殊标记,格式为 {{ VAR }},其中 VAR 是一个变量。该标记在模板生成时会被变量 VAR 的值所替代。例如,如果变量 name = "Tom",则 {{ name }} 会生成 Tom。具体的规则如下:

  ·变量名由大小写字母、数字和下划线 (_) 构成,且第一个字符不是数字,长度不超过 16 个字符。

  ·变量名是大小写敏感的,Name 和 name 是两个不同的变量。

  ·变量的值是字符串。

  ·如果标记中的变量没有定义,则生成空串,相当于把标记从模板中删除。

  ·模板不递归生成。也就是说,如果变量的值中包含形如 {{ VAR }} 的内容,不再做进一步的替换。
输入格式
  输入的第一行包含两个整数 m, n,分别表示模板的行数和模板生成时给出的变量个数。

  接下来 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 个字符(不包括双引号)。

  保证输入的所有变量的名字各不相同。
这道题的整体思路不难,主要考察的还是STL以及字符串基本函数的使用。在这里,我首先将所有字符读入到vector中,将所有标记及其对应的变量读入map中,再依次检查每个字符串是否包含特殊标记的开头 “{{” ;若存在,则再找到标记的位置,并在map中寻找其对应变量,最后将特殊标记替换为对应的值;若存在特殊标记但map中没有对应变量,则将其替换为空 "";若不存在,那么直接输出该行字符串。

我自己写的程序在逻辑上并没有什么问题,但在CCF的OJ提交后告诉我用时超时,只给到90分,这让我有点郁闷。我也看过CCF提供的标准答案,与我的答案大同小异,所以我并不知道原因出在哪里。。。如果有好心人能够告诉我原因的话我将不甚感激!

#include<iostream>
#include<string>
#include<map>
#include<vector>
using namespace std;
int main()
{
int m = 0, n = 0, index = 0, i = 0;
string temp, temp1, temp2;
vector<string> vec;
map<string, string> mapl;
cin >> m >> n;
cin.ignore();
for (i = 0; i < m; i++)
{
getline(cin, temp);
vec.push_back(temp);
}
for (i = 0; i < n; i++){
getline(cin, temp);
index = temp.find(" ");
temp1 = temp.substr(0, index);
temp2 = temp.substr(index + 2, temp.length() - index - 3);
mapl[temp1] = temp2;
}
for (i = 0; i < m; i++)
{
temp = vec[i];
index = temp.find("{{");
while (index != string::npos){
int last_index = temp.find("}}", index);
temp1 = temp.substr(index + 3, last_index - index - 4);
if (mapl.count(temp1))
temp.replace(index, last_index - index + 2, mapl[temp1]);
else temp.replace(index, last_index - index + 2, "");
index = temp.find("{{", index);
}
cout << temp << endl;
}
return 0;
}


由于之前已经写过之前每一届CCF的前面三道题,因此这里不再放出前面写的代码。只将从这个寒假开始写的所有代码放在博客上。

希望能在4月份的CCF考试上取得好成绩。

2016/2/3添加:

整理思路后重新写了一遍代码,发现了问题所在:我之前写的代码每次在搜索 "{{" 时都从上一次找到 “{{” 的位置开始搜索,导致大量的重复搜索(每次都要重复搜索一遍变量代表的值),从而导致了超时。这一次我在修改之后直接跳到变量之后再开始搜索,果然就通过了。

#include<iostream>
#include<string>
#include<map>
#include<vector>
using namespace std;
int main()
{
int m, n, i;
vector<string> vec;
map<string, string> mapl;
string temp;
cin >> m >> n;
cin.ignore();
for (i = 0; i < m; i++)
{
getline(cin, temp);
vec.push_back(temp);
}
for (i = 0; i < n; i++)
{
getline(cin, temp);
int pos = temp.find(" ");
mapl[temp.substr(0, pos)] = temp.substr(pos + 2, temp.length() - pos - 3);
}
for (i = 0; i < m; i++)
{
int pos = vec[i].find("{{");
while (pos != string::npos)
{
int pos1 = vec[i].find("}}", pos);
string temp1 = vec[i].substr(pos + 3, pos1 - pos - 4);
if (mapl.count(temp1))
{
vec[i].replace(pos, pos1 - pos + 2, mapl[temp1]);
}
else vec[i].replace(pos, pos1 - pos + 2, "");
pos = vec[i].find("{{", pos + mapl[temp1].length());//这一行就是之前超时的罪魁祸首
}
cout << vec[i] << endl;
}
return 0;
}


PS:经过测试发现map利用insert添加成员比我在代码中使用的直接赋值更加节约时间。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: